Skip to content

Commit

Permalink
test: allow windows line endings, node-version-specific path displays
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Sep 7, 2023
1 parent be3b34f commit 8f1f73f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
14 changes: 7 additions & 7 deletions src/test/esm-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 () => {
Expand Down
30 changes: 9 additions & 21 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 [
Expand Down Expand Up @@ -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'\); \}/
);
});

Expand Down Expand Up @@ -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'."
);
});

Expand All @@ -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'."
);
});
});
Expand Down
6 changes: 3 additions & 3 deletions src/test/register.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
}
});

Expand Down

0 comments on commit 8f1f73f

Please sign in to comment.