Skip to content
Draft
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ For production mode

Run the test cases.

### `yarn run typecheck`

Run TypeScript type checking without emitting build output.

### `yarn run lint`

Run the linter.
Expand Down
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
"test": "test"
},
"scripts": {
"test": "yarn build:test && FASTIFY_AUTOLOAD_TYPESCRIPT=1 node --test --test-timeout=5000 --experimental-test-coverage 'dist/test/**/*.js'",
"build": "tsc",
"build:test": "yarn build && node scripts/copy-test-assets.js && tsc -p test/tsconfig.json",
"watch": "tsc -w",
"test": "FASTIFY_AUTOLOAD_TYPESCRIPT=1 tsx --test --test-timeout=5000 --experimental-test-coverage test/**/*.test.ts",
"build": "node scripts/clean-dist.js && tsc",
"typecheck": "tsc --noEmit && tsc -p test/tsconfig.json --noEmit",
"watch": "tsc --noEmit --watch",
"start": "fastify start -l info dist/src/app.js --options",
"dev": "yarn run build && concurrently -k -p '[{name}]' -n TypeScript,App -c yellow.bold,cyan.bold yarn:watch yarn:dev:start",
"dev:start": "fastify start --ignore-watch='.ts$' -w -l debug -P dist/src/app.js --options",
"dev": "node scripts/clean-dist.js && FASTIFY_AUTOLOAD_TYPESCRIPT=1 tsx watch scripts/dev.ts",
"lint": "eslint",
"lint:fix": "eslint --fix",
"commitlint": "commitlint --last --strict --verbose",
Expand Down Expand Up @@ -51,7 +50,6 @@
"@trivago/prettier-plugin-sort-imports": "^5.2.2",
"@types/jsonwebtoken": "^9.0.10",
"@types/node": "^24.10.1",
"concurrently": "^9.2.1",
"eslint": "^9.39.1",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.5.4",
Expand All @@ -60,6 +58,7 @@
"husky": "^9.1.7",
"prettier": "^3.7.4",
"prettier-plugin-jsdoc": "^1.7.0",
"tsx": "^4.21.0",
"typescript": "^5.9.3",
"typescript-eslint": "^8.48.1"
},
Expand Down
8 changes: 8 additions & 0 deletions scripts/clean-dist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { rm } from "node:fs/promises";

try {
await rm("dist", { recursive: true, force: true });
} catch (error) {
console.error(error);
process.exitCode = 1;
}
25 changes: 0 additions & 25 deletions scripts/copy-test-assets.js

This file was deleted.

20 changes: 20 additions & 0 deletions scripts/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import app, { options } from "../src/app.js";
import Fastify from "fastify";

const fastify = Fastify({
pluginTimeout: options.pluginTimeout,
logger: { level: "debug" },
});

await fastify.register(app, options);

const port = Number(process.env.PORT ?? 3000);
const host = process.env.ADDRESS;
const listenOptions = host ? { port, host } : { port };

try {
await fastify.listen(listenOptions);
} catch (error) {
fastify.log.error(error);
process.exitCode = 1;
}
27 changes: 10 additions & 17 deletions test/helper.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
// This file contains code that we reuse between our tests.
import { AppOptions } from "../src/app.js";
import helper from "fastify-cli/helper.js";
import app, { AppOptions, options } from "../src/app.js";
import Fastify from "fastify";
import * as test from "node:test";
import * as path from "path";
import { fileURLToPath } from "url";

export type TestContext = {
after: typeof test.after;
};

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const AppPath = path.join(__dirname, "..", "src", "app.js");

// Fill in this config with all the configurations
// needed for testing the application
async function config(): Promise<AppOptions> {
Expand All @@ -26,18 +20,17 @@ async function config(): Promise<AppOptions> {

// Automatically build and tear down our instance
async function build(t: TestContext) {
// 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
const app = await helper.build(argv, await config(), await config());
const fastify = Fastify({ pluginTimeout: options.pluginTimeout });
const appConfig = await config();
await fastify.register(app, appConfig);
await fastify.ready();

// Tear down our app after we are done
t.after(() => void app.close());
t.after(async () => {
await fastify.close();
});

return app;
return fastify;
}

export { config, build };
Loading