-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Examples with fastify added and working. Fixed the nyc issue (all: tr…
…ue does not work..)
- Loading branch information
Showing
15 changed files
with
129 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ export async function stop(): Promise<void> { | |
await app.server.close(); | ||
} | ||
|
||
// start(); | ||
start(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
import { expect } from "chai"; | ||
import { hello } from "./hello"; | ||
import { fastify} from "fastify"; | ||
|
||
describe("Hello", () => { | ||
it("handler", async () => { | ||
const app = fastify(); | ||
app.get("/", hello); | ||
const response = await app.inject({ | ||
method: "GET", | ||
url: "/", | ||
}); | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.headers["content-type"]).to.equal("text/html; charset=utf-8"); | ||
expect(response.body).to.contain("Hallo!"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { FastifyRequest, FastifyReply } from "fastify"; | ||
|
||
export async function hello(req: FastifyRequest, reply: FastifyReply): Promise<string> { | ||
reply.type("text/html; charset=utf-8").code(200); | ||
return "<h1>Hallo!</h1><a href='/persons'>Fetch persons here</a>"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
import { expect } from "chai"; | ||
import { getAll } from "./persons"; | ||
import { fastify} from "fastify"; | ||
|
||
describe("Persons", () => { | ||
it("handler", async () => { | ||
const app = fastify(); | ||
app.get("/", getAll); | ||
const response = await app.inject({ | ||
method: "GET", | ||
url: "/", | ||
}); | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.headers["content-type"]).to.equal("application/json; charset=utf-8"); | ||
const persons = JSON.parse(response.body); | ||
expect(persons.length).to.equal(1); | ||
expect(persons[0].firstName).to.equal("Rick"); | ||
expect(persons[0].lastName).to.equal("Sanchez"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { FastifyInstance } from "fastify"; | ||
import * as persons from "./handler/persons"; | ||
import { getAll } from "./handler/persons"; | ||
import { hello } from "./handler/hello"; | ||
|
||
export function register(app: FastifyInstance): void { | ||
app.get("/persons", persons.getAll); | ||
app.get("/", hello); | ||
app.get("/persons", getAll); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// intentionally uncovered to show nyc corectly finds unused files as well. | ||
export function unused(): void { | ||
console.log("This should be uncovered code"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { expect, use } from "chai"; | ||
import * as chaiAsPromised from "chai-as-promised"; | ||
import * as sinon from "sinon"; | ||
import * as routes from "../src/routes"; | ||
import * as fastify from "fastify"; | ||
|
||
use(chaiAsPromised); | ||
describe("Integration", () => { | ||
|
||
let sandbox: sinon.SinonSandbox; | ||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
const fakeFastify: any = { | ||
listen: sinon.spy(), | ||
get: sinon.spy(), | ||
log: { | ||
error: sinon.spy(), | ||
}, | ||
server: { | ||
close: sinon.spy(), | ||
}, | ||
}; | ||
|
||
before(() => { | ||
const fastifyStub = sinon.stub(fastify, "fastify"); | ||
fastifyStub.returns(fakeFastify); | ||
}); | ||
|
||
let app: any; | ||
it("Successful app startup", async () => { | ||
const mock = sandbox.mock(routes); | ||
|
||
// conditional loading as we can only test this when module loads FIRST time as its auto executing! | ||
// This is just as app auto executes itself on start | ||
app = await import("../src/app"); | ||
|
||
mock.expects("register").calledOnce; | ||
expect(fakeFastify.listen.calledOnce, "listen called once").to.be.true; | ||
expect(fakeFastify.get.calledTwice, "get called twice").to.be.true; | ||
}); | ||
|
||
it("Failed app startup", async () => { | ||
const exitStub = sandbox.stub(process, "exit"); | ||
fakeFastify.listen = () => { throw new Error(); }; | ||
await app.start(); | ||
expect(fakeFastify.log.error.calledOnce, "log.error called once").to.be.true; | ||
expect(exitStub.calledOnce, "process.exit called once").to.be.true; | ||
}); | ||
|
||
it("Close the server", async () => { | ||
await app.stop(); | ||
expect(fakeFastify.server.close.calledOnce, "server close called once").to.be.true; | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.