diff --git a/generate.js b/generate.js index 535a13c3..d3f9996a 100755 --- a/generate.js +++ b/generate.js @@ -48,7 +48,7 @@ const typescriptTemplate = { main: 'app.ts', scripts: { clean: 'node -e "require(\'fs\').rmSync(\'dist\', {recursive: true, force: true})"', - test: 'npm run build:ts && tsc -p test/tsconfig.json && c8 node --test -r ts-node/register "test/**/*.ts"', + test: 'tsc -p test/tsconfig.json && c8 node --test -r ts-node/register "test/**/*.ts"', start: 'npm run build:ts && fastify start -l info dist/app.js', 'build:ts': 'npm run clean && tsc', 'watch:ts': 'tsc -w', diff --git a/templates/app-ts/README.md b/templates/app-ts/README.md index 2d5e417c..06390acb 100644 --- a/templates/app-ts/README.md +++ b/templates/app-ts/README.md @@ -16,7 +16,13 @@ For production mode ### `npm run test` -Run the test cases. +Type check the application source and tests, then run the test cases directly +against the TypeScript source without generating production files in `dist`. + +### `npm run build:ts` + +Compile the application source to `dist`. This production build runs independently +of the tests and is also run by `npm start`. ## Learn More diff --git a/templates/app-ts/test/tsconfig.json b/templates/app-ts/test/tsconfig.json index 384d1712..d5c6e8ef 100644 --- a/templates/app-ts/test/tsconfig.json +++ b/templates/app-ts/test/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "baseUrl": ".", + "rootDir": "..", "noEmit": true }, "include": ["../src/**/*.ts", "**/*.ts"] diff --git a/templates/app-ts/tsconfig.json b/templates/app-ts/tsconfig.json index 50dd0995..15edbefd 100644 --- a/templates/app-ts/tsconfig.json +++ b/templates/app-ts/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "fastify-tsconfig", "compilerOptions": { + "types": ["node"], + "rootDir": "src", "outDir": "dist", "sourceMap": true }, diff --git a/test/generate-typescript.test.js b/test/generate-typescript.test.js index 80e52a38..18b8e1e1 100644 --- a/test/generate-typescript.test.js +++ b/test/generate-typescript.test.js @@ -103,7 +103,7 @@ function define (t) { }) test('should finish successfully with typescript template', async (t) => { - t.plan(26 + Object.keys(expected).length) + t.plan(28 + Object.keys(expected).length) try { await generate(workdir, typescriptTemplate) await verifyPkg(t) @@ -128,7 +128,7 @@ function define (t) { // by default this will be ISC but since we have a MIT licensed pkg file in upper dir, npm will set the license to MIT in this case // so for local tests we need to accept MIT as well t.assert.ok(pkg.license === 'ISC' || pkg.license === 'MIT') - t.assert.strictEqual(pkg.scripts.test, 'npm run build:ts && tsc -p test/tsconfig.json && c8 node --test -r ts-node/register "test/**/*.ts"') + t.assert.strictEqual(pkg.scripts.test, 'tsc -p test/tsconfig.json && c8 node --test -r ts-node/register "test/**/*.ts"') t.assert.strictEqual(pkg.scripts.start, 'npm run build:ts && fastify start -l info dist/app.js') t.assert.strictEqual(pkg.scripts.clean, 'node -e "require(\'fs\').rmSync(\'dist\', {recursive: true, force: true})"') t.assert.strictEqual(pkg.scripts['build:ts'], 'npm run clean && tsc') @@ -146,7 +146,7 @@ function define (t) { t.assert.strictEqual(pkg.devDependencies.concurrently, cliPkg.devDependencies.concurrently) t.assert.strictEqual(pkg.devDependencies.typescript, cliPkg.devDependencies.typescript) - const testGlob = pkg.scripts.test.split(' ', 14)[13].replaceAll('"', '') + const testGlob = pkg.scripts.test.split(' ').at(-1).replaceAll('"', '') t.assert.strictEqual(minimatch.match(['test/routes/plugins/more/test/here/ok.test.ts'], testGlob).length, 1, 'should match glob') resolve() @@ -162,6 +162,8 @@ function define (t) { const tsConfig = JSON.parse(data) t.assert.strictEqual(tsConfig.extends, 'fastify-tsconfig') + t.assert.deepStrictEqual(tsConfig.compilerOptions.types, ['node']) + t.assert.strictEqual(tsConfig.compilerOptions.rootDir, 'src') t.assert.strictEqual(tsConfig.compilerOptions.outDir, 'dist') t.assert.deepStrictEqual(tsConfig.include, ['src/**/*.ts']) })