|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { expect, use } from "chai"; |
| 3 | +import * as chaiAsPromised from "chai-as-promised"; |
| 4 | +import * as sinon from "sinon"; |
| 5 | +import * as routes from "../src/routes"; |
| 6 | +import * as fastify from "fastify"; |
| 7 | + |
| 8 | +use(chaiAsPromised); |
| 9 | +describe("Integration", () => { |
| 10 | + |
| 11 | + let sandbox: sinon.SinonSandbox; |
| 12 | + beforeEach(() => { |
| 13 | + sandbox = sinon.createSandbox(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + sandbox.restore(); |
| 18 | + }); |
| 19 | + |
| 20 | + const fakeFastify: any = { |
| 21 | + listen: sinon.spy(), |
| 22 | + get: sinon.spy(), |
| 23 | + log: { |
| 24 | + error: sinon.spy(), |
| 25 | + }, |
| 26 | + server: { |
| 27 | + close: sinon.spy(), |
| 28 | + }, |
| 29 | + }; |
| 30 | + |
| 31 | + before(() => { |
| 32 | + const fastifyStub = sinon.stub(fastify, "fastify"); |
| 33 | + fastifyStub.returns(fakeFastify); |
| 34 | + }); |
| 35 | + |
| 36 | + let app: any; |
| 37 | + it("Successful app startup", async () => { |
| 38 | + const mock = sandbox.mock(routes); |
| 39 | + |
| 40 | + // conditional loading as we can only test this when module loads FIRST time as its auto executing! |
| 41 | + // This is just as app auto executes itself on start |
| 42 | + app = await import("../src/app"); |
| 43 | + |
| 44 | + mock.expects("register").calledOnce; |
| 45 | + expect(fakeFastify.listen.calledOnce, "listen called once").to.be.true; |
| 46 | + expect(fakeFastify.get.calledTwice, "get called twice").to.be.true; |
| 47 | + }); |
| 48 | + |
| 49 | + it("Failed app startup", async () => { |
| 50 | + const exitStub = sandbox.stub(process, "exit"); |
| 51 | + fakeFastify.listen = () => { throw new Error(); }; |
| 52 | + await app.start(); |
| 53 | + expect(fakeFastify.log.error.calledOnce, "log.error called once").to.be.true; |
| 54 | + expect(exitStub.calledOnce, "process.exit called once").to.be.true; |
| 55 | + }); |
| 56 | + |
| 57 | + it("Close the server", async () => { |
| 58 | + await app.stop(); |
| 59 | + expect(fakeFastify.server.close.calledOnce, "server close called once").to.be.true; |
| 60 | + }); |
| 61 | +}); |
0 commit comments