From 8f1f73fdae6795141f965133a8ae97a362b8c6e5 Mon Sep 17 00:00:00 2001 From: isaacs Date: Thu, 7 Sep 2023 12:54:32 -0700 Subject: [PATCH] test: allow windows line endings, node-version-specific path displays --- src/test/esm-loader.spec.ts | 14 +++++++------- src/test/index.spec.ts | 30 +++++++++--------------------- src/test/register.spec.ts | 6 +++--- 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/test/esm-loader.spec.ts b/src/test/esm-loader.spec.ts index 39d5c14c9..44626a38d 100644 --- a/src/test/esm-loader.spec.ts +++ b/src/test/esm-loader.spec.ts @@ -50,17 +50,15 @@ test.suite('esm', (test) => { }); expect(r.err).not.toBe(null); // on node 20, this will be a path. prior versions, a file: url - const expectedModulePath = join(TEST_DIR, './esm/throw error.ts'); - const expectedModuleUrl = pathToFileURL(expectedModulePath).toString(); - const expectedModulePrint = versionGteLt(process.versions.node, '20.0.0') ? expectedModulePath : expectedModuleUrl; + // on windows in node 20, it's a quasi-url like d:/path/to/throw%20error.ts expect(r.err!.message).toMatch( - [`${expectedModulePrint}:100`, " bar() { throw new Error('this is a demo'); }"].join('\n') + /[\\\/]throw( |%20)error\.ts:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/ ); // the ^ and number of line-breaks is platform-specific // also, node 20 puts the type in here when source mapping it, so it // shows as Foo.Foo.bar expect(r.err!.message).toMatch(/^ at (Foo\.){1,2}bar \(/m); - expect(r.err!.message).toMatch(`Foo.bar (${expectedModulePrint}:100:17)`); + expect(r.err!.message).toMatch(/^ at (Foo\.){1,2}bar ([^\n]+[\\\/]throw( |%20)error\.ts:100:17)`/m); }); test.suite('supports experimental-specifier-resolution=node', (test) => { @@ -97,8 +95,10 @@ test.suite('esm', (test) => { cwd: join(TEST_DIR, './esm-import-http-url'), }); expect(r.err).not.toBe(null); - // expect error from node's default resolver - expect(r.stderr).toMatch(/Error \[ERR_UNSUPPORTED_ESM_URL_SCHEME\]:.*(?:\n.*){0,10}\n *at default(Load|Resolve)/); + // expect error from node's default resolver, has a few different names in different node versions + expect(r.stderr).toMatch( + /Error \[ERR_UNSUPPORTED_ESM_URL_SCHEME\]:.*(?:\n.*){0,10}\n *at (default|next)(Load|Resolve)/ + ); }); test('should bypass import cache when changing search params', async () => { diff --git a/src/test/index.spec.ts b/src/test/index.spec.ts index 417871239..ff22c837b 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -181,9 +181,7 @@ test.suite('ts-node', (test) => { throw new Error('Command was expected to fail, but it succeeded.'); } - expect(r.err.message).toMatch( - [`${join(TEST_DIR, 'throw error.ts')}:100`, " bar() { throw new Error('this is a demo'); }"].join('\n') - ); + expect(r.err.message.replace(/\r\n/g, '\n')).toMatch(/throw( |%20)error\.ts:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/); }); test('should work with source maps in --transpile-only mode', async () => { @@ -192,9 +190,7 @@ test.suite('ts-node', (test) => { throw new Error('Command was expected to fail, but it succeeded.'); } - expect(r.err.message).toMatch( - [`${join(TEST_DIR, 'throw error.ts')}:100`, " bar() { throw new Error('this is a demo'); }"].join('\n') - ); + expect(r.err.message.replace(/\r\n/g, '\n')).toMatch(/throw( |%20)error\.ts:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/); }); test('eval should work with source maps', async () => { @@ -203,9 +199,7 @@ test.suite('ts-node', (test) => { throw new Error('Command was expected to fail, but it succeeded.'); } - expect(r.err.message).toMatch( - [`${join(TEST_DIR, 'throw error.ts')}:100`, " bar() { throw new Error('this is a demo'); }"].join('\n') - ); + expect(r.err.message.replace(/\r\n/g, '\n')).toMatch(/throw( |%20)error\.ts:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/); }); for (const flavor of [ @@ -294,20 +288,16 @@ test.suite('ts-node', (test) => { test('should use source maps with react tsx', async () => { const r = await exec(`${CMD_TS_NODE_WITH_PROJECT_FLAG} "throw error react tsx.tsx"`); expect(r.err).not.toBe(null); - expect(r.err!.message).toMatch( - [`${join(TEST_DIR, './throw error react tsx.tsx')}:100`, " bar() { throw new Error('this is a demo'); }"].join( - '\n' - ) + expect(r.err!.message.replace(/\r\n/g, '\n')).toMatch( + /throw( |%20)error( |%20)react( |%20)tsx\.tsx:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/ ); }); test('should use source maps with react tsx in --transpile-only mode', async () => { const r = await exec(`${CMD_TS_NODE_WITH_PROJECT_FLAG} --transpile-only "throw error react tsx.tsx"`); expect(r.err).not.toBe(null); - expect(r.err!.message).toMatch( - [`${join(TEST_DIR, './throw error react tsx.tsx')}:100`, " bar() { throw new Error('this is a demo'); }"].join( - '\n' - ) + expect(r.err!.message.replace(/\r\n/g, '\n')).toMatch( + /throw( |%20)error( |%20)react( |%20)tsx\.tsx:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/ ); }); @@ -420,8 +410,7 @@ test.suite('ts-node', (test) => { expect(r.err).not.toBe(null); expect(r.stderr.replace(/\r\n/g, '\n')).toMatch( 'TSError: ⨯ Unable to compile TypeScript:\n' + - "maxnodemodulesjsdepth/other.ts(4,7): error TS2322: Type 'string' is not assignable to type 'boolean'.\n" + - '\n' + "maxnodemodulesjsdepth/other.ts(4,7): error TS2322: Type 'string' is not assignable to type 'boolean'." ); }); @@ -430,8 +419,7 @@ test.suite('ts-node', (test) => { expect(r.err).not.toBe(null); expect(r.stderr.replace(/\r\n/g, '\n')).toMatch( 'TSError: ⨯ Unable to compile TypeScript:\n' + - "maxnodemodulesjsdepth-scoped/other.ts(7,7): error TS2322: Type 'string' is not assignable to type 'boolean'.\n" + - '\n' + "maxnodemodulesjsdepth-scoped/other.ts(7,7): error TS2322: Type 'string' is not assignable to type 'boolean'." ); }); }); diff --git a/src/test/register.spec.ts b/src/test/register.spec.ts index 9c9f512c2..b6e4cfa08 100644 --- a/src/test/register.spec.ts +++ b/src/test/register.spec.ts @@ -107,9 +107,9 @@ test.suite('register(create(options))', (test) => { try { require('../../tests/throw error'); } catch (error: any) { - exp(error.stack).toMatch( - ['Error: this is a demo', ` at Foo.bar (${join(TEST_DIR, './throw error.ts')}:100:17)`].join('\n') - ); + // on windows in node 20, this is printed as a quasi-url, like + // d:/path/to/throw%20error.ts + exp(error.stack).toMatch(/Error: this is a demo\n at Foo\.bar \([^)]+[\\\/]throw( %20)error\.ts:100:17\)\n/); } });