Skip to content

fix: make generated TypeScript ESM tests cross-platform - #919

Open
GhaythBenAbid wants to merge 2 commits into
fastify:mainfrom
GhaythBenAbid:fix/windows-ts-esm-test-command
Open

GhaythBenAbid wants to merge 2 commits into
fastify:mainfrom
GhaythBenAbid:fix/windows-ts-esm-test-command

Conversation

@GhaythBenAbid

Copy link
Copy Markdown

Fixes #835

The generated TypeScript ESM test command currently uses POSIX inline environment-variable syntax, which fails in Windows Command Prompt. It also uses the deprecated Node loader flag.

This change adds a small preload module that sets FASTIFY_AUTOLOAD_TYPESCRIPT and registers ts-node through node:module. The generated test script loads it with the cross-platform --import option.

The ESM generator test now exercises the actual app-ts-esm template, including the preload module.

Verification:

  • ESLint passes for the changed files
  • Focused TypeScript and TypeScript ESM generator tests pass
  • unit:cli-js passes: 229 passed, 3 skipped
  • Generated TypeScript ESM test runner passes: 4 passed

@SameRainbows

Copy link
Copy Markdown

Tested locally on Node 24 (Windows).

Moving FASTIFY_AUTOLOAD_TYPESCRIPT=1 and ts-node/esm registration into register-ts-esm.mjs loaded via --import cleanly resolves the Windows cmd.exe syntax issue and removes the loader deprecation warning.

One small cross-platform fix is needed in test/generate-typescript-esm.test.js:

On Windows, the walker yields backslash paths like \package.json, so line 116's filter does not match:

const copiedFileCount = Object.keys(expected).filter(file => file !== '/package.json').length

Because '\package.json' !== '/package.json' evaluates to true, \package.json is included in copiedFileCount, raising the expected assertions to 42 while only 41 run (verifyCopy skips package.json):

✖ should finish successfully with typescript template (1708.2601ms)
  'plan expected 42 assertions but received 41'

Normalizing separators or checking !file.endsWith('package.json') fixes it:

const copiedFileCount = Object.keys(expected).filter(file => !file.endsWith('package.json')).length

With that adjustment, all 6 tests in test/generate-typescript-esm.test.js pass cleanly on Windows.

GhaythBenAbid and others added 2 commits November 14, 2026 21:25
On Windows the walker yields keys like `\package.json`, so comparing
against `/package.json` never matched and the planned assertion count
was one too high ("plan expected 42 assertions but received 41").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@GhaythBenAbid
GhaythBenAbid force-pushed the fix/windows-ts-esm-test-command branch from 6fcb258 to d7f3270 Compare November 14, 2026 20:28
@GhaythBenAbid

Copy link
Copy Markdown
Author

Thanks for testing on Windows, @SameRainbows. Good catch. Fixed in d7f3270 by normalizing path separators before the comparison (file.replace(/\\/g, '/') !== '/package.json') rather than endsWith, so only the root package.json is excluded.

I also rebased onto the latest main. npm test passes locally (unit:cli-js: 229 passed, 3 skipped).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated Fastify ESM/TS app has broken npm test command (FASTIFY_AUTOLOAD_TYPESCRIPT)

2 participants