Skip to content

Commit 8f1f73f

Browse files
committed
test: allow windows line endings, node-version-specific path displays
1 parent be3b34f commit 8f1f73f

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

src/test/esm-loader.spec.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,15 @@ test.suite('esm', (test) => {
5050
});
5151
expect(r.err).not.toBe(null);
5252
// on node 20, this will be a path. prior versions, a file: url
53-
const expectedModulePath = join(TEST_DIR, './esm/throw error.ts');
54-
const expectedModuleUrl = pathToFileURL(expectedModulePath).toString();
55-
const expectedModulePrint = versionGteLt(process.versions.node, '20.0.0') ? expectedModulePath : expectedModuleUrl;
53+
// on windows in node 20, it's a quasi-url like d:/path/to/throw%20error.ts
5654
expect(r.err!.message).toMatch(
57-
[`${expectedModulePrint}:100`, " bar() { throw new Error('this is a demo'); }"].join('\n')
55+
/[\\\/]throw( |%20)error\.ts:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/
5856
);
5957
// the ^ and number of line-breaks is platform-specific
6058
// also, node 20 puts the type in here when source mapping it, so it
6159
// shows as Foo.Foo.bar
6260
expect(r.err!.message).toMatch(/^ at (Foo\.){1,2}bar \(/m);
63-
expect(r.err!.message).toMatch(`Foo.bar (${expectedModulePrint}:100:17)`);
61+
expect(r.err!.message).toMatch(/^ at (Foo\.){1,2}bar ([^\n]+[\\\/]throw( |%20)error\.ts:100:17)`/m);
6462
});
6563

6664
test.suite('supports experimental-specifier-resolution=node', (test) => {
@@ -97,8 +95,10 @@ test.suite('esm', (test) => {
9795
cwd: join(TEST_DIR, './esm-import-http-url'),
9896
});
9997
expect(r.err).not.toBe(null);
100-
// expect error from node's default resolver
101-
expect(r.stderr).toMatch(/Error \[ERR_UNSUPPORTED_ESM_URL_SCHEME\]:.*(?:\n.*){0,10}\n *at default(Load|Resolve)/);
98+
// expect error from node's default resolver, has a few different names in different node versions
99+
expect(r.stderr).toMatch(
100+
/Error \[ERR_UNSUPPORTED_ESM_URL_SCHEME\]:.*(?:\n.*){0,10}\n *at (default|next)(Load|Resolve)/
101+
);
102102
});
103103

104104
test('should bypass import cache when changing search params', async () => {

src/test/index.spec.ts

+9-21
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ test.suite('ts-node', (test) => {
181181
throw new Error('Command was expected to fail, but it succeeded.');
182182
}
183183

184-
expect(r.err.message).toMatch(
185-
[`${join(TEST_DIR, 'throw error.ts')}:100`, " bar() { throw new Error('this is a demo'); }"].join('\n')
186-
);
184+
expect(r.err.message.replace(/\r\n/g, '\n')).toMatch(/throw( |%20)error\.ts:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/);
187185
});
188186

189187
test('should work with source maps in --transpile-only mode', async () => {
@@ -192,9 +190,7 @@ test.suite('ts-node', (test) => {
192190
throw new Error('Command was expected to fail, but it succeeded.');
193191
}
194192

195-
expect(r.err.message).toMatch(
196-
[`${join(TEST_DIR, 'throw error.ts')}:100`, " bar() { throw new Error('this is a demo'); }"].join('\n')
197-
);
193+
expect(r.err.message.replace(/\r\n/g, '\n')).toMatch(/throw( |%20)error\.ts:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/);
198194
});
199195

200196
test('eval should work with source maps', async () => {
@@ -203,9 +199,7 @@ test.suite('ts-node', (test) => {
203199
throw new Error('Command was expected to fail, but it succeeded.');
204200
}
205201

206-
expect(r.err.message).toMatch(
207-
[`${join(TEST_DIR, 'throw error.ts')}:100`, " bar() { throw new Error('this is a demo'); }"].join('\n')
208-
);
202+
expect(r.err.message.replace(/\r\n/g, '\n')).toMatch(/throw( |%20)error\.ts:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/);
209203
});
210204

211205
for (const flavor of [
@@ -294,20 +288,16 @@ test.suite('ts-node', (test) => {
294288
test('should use source maps with react tsx', async () => {
295289
const r = await exec(`${CMD_TS_NODE_WITH_PROJECT_FLAG} "throw error react tsx.tsx"`);
296290
expect(r.err).not.toBe(null);
297-
expect(r.err!.message).toMatch(
298-
[`${join(TEST_DIR, './throw error react tsx.tsx')}:100`, " bar() { throw new Error('this is a demo'); }"].join(
299-
'\n'
300-
)
291+
expect(r.err!.message.replace(/\r\n/g, '\n')).toMatch(
292+
/throw( |%20)error( |%20)react( |%20)tsx\.tsx:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/
301293
);
302294
});
303295

304296
test('should use source maps with react tsx in --transpile-only mode', async () => {
305297
const r = await exec(`${CMD_TS_NODE_WITH_PROJECT_FLAG} --transpile-only "throw error react tsx.tsx"`);
306298
expect(r.err).not.toBe(null);
307-
expect(r.err!.message).toMatch(
308-
[`${join(TEST_DIR, './throw error react tsx.tsx')}:100`, " bar() { throw new Error('this is a demo'); }"].join(
309-
'\n'
310-
)
299+
expect(r.err!.message.replace(/\r\n/g, '\n')).toMatch(
300+
/throw( |%20)error( |%20)react( |%20)tsx\.tsx:100\n bar\(\) \{ throw new Error\('this is a demo'\); \}/
311301
);
312302
});
313303

@@ -420,8 +410,7 @@ test.suite('ts-node', (test) => {
420410
expect(r.err).not.toBe(null);
421411
expect(r.stderr.replace(/\r\n/g, '\n')).toMatch(
422412
'TSError: ⨯ Unable to compile TypeScript:\n' +
423-
"maxnodemodulesjsdepth/other.ts(4,7): error TS2322: Type 'string' is not assignable to type 'boolean'.\n" +
424-
'\n'
413+
"maxnodemodulesjsdepth/other.ts(4,7): error TS2322: Type 'string' is not assignable to type 'boolean'."
425414
);
426415
});
427416

@@ -430,8 +419,7 @@ test.suite('ts-node', (test) => {
430419
expect(r.err).not.toBe(null);
431420
expect(r.stderr.replace(/\r\n/g, '\n')).toMatch(
432421
'TSError: ⨯ Unable to compile TypeScript:\n' +
433-
"maxnodemodulesjsdepth-scoped/other.ts(7,7): error TS2322: Type 'string' is not assignable to type 'boolean'.\n" +
434-
'\n'
422+
"maxnodemodulesjsdepth-scoped/other.ts(7,7): error TS2322: Type 'string' is not assignable to type 'boolean'."
435423
);
436424
});
437425
});

src/test/register.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ test.suite('register(create(options))', (test) => {
107107
try {
108108
require('../../tests/throw error');
109109
} catch (error: any) {
110-
exp(error.stack).toMatch(
111-
['Error: this is a demo', ` at Foo.bar (${join(TEST_DIR, './throw error.ts')}:100:17)`].join('\n')
112-
);
110+
// on windows in node 20, this is printed as a quasi-url, like
111+
// d:/path/to/throw%20error.ts
112+
exp(error.stack).toMatch(/Error: this is a demo\n at Foo\.bar \([^)]+[\\\/]throw( %20)error\.ts:100:17\)\n/);
113113
}
114114
});
115115

0 commit comments

Comments
 (0)