Skip to content

Commit

Permalink
fix(compiler): use resolveModuleNames TypeScript API
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Jul 7, 2020
1 parent 5da0da1 commit 0807967
Show file tree
Hide file tree
Showing 23 changed files with 356 additions and 308 deletions.
File renamed without changes.
File renamed without changes.
7 changes: 0 additions & 7 deletions e2e/__cases__/diagnostics/warn/main.spec.ts

This file was deleted.

2 changes: 0 additions & 2 deletions e2e/__cases__/diagnostics/warn/main.ts

This file was deleted.

4 changes: 2 additions & 2 deletions e2e/__cases__/test-helpers/deprecated.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mocked } from 'ts-jest'
import { foo } from './to-mock'
jest.mock('./to-mock')
import { foo } from './pass-to-mock'
jest.mock('./pass-to-mock')

test('foo', () => {
foo()
Expand Down
25 changes: 25 additions & 0 deletions e2e/__cases__/test-helpers/pass-to-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const foo = () => 'foo'

export function bar() {
return 'bar'
}
export namespace bar {
export function dummy() {
return 'dummy'
}
export namespace dummy {
export const deep = {
deeper: (one: string = '1') => `deeper ${one}`
}
}
}

export class MyClass {
constructor(s: string) {
this.myProperty = 3
this.myStr = s
}
somethingClassy() { return this.myStr }
public myProperty: number;
public myStr: string;
}
4 changes: 2 additions & 2 deletions e2e/__cases__/test-helpers/pass.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mocked } from 'ts-jest/utils'
import { foo, bar, MyClass } from './to-mock'
jest.mock('./to-mock')
import { foo, bar, MyClass } from './pass-to-mock'
jest.mock('./pass-to-mock')

test('foo', () => {
// real returns 'foo', mocked returns 'bar'
Expand Down
6 changes: 3 additions & 3 deletions e2e/__tests__/__snapshots__/diagnostics.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`With diagnostics throw should fail using template "default" 1`] = `
× jest
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand All @@ -28,7 +28,7 @@ exports[`With diagnostics throw should fail using template "default" 1`] = `
`;

exports[`With diagnostics throw should fail using template "with-babel-7" 1`] = `
× jest
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand All @@ -55,7 +55,7 @@ exports[`With diagnostics throw should fail using template "with-babel-7" 1`] =
`;

exports[`With diagnostics throw should fail using template "with-babel-7-string-config" 1`] = `
× jest
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand Down
12 changes: 6 additions & 6 deletions e2e/__tests__/__snapshots__/logger.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ Array [
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics using language service",
"[level:20] compileFn(): computing diagnostics using language service for resolved module",
"[level:20] computing cache key for <cwd>/Hello.ts",
"[level:20] processing <cwd>/Hello.ts",
"[level:20] compileAndUpdateOutput(): get compile output",
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics using language service",
"[level:20] compileFn(): computing diagnostics using language service for resolved module",
]
`;
Expand Down Expand Up @@ -67,15 +67,15 @@ Array [
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics using language service",
"[level:20] compileFn(): computing diagnostics using language service for resolved module",
"[level:20] calling babel-jest processor",
"[level:20] computing cache key for <cwd>/Hello.ts",
"[level:20] processing <cwd>/Hello.ts",
"[level:20] compileAndUpdateOutput(): get compile output",
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics using language service",
"[level:20] compileFn(): computing diagnostics using language service for resolved module",
"[level:20] calling babel-jest processor",
]
`;
Expand Down Expand Up @@ -111,15 +111,15 @@ Array [
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics using language service",
"[level:20] compileFn(): computing diagnostics using language service for resolved module",
"[level:20] calling babel-jest processor",
"[level:20] computing cache key for <cwd>/Hello.ts",
"[level:20] processing <cwd>/Hello.ts",
"[level:20] compileAndUpdateOutput(): get compile output",
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics using language service",
"[level:20] compileFn(): computing diagnostics using language service for resolved module",
"[level:20] calling babel-jest processor",
]
`;
Expand Down
6 changes: 4 additions & 2 deletions e2e/__tests__/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { allValidPackageSets } from '../__helpers__/templates'
import { configureTestCase } from '../__helpers__/test-case'

describe('With diagnostics throw', () => {
const testCase = configureTestCase('diagnostics/throw')
const testCase = configureTestCase('diagnostics', {
noCache: true, // warnings shown only on first compilation
})

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

describe('With diagnostics warn only', () => {
const testCase = configureTestCase('diagnostics/warn', {
const testCase = configureTestCase('diagnostics', {
tsJestConfig: {
diagnostics: { warnOnly: true },
},
Expand Down
44 changes: 35 additions & 9 deletions src/__helpers__/fakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
}
}

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

export const defaultResolve = (path: string): string => `resolved:${path}`

export function createConfigSet({
jestConfig,
tsJestConfig,
parentConfig,
resolve = defaultResolve,
...others
}: {
jestConfig?: Partial<Config.ProjectConfig>
tsJestConfig?: TsJestGlobalOptions
parentConfig?: TsJestGlobalOptions
resolve?: ((path: string) => string) | null
[key: string]: any
} = {}): ConfigSet {
const defaultTestRegex = ['(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.[jt]sx?$']
const defaultTestMatch = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)']
jestConfig = {
...jestConfig,
testMatch: jestConfig?.testMatch ? [...jestConfig.testMatch, ...defaultTestMatch] : defaultTestMatch,
testRegex: jestConfig?.testRegex ? [...defaultTestRegex, ...jestConfig.testRegex] : defaultTestRegex,
}
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), parentConfig)
if (resolve) {
cs.resolvePath = resolve
}
Object.keys(others).forEach((key) => {
Object.defineProperty(cs, key, { value: others[key] })
})

return cs
}

// not really unit-testing here, but it's hard to mock all those values :-D
export function makeCompiler({
jestConfig,
Expand All @@ -68,14 +101,7 @@ export function makeCompiler({
...(tsJestConfig.diagnostics as any),
pretty: false,
}
const testRegex = ['^.+\\.[tj]sx?$']
const testMatch = ['^.+\\.tsx?$']
jestConfig = {
...jestConfig,
testMatch: jestConfig?.testMatch ? [...jestConfig.testMatch, ...testMatch] : testMatch,
testRegex: jestConfig?.testRegex ? [...testRegex, ...jestConfig.testRegex] : testRegex,
}
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), parentConfig)
const cs = createConfigSet({ jestConfig, tsJestConfig, parentConfig, resolve: null })

return createCompilerInstance(cs)
}
3 changes: 0 additions & 3 deletions src/__mocks__/changed-modules/main.spec.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/__mocks__/changed-modules/main.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/__mocks__/thing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
interface Thing {
a: number
}
import { Thing } from './thing'

export const thing: Thing = { a: 1 }
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface Thing {
a: number
b: number
}
3 changes: 0 additions & 3 deletions src/__mocks__/unchanged-modules/main.spec.ts

This file was deleted.

65 changes: 57 additions & 8 deletions src/compiler/__snapshots__/language-service.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,63 @@ exports[`Language service allowJs option should compile js file for allowJs true
================================================================================
`;

exports[`Language service diagnostics should report diagnostics related to typings with pathRegex config matches file name 1`] = `"test-diagnostics.ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'."`;
exports[`Language service diagnostics related to other errors should throw error when cannot compile 1`] = `
"Unable to require \`.d.ts\` file for file: test-cannot-compile.d.ts.
This is usually the result of a faulty configuration or import. Make sure there is a \`.js\`, \`.json\` or another executable extension available alongside \`test-cannot-compile.d.ts\`."
`;

exports[`Language service diagnostics related to typings should report diagnostics for imported modules as well as test files which use those modules with cached 1`] = `"src/__mocks__/thing.spec.ts(3,14): error TS2741: Property 'b' is missing in type '{ a: number; }' but required in type 'Thing'."`;

exports[`Language service diagnostics related to typings should report diagnostics for imported modules as well as test files which use those modules with cached 2`] = `
Array [
"[level:20] compileAndUpdateOutput(): get compile output
",
"[level:20] compileFn(): compiling using language service
",
"[level:20] updateMemoryCache(): update memory cache for language service
",
"[level:20] visitSourceFileNode(): hoisting
",
"[level:20] compileFn(): computing diagnostics using language service for resolved module
",
"[level:20] compileFn(): computing diagnostics using language service for test file which uses the module
",
"[level:20] created new TSError
",
]
`;

exports[`Language service diagnostics related to typings should report diagnostics for imported modules as well as test files which use those modules without cached 1`] = `"src/__mocks__/thing.spec.ts(3,14): error TS2741: Property 'b' is missing in type '{ a: number; }' but required in type 'Thing'."`;

exports[`Language service diagnostics related to typings shouldn't report diagnostic when processing file isn't used by any test files 1`] = `
Array [
"[level:20] compileAndUpdateOutput(): get compile output
",
"[level:20] compileFn(): compiling using language service
",
"[level:20] updateMemoryCache(): update memory cache for language service
",
"[level:20] visitSourceFileNode(): hoisting
",
"[level:20] compileFn(): computing diagnostics using language service for resolved module
",
]
`;

exports[`Language service diagnostics related to typings shouldn't report diagnostics for test file name that has been type checked before 1`] = `
Array [
"[level:20] compileAndUpdateOutput(): get compile output
",
"[level:20] compileFn(): compiling using language service
",
"[level:20] updateMemoryCache(): update memory cache for language service
",
"[level:20] visitSourceFileNode(): hoisting
",
"[level:20] compileFn(): computing diagnostics using language service for resolved module
",
]
`;

exports[`Language service jsx option should compile tsx file for jsx preserve 1`] = `
===[ FILE: test-jsx.tsx ]=======================================================
Expand Down Expand Up @@ -85,10 +141,3 @@ exports[`Language service jsx option should compile tsx file for other jsx optio
version: 3
================================================================================
`;

exports[`Language service should do type check for the test file when imported module has changed 1`] = `"src/__mocks__/changed-modules/main.spec.ts(3,14): error TS2741: Property 'b' is missing in type '{ a: number; }' but required in type 'Thing'."`;

exports[`Language service should throw error when cannot compile 1`] = `
"Unable to require \`.d.ts\` file for file: test-cannot-compile.d.ts.
This is usually the result of a faulty configuration or import. Make sure there is a \`.js\`, \`.json\` or another executable extension available alongside \`test-cannot-compile.d.ts\`."
`;
52 changes: 0 additions & 52 deletions src/compiler/compiler-utils.ts

This file was deleted.

Loading

0 comments on commit 0807967

Please sign in to comment.