Skip to content

Commit ccff67d

Browse files
authored
Make tests faster (#1719)
* add notes and todo list * safely repeating changes from add-cjs-loader-resolve to remember what I did and filter any WIP mess I may have forgotten about * more sync * fix downloaded artifacts * sync * sync * sync * sync * cleanup * sync node-internal-modules-cjs-loader-old.js with raw * sync internal/modules/package_json_reader with new naming convention * sync node-internal-modules-cjs-loader; rename back to old name * sync * more sync of filenames * sync node-internal-errors * lint-fix * fix * fix * further alignment * fix * fix * upgrade v8-compile-cache-lib to get rid of eventemitter warning in tests * Make tests faster * make tests faster * more speed * try running fewer tests up-front to give quicker test feedback * try running fewer tests up-front to give quicker test feedback * try running fewer tests up-front to give quicker test feedback * try running fewer tests up-front to give quicker test feedback * simplify changes to CI
1 parent 97f4ecf commit ccff67d

File tree

11 files changed

+107
-89
lines changed

11 files changed

+107
-89
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ jobs:
173173
- run: |
174174
npm config set cache "$( node -p "process.cwd()" )/temp/npm-cache"
175175
- name: Cache dependencies
176+
if: ${{ matrix.os != 'windows' }}
176177
uses: actions/cache@v2
177178
with:
178179
path: temp/npm-cache

src/test/helpers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export const ROOT_DIR = resolve(__dirname, '../..');
2222
export const DIST_DIR = resolve(__dirname, '..');
2323
export const TEST_DIR = join(__dirname, '../../tests');
2424
export const PROJECT = join(TEST_DIR, 'tsconfig.json');
25+
export const PROJECT_TRANSPILE_ONLY = join(
26+
TEST_DIR,
27+
'tsconfig-transpile-only.json'
28+
);
2529
export const BIN_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node');
2630
export const BIN_PATH_JS = join(TEST_DIR, 'node_modules/ts-node/dist/bin.js');
2731
export const BIN_SCRIPT_PATH = join(
@@ -35,6 +39,8 @@ export const BIN_ESM_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-esm');
3539
//#region command lines
3640
/** Default `ts-node --project` invocation */
3741
export const CMD_TS_NODE_WITH_PROJECT_FLAG = `"${BIN_PATH}" --project "${PROJECT}"`;
42+
/** Default `ts-node --project` invocation with transpile-only */
43+
export const CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG = `"${BIN_PATH}" --project "${PROJECT_TRANSPILE_ONLY}"`;
3844
/** Default `ts-node` invocation without `--project` */
3945
export const CMD_TS_NODE_WITHOUT_PROJECT_FLAG = `"${BIN_PATH}"`;
4046
export const EXPERIMENTAL_MODULES_FLAG = semver.gte(process.version, '12.17.0')

src/test/index.spec.ts

Lines changed: 69 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { tmpdir } from 'os';
55
import semver = require('semver');
66
import {
77
BIN_PATH_JS,
8+
CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG,
89
nodeSupportsEsmHooks,
910
ts,
1011
tsSupportsShowConfig,
@@ -143,7 +144,10 @@ test.suite('ts-node', (test) => {
143144

144145
test('should execute cli with absolute path', async () => {
145146
const { err, stdout } = await exec(
146-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} "${join(TEST_DIR, 'hello-world')}"`
147+
`${CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG} "${join(
148+
TEST_DIR,
149+
'hello-world'
150+
)}"`
147151
);
148152
expect(err).toBe(null);
149153
expect(stdout).toBe('Hello, world!\n');
@@ -157,26 +161,18 @@ test.suite('ts-node', (test) => {
157161
expect(stdout).toBe('example\n');
158162
});
159163

160-
test('should provide registered information globally', async () => {
164+
test("should expose ts-node Service as a symbol property on Node's `process` object", async () => {
161165
const { err, stdout } = await exec(
162-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} env`
166+
`${CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG} env`
163167
);
164168
expect(err).toBe(null);
165169
expect(stdout).toBe('object\n');
166170
});
167171

168-
test('should provide registered information on register', async () => {
169-
const { err, stdout } = await exec(`node -r ts-node/register env.ts`, {
170-
cwd: TEST_DIR,
171-
});
172-
expect(err).toBe(null);
173-
expect(stdout).toBe('object\n');
174-
});
175-
176172
test('should allow js', async () => {
177173
const { err, stdout } = await exec(
178174
[
179-
CMD_TS_NODE_WITH_PROJECT_FLAG,
175+
CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG,
180176
'-O "{\\"allowJs\\":true}"',
181177
'-pe "import { main } from \'./allow-js/run\';main()"',
182178
].join(' ')
@@ -188,7 +184,7 @@ test.suite('ts-node', (test) => {
188184
test('should include jsx when `allow-js` true', async () => {
189185
const { err, stdout } = await exec(
190186
[
191-
CMD_TS_NODE_WITH_PROJECT_FLAG,
187+
CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG,
192188
'-O "{\\"allowJs\\":true}"',
193189
'-pe "import { Foo2 } from \'./allow-js/with-jsx\'; Foo2.sayHi()"',
194190
].join(' ')
@@ -199,21 +195,21 @@ test.suite('ts-node', (test) => {
199195

200196
test('should eval code', async () => {
201197
const { err, stdout } = await exec(
202-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -e "import * as m from './module';console.log(m.example('test'))"`
198+
`${CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG} -e "import * as m from './module';console.log(m.example('test'))"`
203199
);
204200
expect(err).toBe(null);
205201
expect(stdout).toBe('TEST\n');
206202
});
207203

208204
test('should import empty files', async () => {
209205
const { err, stdout } = await exec(
210-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -e "import './empty'"`
206+
`${CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG} -e "import './empty'"`
211207
);
212208
expect(err).toBe(null);
213209
expect(stdout).toBe('');
214210
});
215211

216-
test('should throw errors', async () => {
212+
test('should throw typechecking errors', async () => {
217213
const { err } = await exec(
218214
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -e "import * as m from './module';console.log(m.example(123))"`
219215
);
@@ -377,15 +373,17 @@ test.suite('ts-node', (test) => {
377373
}
378374

379375
test('should pipe into `ts-node` and evaluate', async () => {
380-
const execPromise = exec(CMD_TS_NODE_WITH_PROJECT_FLAG);
376+
const execPromise = exec(CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG);
381377
execPromise.child.stdin!.end("console.log('hello')");
382378
const { err, stdout } = await execPromise;
383379
expect(err).toBe(null);
384380
expect(stdout).toBe('hello\n');
385381
});
386382

387383
test('should pipe into `ts-node`', async () => {
388-
const execPromise = exec(`${CMD_TS_NODE_WITH_PROJECT_FLAG} -p`);
384+
const execPromise = exec(
385+
`${CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG} -p`
386+
);
389387
execPromise.child.stdin!.end('true');
390388
const { err, stdout } = await execPromise;
391389
expect(err).toBe(null);
@@ -404,15 +402,15 @@ test.suite('ts-node', (test) => {
404402

405403
test('should support require flags', async () => {
406404
const { err, stdout } = await exec(
407-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -r ./hello-world -pe "console.log('success')"`
405+
`${CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG} -r ./hello-world -pe "console.log('success')"`
408406
);
409407
expect(err).toBe(null);
410408
expect(stdout).toBe('Hello, world!\nsuccess\nundefined\n');
411409
});
412410

413411
test('should support require from node modules', async () => {
414412
const { err, stdout } = await exec(
415-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -r typescript -e "console.log('success')"`
413+
`${CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG} -r typescript -e "console.log('success')"`
416414
);
417415
expect(err).toBe(null);
418416
expect(stdout).toBe('success\n');
@@ -458,36 +456,25 @@ test.suite('ts-node', (test) => {
458456
);
459457
});
460458

461-
test('should preserve `ts-node` context with child process', async () => {
462-
const { err, stdout } = await exec(
463-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} child-process`
464-
);
465-
expect(err).toBe(null);
466-
expect(stdout).toBe('Hello, world!\n');
467-
});
468-
469459
test('should import js before ts by default', async () => {
470460
const { err, stdout } = await exec(
471-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} import-order/compiled`
461+
`${CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG} import-order/compiled`
472462
);
473463
expect(err).toBe(null);
474464
expect(stdout).toBe('Hello, JavaScript!\n');
475465
});
476466

477-
const preferTsExtsEntrypoint = semver.gte(process.version, '12.0.0')
478-
? 'import-order/compiled'
479-
: 'import-order/require-compiled';
480467
test('should import ts before js when --prefer-ts-exts flag is present', async () => {
481468
const { err, stdout } = await exec(
482-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --prefer-ts-exts ${preferTsExtsEntrypoint}`
469+
`${CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG} --prefer-ts-exts import-order/compiled`
483470
);
484471
expect(err).toBe(null);
485472
expect(stdout).toBe('Hello, TypeScript!\n');
486473
});
487474

488475
test('should import ts before js when TS_NODE_PREFER_TS_EXTS env is present', async () => {
489476
const { err, stdout } = await exec(
490-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} ${preferTsExtsEntrypoint}`,
477+
`${CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG} import-order/compiled`,
491478
{
492479
env: { ...process.env, TS_NODE_PREFER_TS_EXTS: 'true' },
493480
}
@@ -498,7 +485,7 @@ test.suite('ts-node', (test) => {
498485

499486
test('should ignore .d.ts files', async () => {
500487
const { err, stdout } = await exec(
501-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} import-order/importer`
488+
`${CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG} import-order/importer`
502489
);
503490
expect(err).toBe(null);
504491
expect(stdout).toBe('Hello, World!\n');
@@ -538,63 +525,60 @@ test.suite('ts-node', (test) => {
538525
});
539526
});
540527

541-
if (semver.gte(ts.version, '2.7.0')) {
542-
test('should locate tsconfig relative to entry-point by default', async () => {
543-
const { err, stdout } = await exec(`${BIN_PATH} ../a/index`, {
544-
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
545-
});
546-
expect(err).toBe(null);
547-
expect(stdout).toMatch(/plugin-a/);
528+
test('should locate tsconfig relative to entry-point by default', async () => {
529+
const { err, stdout } = await exec(`${BIN_PATH} ../a/index`, {
530+
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
548531
});
549-
test('should locate tsconfig relative to entry-point via ts-node-script', async () => {
550-
const { err, stdout } = await exec(`${BIN_SCRIPT_PATH} ../a/index`, {
551-
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
552-
});
553-
expect(err).toBe(null);
554-
expect(stdout).toMatch(/plugin-a/);
555-
});
556-
test('should locate tsconfig relative to entry-point with --script-mode', async () => {
557-
const { err, stdout } = await exec(
558-
`${BIN_PATH} --script-mode ../a/index`,
559-
{
560-
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
561-
}
562-
);
563-
expect(err).toBe(null);
564-
expect(stdout).toMatch(/plugin-a/);
532+
expect(err).toBe(null);
533+
expect(stdout).toMatch(/plugin-a/);
534+
});
535+
test('should locate tsconfig relative to entry-point via ts-node-script', async () => {
536+
const { err, stdout } = await exec(`${BIN_SCRIPT_PATH} ../a/index`, {
537+
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
565538
});
566-
test('should locate tsconfig relative to cwd via ts-node-cwd', async () => {
567-
const { err, stdout } = await exec(`${BIN_CWD_PATH} ../a/index`, {
539+
expect(err).toBe(null);
540+
expect(stdout).toMatch(/plugin-a/);
541+
});
542+
test('should locate tsconfig relative to entry-point with --script-mode', async () => {
543+
const { err, stdout } = await exec(
544+
`${BIN_PATH} --script-mode ../a/index`,
545+
{
568546
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
569-
});
570-
expect(err).toBe(null);
571-
expect(stdout).toMatch(/plugin-b/);
547+
}
548+
);
549+
expect(err).toBe(null);
550+
expect(stdout).toMatch(/plugin-a/);
551+
});
552+
test('should locate tsconfig relative to cwd via ts-node-cwd', async () => {
553+
const { err, stdout } = await exec(`${BIN_CWD_PATH} ../a/index`, {
554+
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
572555
});
573-
test('should locate tsconfig relative to cwd in --cwd-mode', async () => {
556+
expect(err).toBe(null);
557+
expect(stdout).toMatch(/plugin-b/);
558+
});
559+
test('should locate tsconfig relative to cwd in --cwd-mode', async () => {
560+
const { err, stdout } = await exec(`${BIN_PATH} --cwd-mode ../a/index`, {
561+
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
562+
});
563+
expect(err).toBe(null);
564+
expect(stdout).toMatch(/plugin-b/);
565+
});
566+
test('should locate tsconfig relative to realpath, not symlink, when entrypoint is a symlink', async (t) => {
567+
if (
568+
lstatSync(
569+
join(TEST_DIR, 'main-realpath/symlink/symlink.tsx')
570+
).isSymbolicLink()
571+
) {
574572
const { err, stdout } = await exec(
575-
`${BIN_PATH} --cwd-mode ../a/index`,
576-
{ cwd: join(TEST_DIR, 'cwd-and-script-mode/b') }
573+
`${BIN_PATH} main-realpath/symlink/symlink.tsx`
577574
);
578575
expect(err).toBe(null);
579-
expect(stdout).toMatch(/plugin-b/);
580-
});
581-
test('should locate tsconfig relative to realpath, not symlink, when entrypoint is a symlink', async (t) => {
582-
if (
583-
lstatSync(
584-
join(TEST_DIR, 'main-realpath/symlink/symlink.tsx')
585-
).isSymbolicLink()
586-
) {
587-
const { err, stdout } = await exec(
588-
`${BIN_PATH} main-realpath/symlink/symlink.tsx`
589-
);
590-
expect(err).toBe(null);
591-
expect(stdout).toBe('');
592-
} else {
593-
t.log('Skipping');
594-
return;
595-
}
596-
});
597-
}
576+
expect(stdout).toBe('');
577+
} else {
578+
t.log('Skipping');
579+
return;
580+
}
581+
});
598582

599583
test.suite('should read ts-node options from tsconfig.json', (test) => {
600584
const BIN_EXEC = `"${BIN_PATH}" --project tsconfig-options/tsconfig.json`;

src/test/register.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { once } from 'lodash';
22
import {
33
contextTsNodeUnderTest,
4-
PROJECT,
4+
PROJECT_TRANSPILE_ONLY,
55
resetNodeEnvironment,
66
TEST_DIR,
77
tsNodeTypes,
@@ -16,7 +16,7 @@ const SOURCE_MAP_REGEXP =
1616
/\/\/# sourceMappingURL=data:application\/json;charset=utf\-8;base64,[\w\+]+=*$/;
1717

1818
const createOptions: tsNodeTypes.CreateOptions = {
19-
project: PROJECT,
19+
project: PROJECT_TRANSPILE_ONLY,
2020
compilerOptions: {
2121
jsx: 'preserve',
2222
},

tests/cwd-and-script-mode/a/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"ts-node": {
3+
"transpileOnly": true
4+
},
25
"compilerOptions": {
36
"plugins": [
47
{

tests/cwd-and-script-mode/b/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"ts-node": {
3+
"transpileOnly": true
4+
},
25
"compilerOptions": {
36
"plugins": [
47
{

tests/esm-import-assertions/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"ts-node": {
3+
"transpileOnly": true
4+
},
25
"compilerOptions": {
36
"module": "ESNext",
47
"target": "ESNext",

tests/import-order/require-compiled.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/main-realpath/target/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"ts-node": {
3+
"transpileOnly": true
4+
},
25
"compilerOptions": {
36
"jsx": "react"
47
}

tests/recursive-fork/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { fork } from 'child_process';
22

3+
// Type syntax to prove its compiled, though the import above should also
4+
// prove the same
5+
const a = null as any;
6+
37
console.log(JSON.stringify({ execArgv: process.execArgv, argv: process.argv }));
48
if (process.env.generation !== 'grandchild') {
59
const nextGeneration =

0 commit comments

Comments
 (0)