Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test helper utility in TypeScript template #554

Merged
merged 3 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 7 additions & 7 deletions templates/app-ts/test/helper.ts
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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())
Expand Down