|
| 1 | +import { BIN_PATH } from '../helpers/paths'; |
| 2 | +import { createExec } from '../exec-helpers'; |
| 3 | +import { TEST_DIR } from '../helpers/paths'; |
| 4 | +import { context, expect } from '../testlib'; |
| 5 | +import { join, resolve } from 'path'; |
| 6 | +import { tsSupportsExtendsArray } from '../helpers/version-checks'; |
| 7 | +import { ctxTsNode } from '../helpers'; |
| 8 | + |
| 9 | +const test = context(ctxTsNode); |
| 10 | + |
| 11 | +const exec = createExec({ |
| 12 | + cwd: TEST_DIR, |
| 13 | +}); |
| 14 | + |
| 15 | +test.suite('should read ts-node options from tsconfig.json', (test) => { |
| 16 | + const BIN_EXEC = `"${BIN_PATH}" --project tsconfig-options/tsconfig.json`; |
| 17 | + |
| 18 | + test('should override compiler options from env', async () => { |
| 19 | + const r = await exec(`${BIN_EXEC} tsconfig-options/log-options1.js`, { |
| 20 | + env: { |
| 21 | + ...process.env, |
| 22 | + TS_NODE_COMPILER_OPTIONS: '{"typeRoots": ["env-typeroots"]}', |
| 23 | + }, |
| 24 | + }); |
| 25 | + expect(r.err).toBe(null); |
| 26 | + const { config } = JSON.parse(r.stdout); |
| 27 | + expect(config.options.typeRoots).toEqual([ |
| 28 | + join(TEST_DIR, './tsconfig-options/env-typeroots').replace(/\\/g, '/'), |
| 29 | + ]); |
| 30 | + }); |
| 31 | + |
| 32 | + test('should use options from `tsconfig.json`', async () => { |
| 33 | + const r = await exec(`${BIN_EXEC} tsconfig-options/log-options1.js`); |
| 34 | + expect(r.err).toBe(null); |
| 35 | + const { options, config } = JSON.parse(r.stdout); |
| 36 | + expect(config.options.typeRoots).toEqual([ |
| 37 | + join(TEST_DIR, './tsconfig-options/tsconfig-typeroots').replace( |
| 38 | + /\\/g, |
| 39 | + '/' |
| 40 | + ), |
| 41 | + ]); |
| 42 | + expect(config.options.types).toEqual(['tsconfig-tsnode-types']); |
| 43 | + expect(options.pretty).toBe(undefined); |
| 44 | + expect(options.skipIgnore).toBe(false); |
| 45 | + expect(options.transpileOnly).toBe(true); |
| 46 | + expect(options.require).toEqual([ |
| 47 | + join(TEST_DIR, './tsconfig-options/required1.js'), |
| 48 | + ]); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should ignore empty strings in the array options', async () => { |
| 52 | + const r = await exec(`${BIN_EXEC} tsconfig-options/log-options1.js`, { |
| 53 | + env: { |
| 54 | + ...process.env, |
| 55 | + TS_NODE_IGNORE: '', |
| 56 | + }, |
| 57 | + }); |
| 58 | + expect(r.err).toBe(null); |
| 59 | + const { options } = JSON.parse(r.stdout); |
| 60 | + expect(options.ignore).toEqual([]); |
| 61 | + }); |
| 62 | + |
| 63 | + test('should have flags override / merge with `tsconfig.json`', async () => { |
| 64 | + const r = await exec( |
| 65 | + `${BIN_EXEC} --skip-ignore --compiler-options "{\\"types\\":[\\"flags-types\\"]}" --require ./tsconfig-options/required2.js tsconfig-options/log-options2.js` |
| 66 | + ); |
| 67 | + expect(r.err).toBe(null); |
| 68 | + const { options, config } = JSON.parse(r.stdout); |
| 69 | + expect(config.options.typeRoots).toEqual([ |
| 70 | + join(TEST_DIR, './tsconfig-options/tsconfig-typeroots').replace( |
| 71 | + /\\/g, |
| 72 | + '/' |
| 73 | + ), |
| 74 | + ]); |
| 75 | + expect(config.options.types).toEqual(['flags-types']); |
| 76 | + expect(options.pretty).toBe(undefined); |
| 77 | + expect(options.skipIgnore).toBe(true); |
| 78 | + expect(options.transpileOnly).toBe(true); |
| 79 | + expect(options.require).toEqual([ |
| 80 | + join(TEST_DIR, './tsconfig-options/required1.js'), |
| 81 | + './tsconfig-options/required2.js', |
| 82 | + ]); |
| 83 | + }); |
| 84 | + |
| 85 | + test('should have `tsconfig.json` override environment', async () => { |
| 86 | + const r = await exec(`${BIN_EXEC} tsconfig-options/log-options1.js`, { |
| 87 | + env: { |
| 88 | + ...process.env, |
| 89 | + TS_NODE_PRETTY: 'true', |
| 90 | + TS_NODE_SKIP_IGNORE: 'true', |
| 91 | + }, |
| 92 | + }); |
| 93 | + expect(r.err).toBe(null); |
| 94 | + const { options, config } = JSON.parse(r.stdout); |
| 95 | + expect(config.options.typeRoots).toEqual([ |
| 96 | + join(TEST_DIR, './tsconfig-options/tsconfig-typeroots').replace( |
| 97 | + /\\/g, |
| 98 | + '/' |
| 99 | + ), |
| 100 | + ]); |
| 101 | + expect(config.options.types).toEqual(['tsconfig-tsnode-types']); |
| 102 | + expect(options.pretty).toBe(true); |
| 103 | + expect(options.skipIgnore).toBe(false); |
| 104 | + expect(options.transpileOnly).toBe(true); |
| 105 | + expect(options.require).toEqual([ |
| 106 | + join(TEST_DIR, './tsconfig-options/required1.js'), |
| 107 | + ]); |
| 108 | + }); |
| 109 | + |
| 110 | + test('should pull ts-node options from extended `tsconfig.json`', async () => { |
| 111 | + const r = await exec( |
| 112 | + `${BIN_PATH} --show-config --project ./tsconfig-extends/tsconfig.json` |
| 113 | + ); |
| 114 | + expect(r.err).toBe(null); |
| 115 | + const config = JSON.parse(r.stdout); |
| 116 | + expect(config['ts-node'].require).toEqual([ |
| 117 | + resolve(TEST_DIR, 'tsconfig-extends/other/require-hook.js'), |
| 118 | + ]); |
| 119 | + expect(config['ts-node'].scopeDir).toBe( |
| 120 | + resolve(TEST_DIR, 'tsconfig-extends/other/scopedir') |
| 121 | + ); |
| 122 | + expect(config['ts-node'].preferTsExts).toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + test.suite( |
| 126 | + 'should pull ts-node options from extended `tsconfig.json`', |
| 127 | + (test) => { |
| 128 | + test.if(tsSupportsExtendsArray); |
| 129 | + test('test', async () => { |
| 130 | + const r = await exec( |
| 131 | + `${BIN_PATH} --show-config --project ./tsconfig-extends-multiple/tsconfig.json` |
| 132 | + ); |
| 133 | + expect(r.err).toBe(null); |
| 134 | + const config = JSON.parse(r.stdout); |
| 135 | + |
| 136 | + // root tsconfig extends [a, c] |
| 137 | + // a extends b |
| 138 | + // c extends d |
| 139 | + |
| 140 | + // https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#supporting-multiple-configuration-files-in-extends |
| 141 | + // If any fields "conflict", the latter entry wins. |
| 142 | + |
| 143 | + // This value comes from c |
| 144 | + expect(config.compilerOptions.target).toBe('es2017'); |
| 145 | + |
| 146 | + // From root |
| 147 | + expect(config['ts-node'].preferTsExts).toBe(true); |
| 148 | + |
| 149 | + // From a |
| 150 | + expect(config['ts-node'].require).toEqual([ |
| 151 | + resolve( |
| 152 | + TEST_DIR, |
| 153 | + 'tsconfig-extends-multiple/a/require-hook-from-a.js' |
| 154 | + ), |
| 155 | + ]); |
| 156 | + |
| 157 | + // From a, overrides declaration in b |
| 158 | + expect(config['ts-node'].scopeDir).toBe( |
| 159 | + resolve(TEST_DIR, 'tsconfig-extends-multiple/a/scopedir-from-a') |
| 160 | + ); |
| 161 | + |
| 162 | + // From b |
| 163 | + const key = |
| 164 | + process.platform === 'win32' |
| 165 | + ? 'b\\module-types-from-b' |
| 166 | + : 'b/module-types-from-b'; |
| 167 | + expect(config['ts-node'].moduleTypes).toStrictEqual({ |
| 168 | + [key]: 'cjs', |
| 169 | + }); |
| 170 | + |
| 171 | + // From c, overrides declaration in b |
| 172 | + expect(config['ts-node'].transpiler).toBe('transpiler-from-c'); |
| 173 | + |
| 174 | + // From d, inherited by c, overrides value from b |
| 175 | + expect(config['ts-node'].ignore).toStrictEqual([ |
| 176 | + 'ignore-pattern-from-d', |
| 177 | + ]); |
| 178 | + }); |
| 179 | + } |
| 180 | + ); |
| 181 | +}); |
0 commit comments