Skip to content

Commit a89f0e2

Browse files
authoredMar 17, 2021
fix(compiler): initialize compiler with .ts/.tsx/.d.ts files only (#2458)
Closes #2445
1 parent 0b9abe0 commit a89f0e2

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed
 

‎src/__mocks__/thing.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
// @ts-expect-error testing purpose
2+
import babelFooCfg from './babel-foo.config'
13
import { getFoo } from './thing1'
24
import { getFooBar } from './thing1'
35
import { getBar } from './thing2'
46

57
getFoo('foo')
68
getBar('bar')
79
getFooBar('foobar')
10+
getFoo(JSON.stringify(babelFooCfg.presets))

‎src/compiler/__snapshots__/ts-compiler.spec.ts.snap

+26-2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,33 @@ const App = () => {
3636
`;
3737

3838
exports[`TsCompiler isolatedModule false should compile codes with useESM true 1`] = `
39-
"import { getFoo } from './thing1';
39+
"// @ts-expect-error testing purpose
40+
import babelFooCfg from './babel-foo.config';
41+
import { getFoo } from './thing1';
4042
import { getFooBar } from './thing1';
4143
import { getBar } from './thing2';
4244
getFoo('foo');
4345
getBar('bar');
4446
getFooBar('foobar');
47+
getFoo(JSON.stringify(babelFooCfg.presets));
48+
//# "
49+
`;
50+
51+
exports[`TsCompiler isolatedModule false should compile ts file which has an existing js file 1`] = `
52+
"\\"use strict\\";
53+
var __importDefault = (this && this.__importDefault) || function (mod) {
54+
return (mod && mod.__esModule) ? mod : { \\"default\\": mod };
55+
};
56+
Object.defineProperty(exports, \\"__esModule\\", { value: true });
57+
// @ts-expect-error testing purpose
58+
const babel_foo_config_1 = __importDefault(require(\\"./babel-foo.config\\"));
59+
const thing1_1 = require(\\"./thing1\\");
60+
const thing1_2 = require(\\"./thing1\\");
61+
const thing2_1 = require(\\"./thing2\\");
62+
thing1_1.getFoo('foo');
63+
thing2_1.getBar('bar');
64+
thing1_2.getFooBar('foobar');
65+
thing1_1.getFoo(JSON.stringify(babel_foo_config_1.default.presets));
4566
//# "
4667
`;
4768

@@ -73,11 +94,14 @@ exports.default = 42;
7394
`;
7495

7596
exports[`TsCompiler isolatedModule true should transpile code with useESM true 1`] = `
76-
"import { getFoo } from './thing1';
97+
"// @ts-expect-error testing purpose
98+
import babelFooCfg from './babel-foo.config';
99+
import { getFoo } from './thing1';
77100
import { getFooBar } from './thing1';
78101
import { getBar } from './thing2';
79102
getFoo('foo');
80103
getBar('bar');
81104
getFooBar('foobar');
105+
getFoo(JSON.stringify(babelFooCfg.presets));
82106
//# "
83107
`;

‎src/compiler/ts-compiler.spec.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { readFileSync } from 'fs'
22
import { join, normalize } from 'path'
33

4-
import { makeCompiler } from '../__helpers__/fakers'
4+
import { createConfigSet, makeCompiler } from '../__helpers__/fakers'
55
import { logTargetMock } from '../__helpers__/mocks'
66
import { mockFolder } from '../__helpers__/path'
77
import ProcessedSource from '../__helpers__/processed-source'
88

9+
import { TsCompiler } from './ts-compiler'
10+
911
const logTarget = logTargetMock()
1012

1113
describe('TsCompiler', () => {
@@ -236,6 +238,20 @@ const t: string = f(5)
236238
expect(compiler._initialCompilerOptions.allowSyntheticDefaultImports).not.toEqual(true)
237239
})
238240

241+
test('should compile ts file which has an existing js file', () => {
242+
const configSet = createConfigSet({
243+
tsJestConfig: baseTsJestConfig,
244+
})
245+
const fileName = join(mockFolder, 'thing.ts')
246+
const fileContent = readFileSync(fileName, 'utf-8')
247+
configSet.parsedTsConfig.fileNames.push(...[fileName.replace('.ts', '.js'), fileName])
248+
const compiler = new TsCompiler(configSet, new Map())
249+
250+
const compiledOutput = compiler.getCompiledOutput(fileContent, fileName, false)
251+
252+
expect(new ProcessedSource(compiledOutput, fileName).outputCodeWithoutMaps).toMatchSnapshot()
253+
})
254+
239255
describe('allowJs option', () => {
240256
const fileName = 'test-allow-js.js'
241257
const source = 'export default 42'
@@ -407,6 +423,7 @@ const t: string = f(5)
407423
const jestCacheFS = new Map<string, string>()
408424
const importedModule1 = join(mockFolder, 'thing1.ts')
409425
const importedModule2 = join(mockFolder, 'thing2.ts')
426+
const importedModule3 = join(mockFolder, 'babel-foo.config.js')
410427
const fileContentWithModules = readFileSync(fileName, 'utf-8')
411428
jestCacheFS.set(importedModule1, readFileSync(importedModule1, 'utf-8'))
412429
const compiler = makeCompiler(
@@ -420,7 +437,7 @@ const t: string = f(5)
420437
compiler
421438
.getResolvedModules(fileContentWithModules, fileName, new Map())
422439
.map((resolvedFileName) => normalize(resolvedFileName)),
423-
).toEqual([importedModule1, importedModule2])
440+
).toEqual([importedModule3, importedModule1, importedModule2])
424441
})
425442
})
426443

‎src/compiler/ts-compiler.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import type {
2121
ResolvedModuleWithFailedLookupLocations,
2222
} from 'typescript'
2323

24-
import { ConfigSet, TS_JEST_OUT_DIR } from '../config/config-set'
25-
import { LINE_FEED } from '../constants'
24+
import type { ConfigSet } from '../config/config-set'
25+
import { LINE_FEED, TS_TSX_REGEX } from '../constants'
2626
import type { StringMap, TsCompilerInstance, TsJestAstTransformer, TTypeScript } from '../types'
2727
import { rootLogger } from '../utils/logger'
2828
import { Errors, interpolate } from '../utils/messages'
@@ -116,12 +116,7 @@ export class TsCompiler implements TsCompilerInstance {
116116
private _createLanguageService(): void {
117117
// Initialize memory cache for typescript compiler
118118
this._parsedTsConfig.fileNames
119-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
120-
.filter(
121-
(fileName) =>
122-
!this.configSet.isTestFile(fileName) &&
123-
!fileName.includes(this._parsedTsConfig.options.outDir ?? TS_JEST_OUT_DIR),
124-
)
119+
.filter((fileName) => TS_TSX_REGEX.test(fileName) && !this.configSet.isTestFile(fileName))
125120
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
126121
.forEach((fileName) => this._fileVersionCache!.set(fileName, 0))
127122
/* istanbul ignore next */

0 commit comments

Comments
 (0)
Please sign in to comment.