$ mkdir myapp && cd myapp
$ yarn init -y
$ yarn add fastify fastify-autoload fastify-cli fastify-plugin fastify-sensible
$ yarn add -D jest ts-jest @types/jest ts-node @types/node fastify-tsconfig typescript cross-env concurrently
$ mkdir -p src/routes test/routes
$ touch .gitignore tsconfig.json src/app.ts src/routes/root.ts test/helper.ts test/routes/root.test.ts
dist
node_modules
coverage
$ git init
{
"extends": "fastify-tsconfig",
"compilerOptions": {
"outDir": "dist"
},
"include": ["src/**/*.ts"]
}
$ yarn ts-jest config:init
"scripts": {
"test": "jest --coverage",
"start": "npm run build:ts && fastify start -l info dist/app.js",
"build:ts": "tsc",
"dev": "tsc && concurrently -k -p \"[{name}]\" -n \"Typescript,App\" -c \"yellow.bold,cyan.bold\" \"tsc -w\" \"fastify start --ignore-watch=.ts$ -w -l info -P dist/app.js\""
},
import { join } from "path"
import AutoLoad from "fastify-autoload"
import { FastifyPluginAsync } from "fastify"
const app: FastifyPluginAsync = async (fastify) => {
void fastify.register(AutoLoad, {
dir: join(__dirname, 'routes')
})
}
export default app
export { app }
import { FastifyPluginAsync } from "fastify"
const root: FastifyPluginAsync = async (fastify) => {
fastify.get('/', async (req, res) => {
return { root: true }
})
}
export default root
import Fastify from "fastify"
import fp from "fastify-plugin"
import App from "../src/app"
export function build() {
const app = Fastify()
beforeAll(async () => {
void app.register(fp(App))
await app.ready()
})
afterAll(() => app.close())
return app
}
import { build } from "../helper"
describe("Root route", () => {
const app = build()
it("works", async () => {
const res = await app.inject({ url: "/" })
expect(res.json()).toEqual({ root: true })
})
})
$ git add .
$ git commit -m "Empty Fastify project"
$ yarn test
$ yarn dev