From ec65297ccc76ad4ee22f1708875608b3417e36a6 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Fri, 30 Aug 2024 15:33:41 +0200 Subject: [PATCH] test: check typescript loader --- test/es-module/test-typescript.mjs | 26 ++++++++++++++++++++++ test/fixtures/typescript/ts/hook.ts | 11 +++++++++ test/fixtures/typescript/ts/test-loader.ts | 4 ++++ test/fixtures/typescript/ts/test-simple.js | 2 ++ 4 files changed, 43 insertions(+) create mode 100644 test/fixtures/typescript/ts/hook.ts create mode 100644 test/fixtures/typescript/ts/test-loader.ts create mode 100644 test/fixtures/typescript/ts/test-simple.js diff --git a/test/es-module/test-typescript.mjs b/test/es-module/test-typescript.mjs index 495c82ffd9e79b..dbcbebbb2682e6 100644 --- a/test/es-module/test-typescript.mjs +++ b/test/es-module/test-typescript.mjs @@ -323,3 +323,29 @@ test('expect error when executing a TypeScript file with generics', async () => strictEqual(result.stdout, ''); strictEqual(result.code, 1); }); + +test('execute a TypeScript loader and a .ts file', async () => { + const result = await spawnPromisified(process.execPath, [ + '--experimental-strip-types', + '--no-warnings', + '--import', + fixtures.fileURL('typescript/ts/test-loader.ts'), + fixtures.path('typescript/ts/test-typescript.ts'), + ]); + strictEqual(result.stderr, ''); + match(result.stdout, /Hello, TypeScript!/); + strictEqual(result.code, 0); +}); + +test('execute a TypeScript loader and a .js file', async () => { + const result = await spawnPromisified(process.execPath, [ + '--experimental-strip-types', + '--no-warnings', + '--import', + fixtures.fileURL('typescript/ts/test-loader.ts'), + fixtures.path('typescript/ts/test-simple.js'), + ]); + strictEqual(result.stderr, ''); + match(result.stdout, /Hello, TypeScript!/); + strictEqual(result.code, 0); +}); diff --git a/test/fixtures/typescript/ts/hook.ts b/test/fixtures/typescript/ts/hook.ts new file mode 100644 index 00000000000000..e0dd46448b837e --- /dev/null +++ b/test/fixtures/typescript/ts/hook.ts @@ -0,0 +1,11 @@ +import type { ResolveHook } from 'node:module'; + +// Pass through +export const resolve: ResolveHook = async function resolve(specifier, context, nextResolve) { + if(false){ + // https://github.com/nodejs/node/issues/54645 + // A bug in the typescript parsers swc causes + // the next line to not be parsed correctly + } + return nextResolve(specifier, context); +}; diff --git a/test/fixtures/typescript/ts/test-loader.ts b/test/fixtures/typescript/ts/test-loader.ts new file mode 100644 index 00000000000000..8b957bf72f0aa0 --- /dev/null +++ b/test/fixtures/typescript/ts/test-loader.ts @@ -0,0 +1,4 @@ +import { register } from 'node:module'; +import * as fixtures from '../../../common/fixtures.mjs'; + +register(fixtures.fileURL('typescript/ts/hook.ts')); diff --git a/test/fixtures/typescript/ts/test-simple.js b/test/fixtures/typescript/ts/test-simple.js new file mode 100644 index 00000000000000..f738e60f7d61db --- /dev/null +++ b/test/fixtures/typescript/ts/test-simple.js @@ -0,0 +1,2 @@ +const str = "Hello, TypeScript!"; +console.log(str);