From 9c1eded8c8a3ede395d8318bed01c0aa19732510 Mon Sep 17 00:00:00 2001 From: Noor Eldeen Salah Date: Wed, 21 Sep 2022 15:08:10 +0200 Subject: [PATCH] Add test helper utility in TypeScript template (#554) * Add test helper utility in TypeScript template * Increase tap's `--timeout` flag to `300` --- package.json | 2 +- templates/app-ts/test/helper.ts | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 2de66986..1ba63616 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "scripts": { "lint": "standard", "lint:fix": "standard --fix", - "unit": "tap \"test/**/*.test.{js,ts}\" \"templates/**/*.test.{js,ts}\" --timeout 120", + "unit": "tap \"test/**/*.test.{js,ts}\" \"templates/**/*.test.{js,ts}\" --timeout 300", "pretest": "xcopy /e /k /i . \"..\\node_modules\\fastify-cli\" || rsync -r --exclude=node_modules ./ node_modules/fastify-cli || echo 'this is fine'", "test": "npm run lint && npm run unit && npm run test:typescript", "test:typescript": "tsd templates/plugin && tsc --project templates/app-ts/tsconfig.json && del-cli templates/app-ts/dist" diff --git a/templates/app-ts/test/helper.ts b/templates/app-ts/test/helper.ts index 72aad8d6..849731b7 100644 --- a/templates/app-ts/test/helper.ts +++ b/templates/app-ts/test/helper.ts @@ -1,11 +1,12 @@ // This file contains code that we reuse between our tests. -import Fastify from 'fastify' -import fp from 'fastify-plugin' -import App from '../src/app' +const helper = require('fastify-cli/helper.js') +import * as path from 'path' import * as tap from 'tap'; export type Test = typeof tap['Test']['prototype']; +const AppPath = path.join(__dirname, '..', 'src', 'app.ts') + // Fill in this config with all the configurations // needed for testing the application async function config () { @@ -14,14 +15,13 @@ async function config () { // Automatically build and tear down our instance async function build (t: Test) { - const app = Fastify() + // you can set all the options supported by the fastify CLI command + const argv = [AppPath] // fastify-plugin ensures that all decorators // are exposed for testing purposes, this is // different from the production setup - void app.register(fp(App), await config()) - - await app.ready(); + const app = await helper.build(argv, await config()) // Tear down our app after we are done t.teardown(() => void app.close())