Skip to content

Commit 5f26054

Browse files
authored
fix(compiler): use resolveModuleNames TypeScript API to get resolved modules for test files (#1784)
Fixes #1747
1 parent 00a3726 commit 5f26054

File tree

24 files changed

+372
-297
lines changed

24 files changed

+372
-297
lines changed
File renamed without changes.
File renamed without changes.

e2e/__cases__/diagnostics/warn/main.spec.ts

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

e2e/__cases__/diagnostics/warn/main.ts

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

e2e/__cases__/test-helpers/deprecated.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mocked } from 'ts-jest'
2-
import { foo } from './to-mock'
3-
jest.mock('./to-mock')
2+
import { foo } from './pass-to-mock'
3+
jest.mock('./pass-to-mock')
44

55
test('foo', () => {
66
foo()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export const foo = () => 'foo'
2+
3+
export function bar() {
4+
return 'bar'
5+
}
6+
export namespace bar {
7+
export function dummy() {
8+
return 'dummy'
9+
}
10+
export namespace dummy {
11+
export const deep = {
12+
deeper: (one: string = '1') => `deeper ${one}`
13+
}
14+
}
15+
}
16+
17+
export class MyClass {
18+
constructor(s: string) {
19+
this.myProperty = 3
20+
this.myStr = s
21+
}
22+
somethingClassy() { return this.myStr }
23+
public myProperty: number;
24+
public myStr: string;
25+
}

e2e/__cases__/test-helpers/pass.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mocked } from 'ts-jest/utils'
2-
import { foo, bar, MyClass } from './to-mock'
3-
jest.mock('./to-mock')
2+
import { foo, bar, MyClass } from './pass-to-mock'
3+
jest.mock('./pass-to-mock')
44

55
test('foo', () => {
66
// real returns 'foo', mocked returns 'bar'

e2e/__tests__/__snapshots__/diagnostics.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`With diagnostics throw should fail using template "default" 1`] = `
4-
× jest
4+
× jest --no-cache
55
↳ exit code: 1
66
===[ STDOUT ]===================================================================
77
@@ -28,7 +28,7 @@ exports[`With diagnostics throw should fail using template "default" 1`] = `
2828
`;
2929

3030
exports[`With diagnostics throw should fail using template "with-babel-7" 1`] = `
31-
× jest
31+
× jest --no-cache
3232
↳ exit code: 1
3333
===[ STDOUT ]===================================================================
3434
@@ -55,7 +55,7 @@ exports[`With diagnostics throw should fail using template "with-babel-7" 1`] =
5555
`;
5656

5757
exports[`With diagnostics throw should fail using template "with-babel-7-string-config" 1`] = `
58-
× jest
58+
× jest --no-cache
5959
↳ exit code: 1
6060
===[ STDOUT ]===================================================================
6161

e2e/__tests__/diagnostics.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { allValidPackageSets } from '../__helpers__/templates'
22
import { configureTestCase } from '../__helpers__/test-case'
33

44
describe('With diagnostics throw', () => {
5-
const testCase = configureTestCase('diagnostics/throw')
5+
const testCase = configureTestCase('diagnostics', {
6+
noCache: true, // warnings shown only on first compilation
7+
})
68

79
testCase.runWithTemplates(allValidPackageSets, 1, (runTest, { testLabel }) => {
810
it(testLabel, () => {
@@ -14,7 +16,7 @@ describe('With diagnostics throw', () => {
1416
})
1517

1618
describe('With diagnostics warn only', () => {
17-
const testCase = configureTestCase('diagnostics/warn', {
19+
const testCase = configureTestCase('diagnostics', {
1820
tsJestConfig: {
1921
diagnostics: { warnOnly: true },
2022
},

src/__helpers__/fakers.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
2626
}
2727
}
2828

29-
export function getJestConfig<T extends Config.ProjectConfig>(
29+
function getJestConfig<T extends Config.ProjectConfig>(
3030
options?: Partial<Config.InitialOptions | Config.ProjectConfig>,
3131
tsJestOptions?: TsJestGlobalOptions,
3232
): T {
@@ -53,6 +53,39 @@ export function importReason(text = '[[BECAUSE]]'): ImportReasons {
5353
return text as any
5454
}
5555

56+
export const defaultResolve = (path: string): string => `resolved:${path}`
57+
58+
export function createConfigSet({
59+
jestConfig,
60+
tsJestConfig,
61+
parentConfig,
62+
resolve = defaultResolve,
63+
...others
64+
}: {
65+
jestConfig?: Partial<Config.ProjectConfig>
66+
tsJestConfig?: TsJestGlobalOptions
67+
parentConfig?: TsJestGlobalOptions
68+
resolve?: ((path: string) => string) | null
69+
[key: string]: any
70+
} = {}): ConfigSet {
71+
const defaultTestRegex = ['(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.[jt]sx?$']
72+
const defaultTestMatch = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)']
73+
jestConfig = {
74+
...jestConfig,
75+
testMatch: jestConfig?.testMatch ? [...jestConfig.testMatch, ...defaultTestMatch] : defaultTestMatch,
76+
testRegex: jestConfig?.testRegex ? [...defaultTestRegex, ...jestConfig.testRegex] : defaultTestRegex,
77+
}
78+
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), parentConfig)
79+
if (resolve) {
80+
cs.resolvePath = resolve
81+
}
82+
Object.keys(others).forEach((key) => {
83+
Object.defineProperty(cs, key, { value: others[key] })
84+
})
85+
86+
return cs
87+
}
88+
5689
// not really unit-testing here, but it's hard to mock all those values :-D
5790
export function makeCompiler({
5891
jestConfig,
@@ -68,14 +101,7 @@ export function makeCompiler({
68101
...(tsJestConfig.diagnostics as any),
69102
pretty: false,
70103
}
71-
const testRegex = ['^.+\\.[tj]sx?$']
72-
const testMatch = ['^.+\\.tsx?$']
73-
jestConfig = {
74-
...jestConfig,
75-
testMatch: jestConfig?.testMatch ? [...jestConfig.testMatch, ...testMatch] : testMatch,
76-
testRegex: jestConfig?.testRegex ? [...testRegex, ...jestConfig.testRegex] : testRegex,
77-
}
78-
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), parentConfig)
104+
const cs = createConfigSet({ jestConfig, tsJestConfig, parentConfig, resolve: null })
79105

80106
return createCompilerInstance(cs)
81107
}

0 commit comments

Comments
 (0)