/
index.spec.ts
1311 lines (1223 loc) · 46.4 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { _test } from './testlib';
import * as expect from 'expect';
import { join, resolve, sep as pathSep } from 'path';
import { tmpdir } from 'os';
import semver = require('semver');
import { ts } from './helpers';
import { lstatSync, mkdtempSync } from 'fs';
import { npath } from '@yarnpkg/fslib';
import type _createRequire from 'create-require';
import { pathToFileURL } from 'url';
import { createExec } from './exec-helpers';
import {
BIN_CWD_PATH,
BIN_PATH,
BIN_SCRIPT_PATH,
DIST_DIR,
ROOT_DIR,
TEST_DIR,
testsDirRequire,
tsNodeTypes,
xfs,
contextTsNodeUnderTest,
CMD_TS_NODE_WITH_PROJECT_FLAG,
CMD_TS_NODE_WITHOUT_PROJECT_FLAG,
CMD_ESM_LOADER_WITHOUT_PROJECT,
EXPERIMENTAL_MODULES_FLAG,
} from './helpers';
const exec = createExec({
cwd: TEST_DIR,
});
const test = _test.context(contextTsNodeUnderTest);
test.suite('ts-node', (test) => {
test('should export the correct version', (t) => {
expect(t.context.tsNodeUnderTest.VERSION).toBe(
require('../../package.json').version
);
});
test('should export all CJS entrypoints', () => {
// Ensure our package.json "exports" declaration allows `require()`ing all our entrypoints
// https://github.com/TypeStrong/ts-node/pull/1026
testsDirRequire.resolve('ts-node');
// only reliably way to ask node for the root path of a dependency is Path.resolve(require.resolve('ts-node/package'), '..')
testsDirRequire.resolve('ts-node/package');
testsDirRequire.resolve('ts-node/package.json');
// All bin entrypoints for people who need to augment our CLI: `node -r otherstuff ./node_modules/ts-node/dist/bin`
testsDirRequire.resolve('ts-node/dist/bin');
testsDirRequire.resolve('ts-node/dist/bin.js');
testsDirRequire.resolve('ts-node/dist/bin-transpile');
testsDirRequire.resolve('ts-node/dist/bin-transpile.js');
testsDirRequire.resolve('ts-node/dist/bin-script');
testsDirRequire.resolve('ts-node/dist/bin-script.js');
testsDirRequire.resolve('ts-node/dist/bin-cwd');
testsDirRequire.resolve('ts-node/dist/bin-cwd.js');
// Must be `require()`able obviously
testsDirRequire.resolve('ts-node/register');
testsDirRequire.resolve('ts-node/register/files');
testsDirRequire.resolve('ts-node/register/transpile-only');
testsDirRequire.resolve('ts-node/register/type-check');
// `node --loader ts-node/esm`
testsDirRequire.resolve('ts-node/esm');
testsDirRequire.resolve('ts-node/esm.mjs');
testsDirRequire.resolve('ts-node/esm/transpile-only');
testsDirRequire.resolve('ts-node/esm/transpile-only.mjs');
testsDirRequire.resolve('ts-node/transpilers/swc');
testsDirRequire.resolve('ts-node/transpilers/swc-experimental');
testsDirRequire.resolve('ts-node/node10/tsconfig.json');
testsDirRequire.resolve('ts-node/node12/tsconfig.json');
testsDirRequire.resolve('ts-node/node14/tsconfig.json');
testsDirRequire.resolve('ts-node/node16/tsconfig.json');
});
test('should not load typescript outside of loadConfig', async () => {
const { err, stdout } = await exec(
`node -e "require('ts-node'); console.dir(Object.keys(require.cache).filter(k => k.includes('node_modules/typescript')).length)"`
);
expect(err).toBe(null);
expect(stdout).toBe('0\n');
});
test.suite('cli', (test) => {
test('should execute cli', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} hello-world`
);
expect(err).toBe(null);
expect(stdout).toBe('Hello, world!\n');
});
test('shows usage via --help', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} --help`
);
expect(err).toBe(null);
expect(stdout).toMatch(/Usage: ts-node /);
});
test('shows version via -v', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} -v`
);
expect(err).toBe(null);
expect(stdout.trim()).toBe(
'v' + testsDirRequire('ts-node/package').version
);
});
test('shows version of compiler via -vv', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} -vv`
);
expect(err).toBe(null);
expect(stdout.trim()).toBe(
`ts-node v${testsDirRequire('ts-node/package').version}\n` +
`node ${process.version}\n` +
`compiler v${testsDirRequire('typescript/package').version}`
);
});
test('should register via cli', async () => {
const { err, stdout } = await exec(
`node -r ts-node/register hello-world.ts`,
{
cwd: TEST_DIR,
}
);
expect(err).toBe(null);
expect(stdout).toBe('Hello, world!\n');
});
test('should execute cli with absolute path', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} "${join(TEST_DIR, 'hello-world')}"`
);
expect(err).toBe(null);
expect(stdout).toBe('Hello, world!\n');
});
test('should print scripts', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -pe "import { example } from './complex/index';example()"`
);
expect(err).toBe(null);
expect(stdout).toBe('example\n');
});
test('should provide registered information globally', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} env`
);
expect(err).toBe(null);
expect(stdout).toBe('object\n');
});
test('should provide registered information on register', async () => {
const { err, stdout } = await exec(`node -r ts-node/register env.ts`, {
cwd: TEST_DIR,
});
expect(err).toBe(null);
expect(stdout).toBe('object\n');
});
if (semver.gte(ts.version, '1.8.0')) {
test('should allow js', async () => {
const { err, stdout } = await exec(
[
CMD_TS_NODE_WITH_PROJECT_FLAG,
'-O "{\\"allowJs\\":true}"',
'-pe "import { main } from \'./allow-js/run\';main()"',
].join(' ')
);
expect(err).toBe(null);
expect(stdout).toBe('hello world\n');
});
test('should include jsx when `allow-js` true', async () => {
const { err, stdout } = await exec(
[
CMD_TS_NODE_WITH_PROJECT_FLAG,
'-O "{\\"allowJs\\":true}"',
'-pe "import { Foo2 } from \'./allow-js/with-jsx\'; Foo2.sayHi()"',
].join(' ')
);
expect(err).toBe(null);
expect(stdout).toBe('hello world\n');
});
}
test('should eval code', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -e "import * as m from './module';console.log(m.example('test'))"`
);
expect(err).toBe(null);
expect(stdout).toBe('TEST\n');
});
test('should import empty files', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -e "import './empty'"`
);
expect(err).toBe(null);
expect(stdout).toBe('');
});
test('should throw errors', async () => {
const { err } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -e "import * as m from './module';console.log(m.example(123))"`
);
if (err === null) {
throw new Error('Command was expected to fail, but it succeeded.');
}
expect(err.message).toMatch(
new RegExp(
"TS2345: Argument of type '(?:number|123)' " +
"is not assignable to parameter of type 'string'\\."
)
);
});
test('should be able to ignore diagnostic', async () => {
const { err } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --ignore-diagnostics 2345 -e "import * as m from './module';console.log(m.example(123))"`
);
if (err === null) {
throw new Error('Command was expected to fail, but it succeeded.');
}
expect(err.message).toMatch(
/TypeError: (?:(?:undefined|foo\.toUpperCase) is not a function|.*has no method \'toUpperCase\')/
);
});
test('should work with source maps', async () => {
const { err } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} "throw error"`
);
if (err === null) {
throw new Error('Command was expected to fail, but it succeeded.');
}
expect(err.message).toMatch(
[
`${join(TEST_DIR, 'throw error.ts')}:100`,
" bar() { throw new Error('this is a demo'); }",
' ^',
'Error: this is a demo',
].join('\n')
);
});
test('should work with source maps in --transpile-only mode', async () => {
const { err } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --transpile-only "throw error"`
);
if (err === null) {
throw new Error('Command was expected to fail, but it succeeded.');
}
expect(err.message).toMatch(
[
`${join(TEST_DIR, 'throw error.ts')}:100`,
" bar() { throw new Error('this is a demo'); }",
' ^',
'Error: this is a demo',
].join('\n')
);
});
test('eval should work with source maps', async () => {
const { err } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -pe "import './throw error'"`
);
if (err === null) {
throw new Error('Command was expected to fail, but it succeeded.');
}
expect(err.message).toMatch(
[
`${join(TEST_DIR, 'throw error.ts')}:100`,
" bar() { throw new Error('this is a demo'); }",
' ^',
].join('\n')
);
});
test('should support transpile only mode', async () => {
const { err } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --transpile-only -pe "x"`
);
if (err === null) {
throw new Error('Command was expected to fail, but it succeeded.');
}
expect(err.message).toMatch('ReferenceError: x is not defined');
});
test('should throw error even in transpileOnly mode', async () => {
const { err } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --transpile-only -pe "console."`
);
if (err === null) {
throw new Error('Command was expected to fail, but it succeeded.');
}
expect(err.message).toMatch('error TS1003: Identifier expected');
});
for (const flavor of [
'--transpiler ts-node/transpilers/swc transpile-only-swc',
'--transpiler ts-node/transpilers/swc-experimental transpile-only-swc',
'--swc transpile-only-swc',
'transpile-only-swc-via-tsconfig',
'transpile-only-swc-shorthand-via-tsconfig',
]) {
test(`should support swc and third-party transpilers: ${flavor}`, async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} ${flavor}`,
{
env: {
...process.env,
NODE_OPTIONS: `${
process.env.NODE_OPTIONS || ''
} --require ${require.resolve('../../tests/spy-swc-transpiler')}`,
},
}
);
expect(err).toBe(null);
expect(stdout).toMatch(
'Hello World! swc transpiler invocation count: 1\n'
);
});
}
test.suite('should support `traceResolution` compiler option', (test) => {
test('prints traces before running code when enabled', async () => {
const { err, stdout } = await exec(
`${BIN_PATH} --compiler-options="{ \\"traceResolution\\": true }" -e "console.log('ok')"`
);
expect(err).toBeNull();
expect(stdout).toContain('======== Resolving module');
expect(stdout.endsWith('ok\n')).toBe(true);
});
test('does NOT print traces when not enabled', async () => {
const { err, stdout } = await exec(
`${BIN_PATH} -e "console.log('ok')"`
);
expect(err).toBeNull();
expect(stdout).not.toContain('======== Resolving module');
expect(stdout.endsWith('ok\n')).toBe(true);
});
});
if (semver.gte(process.version, '12.16.0')) {
test('swc transpiler supports native ESM emit', async () => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./index.ts`,
{
cwd: resolve(TEST_DIR, 'transpile-only-swc-native-esm'),
}
);
expect(err).toBe(null);
expect(stdout).toMatch('Hello file://');
});
}
test('should pipe into `ts-node` and evaluate', async () => {
const execPromise = exec(CMD_TS_NODE_WITH_PROJECT_FLAG);
execPromise.child.stdin!.end("console.log('hello')");
const { err, stdout } = await execPromise;
expect(err).toBe(null);
expect(stdout).toBe('hello\n');
});
test('should pipe into `ts-node`', async () => {
const execPromise = exec(`${CMD_TS_NODE_WITH_PROJECT_FLAG} -p`);
execPromise.child.stdin!.end('true');
const { err, stdout } = await execPromise;
expect(err).toBe(null);
expect(stdout).toBe('true\n');
});
test('should pipe into an eval script', async () => {
const execPromise = exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --transpile-only -pe "process.stdin.isTTY"`
);
execPromise.child.stdin!.end('true');
const { err, stdout } = await execPromise;
expect(err).toBe(null);
expect(stdout).toBe('undefined\n');
});
test('should support require flags', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -r ./hello-world -pe "console.log('success')"`
);
expect(err).toBe(null);
expect(stdout).toBe('Hello, world!\nsuccess\nundefined\n');
});
test('should support require from node modules', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} -r typescript -e "console.log('success')"`
);
expect(err).toBe(null);
expect(stdout).toBe('success\n');
});
test('should use source maps with react tsx', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} "throw error react tsx.tsx"`
);
expect(err).not.toBe(null);
expect(err!.message).toMatch(
[
`${join(TEST_DIR, './throw error react tsx.tsx')}:100`,
" bar() { throw new Error('this is a demo'); }",
' ^',
'Error: this is a demo',
].join('\n')
);
});
test('should use source maps with react tsx in --transpile-only mode', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --transpile-only "throw error react tsx.tsx"`
);
expect(err).not.toBe(null);
expect(err!.message).toMatch(
[
`${join(TEST_DIR, './throw error react tsx.tsx')}:100`,
" bar() { throw new Error('this is a demo'); }",
' ^',
'Error: this is a demo',
].join('\n')
);
});
test('should allow custom typings', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} custom-types`
);
// This error comes from *node*, meaning TypeScript respected the custom types (good) but *node* could not find the non-existent module (expected)
expect(err?.message).toMatch(
/Error: Cannot find module 'does-not-exist'/
);
});
test('should preserve `ts-node` context with child process', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} child-process`
);
expect(err).toBe(null);
expect(stdout).toBe('Hello, world!\n');
});
test('should import js before ts by default', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} import-order/compiled`
);
expect(err).toBe(null);
expect(stdout).toBe('Hello, JavaScript!\n');
});
const preferTsExtsEntrypoint = semver.gte(process.version, '12.0.0')
? 'import-order/compiled'
: 'import-order/require-compiled';
test('should import ts before js when --prefer-ts-exts flag is present', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --prefer-ts-exts ${preferTsExtsEntrypoint}`
);
expect(err).toBe(null);
expect(stdout).toBe('Hello, TypeScript!\n');
});
test('should import ts before js when TS_NODE_PREFER_TS_EXTS env is present', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} ${preferTsExtsEntrypoint}`,
{
env: { ...process.env, TS_NODE_PREFER_TS_EXTS: 'true' },
}
);
expect(err).toBe(null);
expect(stdout).toBe('Hello, TypeScript!\n');
});
test('should ignore .d.ts files', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} import-order/importer`
);
expect(err).toBe(null);
expect(stdout).toBe('Hello, World!\n');
});
test.suite('issue #884', (test) => {
test('should compile', async (t) => {
// TODO disabled because it consistently fails on Windows on TS 2.7
if (
process.platform === 'win32' &&
semver.satisfies(ts.version, '2.7')
) {
t.log('Skipping');
return;
} else {
const { err, stdout } = await exec(
`"${BIN_PATH}" --project issue-884/tsconfig.json issue-884`
);
expect(err).toBe(null);
expect(stdout).toBe('');
}
});
});
test.suite('issue #986', (test) => {
test('should not compile', async () => {
const { err, stdout, stderr } = await exec(
`"${BIN_PATH}" --project issue-986/tsconfig.json issue-986`
);
expect(err).not.toBe(null);
expect(stderr).toMatch("Cannot find name 'TEST'"); // TypeScript error.
expect(stdout).toBe('');
});
test('should compile with `--files`', async () => {
const { err, stdout, stderr } = await exec(
`"${BIN_PATH}" --files --project issue-986/tsconfig.json issue-986`
);
expect(err).not.toBe(null);
expect(stderr).toMatch('ReferenceError: TEST is not defined'); // Runtime error.
expect(stdout).toBe('');
});
});
if (semver.gte(ts.version, '2.7.0')) {
test('should locate tsconfig relative to entry-point by default', async () => {
const { err, stdout } = await exec(`${BIN_PATH} ../a/index`, {
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
});
expect(err).toBe(null);
expect(stdout).toMatch(/plugin-a/);
});
test('should locate tsconfig relative to entry-point via ts-node-script', async () => {
const { err, stdout } = await exec(`${BIN_SCRIPT_PATH} ../a/index`, {
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
});
expect(err).toBe(null);
expect(stdout).toMatch(/plugin-a/);
});
test('should locate tsconfig relative to entry-point with --script-mode', async () => {
const { err, stdout } = await exec(
`${BIN_PATH} --script-mode ../a/index`,
{
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
}
);
expect(err).toBe(null);
expect(stdout).toMatch(/plugin-a/);
});
test('should locate tsconfig relative to cwd via ts-node-cwd', async () => {
const { err, stdout } = await exec(`${BIN_CWD_PATH} ../a/index`, {
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
});
expect(err).toBe(null);
expect(stdout).toMatch(/plugin-b/);
});
test('should locate tsconfig relative to cwd in --cwd-mode', async () => {
const { err, stdout } = await exec(
`${BIN_PATH} --cwd-mode ../a/index`,
{ cwd: join(TEST_DIR, 'cwd-and-script-mode/b') }
);
expect(err).toBe(null);
expect(stdout).toMatch(/plugin-b/);
});
test('should locate tsconfig relative to realpath, not symlink, when entrypoint is a symlink', async (t) => {
if (
lstatSync(
join(TEST_DIR, 'main-realpath/symlink/symlink.tsx')
).isSymbolicLink()
) {
const { err, stdout } = await exec(
`${BIN_PATH} main-realpath/symlink/symlink.tsx`
);
expect(err).toBe(null);
expect(stdout).toBe('');
} else {
t.log('Skipping');
return;
}
});
}
test.suite('should read ts-node options from tsconfig.json', (test) => {
const BIN_EXEC = `"${BIN_PATH}" --project tsconfig-options/tsconfig.json`;
test('should override compiler options from env', async () => {
const { err, stdout } = await exec(
`${BIN_EXEC} tsconfig-options/log-options1.js`,
{
env: {
...process.env,
TS_NODE_COMPILER_OPTIONS: '{"typeRoots": ["env-typeroots"]}',
},
}
);
expect(err).toBe(null);
const { config } = JSON.parse(stdout);
expect(config.options.typeRoots).toEqual([
join(TEST_DIR, './tsconfig-options/env-typeroots').replace(
/\\/g,
'/'
),
]);
});
test('should use options from `tsconfig.json`', async () => {
const { err, stdout } = await exec(
`${BIN_EXEC} tsconfig-options/log-options1.js`
);
expect(err).toBe(null);
const { options, config } = JSON.parse(stdout);
expect(config.options.typeRoots).toEqual([
join(TEST_DIR, './tsconfig-options/tsconfig-typeroots').replace(
/\\/g,
'/'
),
]);
expect(config.options.types).toEqual(['tsconfig-tsnode-types']);
expect(options.pretty).toBe(undefined);
expect(options.skipIgnore).toBe(false);
expect(options.transpileOnly).toBe(true);
expect(options.require).toEqual([
join(TEST_DIR, './tsconfig-options/required1.js'),
]);
});
test('should ignore empty strings in the array options', async () => {
const { err, stdout } = await exec(
`${BIN_EXEC} tsconfig-options/log-options1.js`,
{
env: {
...process.env,
TS_NODE_IGNORE: '',
},
}
);
expect(err).toBe(null);
const { options } = JSON.parse(stdout);
expect(options.ignore).toEqual([]);
});
test('should have flags override / merge with `tsconfig.json`', async () => {
const { err, stdout } = await exec(
`${BIN_EXEC} --skip-ignore --compiler-options "{\\"types\\":[\\"flags-types\\"]}" --require ./tsconfig-options/required2.js tsconfig-options/log-options2.js`
);
expect(err).toBe(null);
const { options, config } = JSON.parse(stdout);
expect(config.options.typeRoots).toEqual([
join(TEST_DIR, './tsconfig-options/tsconfig-typeroots').replace(
/\\/g,
'/'
),
]);
expect(config.options.types).toEqual(['flags-types']);
expect(options.pretty).toBe(undefined);
expect(options.skipIgnore).toBe(true);
expect(options.transpileOnly).toBe(true);
expect(options.require).toEqual([
join(TEST_DIR, './tsconfig-options/required1.js'),
'./tsconfig-options/required2.js',
]);
});
test('should have `tsconfig.json` override environment', async () => {
const { err, stdout } = await exec(
`${BIN_EXEC} tsconfig-options/log-options1.js`,
{
env: {
...process.env,
TS_NODE_PRETTY: 'true',
TS_NODE_SKIP_IGNORE: 'true',
},
}
);
expect(err).toBe(null);
const { options, config } = JSON.parse(stdout);
expect(config.options.typeRoots).toEqual([
join(TEST_DIR, './tsconfig-options/tsconfig-typeroots').replace(
/\\/g,
'/'
),
]);
expect(config.options.types).toEqual(['tsconfig-tsnode-types']);
expect(options.pretty).toBe(true);
expect(options.skipIgnore).toBe(false);
expect(options.transpileOnly).toBe(true);
expect(options.require).toEqual([
join(TEST_DIR, './tsconfig-options/required1.js'),
]);
});
if (semver.gte(ts.version, '3.2.0')) {
test('should pull ts-node options from extended `tsconfig.json`', async () => {
const { err, stdout } = await exec(
`${BIN_PATH} --show-config --project ./tsconfig-extends/tsconfig.json`
);
expect(err).toBe(null);
const config = JSON.parse(stdout);
expect(config['ts-node'].require).toEqual([
resolve(TEST_DIR, 'tsconfig-extends/other/require-hook.js'),
]);
expect(config['ts-node'].scopeDir).toBe(
resolve(TEST_DIR, 'tsconfig-extends/other/scopedir')
);
expect(config['ts-node'].preferTsExts).toBe(true);
});
}
});
test.suite(
'should use implicit @tsconfig/bases config when one is not loaded from disk',
(_test) => {
const test = _test.context(async (t) => ({
tempDir: mkdtempSync(join(tmpdir(), 'ts-node-spec')),
}));
if (
semver.gte(ts.version, '3.5.0') &&
semver.gte(process.versions.node, '14.0.0')
) {
const libAndTarget = semver.gte(process.versions.node, '16.0.0')
? 'es2021'
: 'es2020';
test('implicitly uses @tsconfig/node14 or @tsconfig/node16 compilerOptions when both TS and node versions support it', async (t) => {
// node14 and node16 configs are identical, hence the "or"
const {
context: { tempDir },
} = t;
const {
err: err1,
stdout: stdout1,
stderr: stderr1,
} = await exec(`${BIN_PATH} --showConfig`, { cwd: tempDir });
expect(err1).toBe(null);
t.like(JSON.parse(stdout1), {
compilerOptions: {
target: libAndTarget,
lib: [libAndTarget],
},
});
const {
err: err2,
stdout: stdout2,
stderr: stderr2,
} = await exec(`${BIN_PATH} -pe 10n`, { cwd: tempDir });
expect(err2).toBe(null);
expect(stdout2).toBe('10n\n');
});
} else {
test('implicitly uses @tsconfig/* lower than node14 (node12) when either TS or node versions do not support @tsconfig/node14', async ({
context: { tempDir },
}) => {
const { err, stdout, stderr } = await exec(`${BIN_PATH} -pe 10n`, {
cwd: tempDir,
});
expect(err).not.toBe(null);
expect(stderr).toMatch(
/BigInt literals are not available when targeting lower than|error TS2304: Cannot find name 'n'/
);
});
}
test('implicitly loads @types/node even when not installed within local directory', async ({
context: { tempDir },
}) => {
const { err, stdout, stderr } = await exec(
`${BIN_PATH} -pe process.env.foo`,
{
cwd: tempDir,
env: { ...process.env, foo: 'hello world' },
}
);
expect(err).toBe(null);
expect(stdout).toBe('hello world\n');
});
test('implicitly loads local @types/node', async ({
context: { tempDir },
}) => {
await xfs.copyPromise(
npath.toPortablePath(tempDir),
npath.toPortablePath(join(TEST_DIR, 'local-types-node'))
);
const { err, stdout, stderr } = await exec(
`${BIN_PATH} -pe process.env.foo`,
{
cwd: tempDir,
env: { ...process.env, foo: 'hello world' },
}
);
expect(err).not.toBe(null);
expect(stderr).toMatch(
"Property 'env' does not exist on type 'LocalNodeTypes_Process'"
);
});
}
);
if (semver.gte(ts.version, '3.2.0')) {
test.suite(
'should bundle @tsconfig/bases to be used in your own tsconfigs',
(test) => {
const macro = test.macro((nodeVersion: string) => async (t) => {
const config = require(`@tsconfig/${nodeVersion}/tsconfig.json`);
const { err, stdout, stderr } = await exec(
`${BIN_PATH} --showConfig -e 10n`,
{
cwd: join(TEST_DIR, 'tsconfig-bases', nodeVersion),
}
);
expect(err).toBe(null);
t.like(JSON.parse(stdout), {
compilerOptions: {
target: config.compilerOptions.target,
lib: config.compilerOptions.lib,
},
});
});
test(`ts-node/node10/tsconfig.json`, macro, 'node10');
test(`ts-node/node12/tsconfig.json`, macro, 'node12');
test(`ts-node/node14/tsconfig.json`, macro, 'node14');
test(`ts-node/node16/tsconfig.json`, macro, 'node16');
}
);
}
test.suite('compiler host', (test) => {
test('should execute cli', async () => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --compiler-host hello-world`
);
expect(err).toBe(null);
expect(stdout).toBe('Hello, world!\n');
});
});
test('should transpile files inside a node_modules directory when not ignored', async () => {
const { err, stdout, stderr } = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} from-node-modules/from-node-modules`
);
if (err)
throw new Error(
`Unexpected error: ${err}\nstdout:\n${stdout}\nstderr:\n${stderr}`
);
expect(JSON.parse(stdout)).toEqual({
external: {
tsmri: { name: 'typescript-module-required-internally' },
jsmri: { name: 'javascript-module-required-internally' },
tsmii: { name: 'typescript-module-imported-internally' },
jsmii: { name: 'javascript-module-imported-internally' },
},
tsmie: { name: 'typescript-module-imported-externally' },
jsmie: { name: 'javascript-module-imported-externally' },
tsmre: { name: 'typescript-module-required-externally' },
jsmre: { name: 'javascript-module-required-externally' },
});
});
test.suite('should respect maxNodeModulesJsDepth', (test) => {
test('for unscoped modules', async () => {
const { err, stdout, stderr } = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} maxnodemodulesjsdepth`
);
expect(err).not.toBe(null);
expect(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'
);
});
test('for @scoped modules', async () => {
const { err, stdout, stderr } = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} maxnodemodulesjsdepth-scoped`
);
expect(err).not.toBe(null);
expect(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'
);
});
});
if (semver.gte(ts.version, '3.2.0')) {
test('--showConfig should log resolved configuration', async (t) => {
function native(path: string) {
return path.replace(/\/|\\/g, pathSep);
}
function posix(path: string) {
return path.replace(/\/|\\/g, '/');
}
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --showConfig`
);
expect(err).toBe(null);
t.is(
stdout,
JSON.stringify(
{
'ts-node': {
cwd: native(`${ROOT_DIR}/tests`),
projectSearchDir: native(`${ROOT_DIR}/tests`),
project: native(`${ROOT_DIR}/tests/tsconfig.json`),
},
compilerOptions: {
target: 'es6',
jsx: 'react',
noEmit: false,
strict: true,
typeRoots: [
posix(`${ROOT_DIR}/tests/typings`),
posix(`${ROOT_DIR}/node_modules/@types`),
],
sourceMap: true,
inlineSourceMap: false,
inlineSources: true,
declaration: false,
outDir: './.ts-node',
module: 'commonjs',
},
},
null,
2
) + '\n'
);
});
} else {
test('--show-config should log error message when used with old typescript versions', async (t) => {
const { err, stderr } = await exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --showConfig`
);
expect(err).not.toBe(null);
expect(stderr).toMatch('Error: --showConfig requires');
});
}
test('should support compiler scope specified via tsconfig.json', async (t) => {
const { err, stderr, stdout } = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} --project ./scope/c/config/tsconfig.json ./scope/c/index.js`
);
expect(err).toBe(null);
expect(stdout).toBe(`value\nFailures: 0\n`);
});
});
test.suite('create', (_test) => {
const test = _test.context(async (t) => {
return {
service: t.context.tsNodeUnderTest.create({
compilerOptions: { target: 'es5' },
skipProject: true,
}),
};
});
test('should create generic compiler instances', ({
context: { service },
}) => {
const output = service.compile('const x = 10', 'test.ts');
expect(output).toMatch('var x = 10;');
});
test.suite('should get type information', (test) => {
test('given position of identifier', ({ context: { service } }) => {
expect(
service.getTypeInfo('/**jsdoc here*/const x = 10', 'test.ts', 21)
).toEqual({
comment: 'jsdoc here',
name: 'const x: 10',
});
});
test('given position that does not point to an identifier', ({
context: { service },
}) => {
expect(
service.getTypeInfo('/**jsdoc here*/const x = 10', 'test.ts', 0)
).toEqual({
comment: '',
name: '',
});
});
});
});
test.suite('issue #1098', (test) => {