Skip to content

Commit

Permalink
refactor(config): refactor ConfigSet class (#2050)
Browse files Browse the repository at this point in the history
- Convert most of getters to private fields
- Optimize logic of mapping user config to internal config
- Expose a few getters as public apis
- Rename interface `TsJestConfig$diagnostics` to `TsJestDiagnosticsCfg`
- Change `CachedConfigSet` to cache transformerConfig string instead of object type
  • Loading branch information
ahnpnl committed Oct 22, 2020
1 parent dee15ff commit 7468e51
Show file tree
Hide file tree
Showing 12 changed files with 1,313 additions and 1,799 deletions.
136 changes: 101 additions & 35 deletions e2e/__tests__/__snapshots__/logger.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ Array [
"[level:20] checking version of jest: OK",
"[level:20] created new transformer",
"[level:30] no matching config-set found, creating a new one",
"[level:20] backporting config",
"[level:20] normalized jest config",
"[level:20] normalized ts-jest config",
"[level:20] babel is disabled",
"[level:20] loaded module typescript",
"[level:20] patching typescript",
"[level:20] checking version of typescript: OK",
"[level:20] normalized compiler module config via ts-jest option",
"[level:20] backporting config",
"[level:20] normalized jest config",
"[level:20] babel is disabled",
"[level:20] normalized diagnostics config via ts-jest option",
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
"[level:20] normalized typescript config",
"[level:20] normalized typescript config via ts-jest option",
"[level:20] normalized custom AST transformers via ts-jest option",
"[level:20] file caching disabled",
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
"[level:20] processing <cwd>/Hello.spec.ts",
"[level:20] initializeLanguageServiceInstance(): create typescript compiler",
"[level:20] file caching disabled",
"[level:20] initializeLanguageServiceInstance(): creating language service",
"[level:20] compileAndUpdateOutput(): get compile output",
"[level:20] compileFn(): compiling using language service",
Expand All @@ -45,23 +47,25 @@ Array [
"[level:20] checking version of jest: OK",
"[level:20] created new transformer",
"[level:30] no matching config-set found, creating a new one",
"[level:20] backporting config",
"[level:20] normalized jest config",
"[level:20] normalized ts-jest config",
"[level:20] normalized babel config via ts-jest option",
"[level:20] loaded module typescript",
"[level:20] patching typescript",
"[level:20] checking version of typescript: OK",
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
"[level:20] normalized typescript config",
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
"[level:20] processing <cwd>/Hello.spec.ts",
"[level:20] creating babel-jest transformer",
"[level:20] normalized compiler module config via ts-jest option",
"[level:20] backporting config",
"[level:20] normalized jest config",
"[level:20] normalized babel config via ts-jest option",
"[level:20] loaded module babel-jest",
"[level:20] patching babel-jest",
"[level:20] checking version of babel-jest: OK",
"[level:20] initializeLanguageServiceInstance(): create typescript compiler",
"[level:20] created babel-jest transformer",
"[level:20] normalized diagnostics config via ts-jest option",
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
"[level:20] normalized typescript config via ts-jest option",
"[level:20] normalized custom AST transformers via ts-jest option",
"[level:20] file caching disabled",
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
"[level:20] processing <cwd>/Hello.spec.ts",
"[level:20] initializeLanguageServiceInstance(): create typescript compiler",
"[level:20] initializeLanguageServiceInstance(): creating language service",
"[level:20] compileAndUpdateOutput(): get compile output",
"[level:20] compileFn(): compiling using language service",
Expand All @@ -88,24 +92,26 @@ Array [
"[level:20] checking version of jest: OK",
"[level:20] created new transformer",
"[level:30] no matching config-set found, creating a new one",
"[level:20] loaded module typescript",
"[level:20] patching typescript",
"[level:20] checking version of typescript: OK",
"[level:20] normalized compiler module config via ts-jest option",
"[level:20] backporting config",
"[level:20] normalized jest config",
"[level:20] resolved path from babel.config.js to <cwd>/babel.config.js",
"[level:20] normalized ts-jest config",
"[level:20] normalized babel config via ts-jest option",
"[level:20] loaded module typescript",
"[level:20] patching typescript",
"[level:20] checking version of typescript: OK",
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
"[level:20] normalized typescript config",
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
"[level:20] processing <cwd>/Hello.spec.ts",
"[level:20] creating babel-jest transformer",
"[level:20] loaded module babel-jest",
"[level:20] patching babel-jest",
"[level:20] checking version of babel-jest: OK",
"[level:20] initializeLanguageServiceInstance(): create typescript compiler",
"[level:20] created babel-jest transformer",
"[level:20] normalized diagnostics config via ts-jest option",
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
"[level:20] normalized typescript config via ts-jest option",
"[level:20] normalized custom AST transformers via ts-jest option",
"[level:20] file caching disabled",
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
"[level:20] processing <cwd>/Hello.spec.ts",
"[level:20] initializeLanguageServiceInstance(): create typescript compiler",
"[level:20] initializeLanguageServiceInstance(): creating language service",
"[level:20] compileAndUpdateOutput(): get compile output",
"[level:20] compileFn(): compiling using language service",
Expand All @@ -124,12 +130,13 @@ Array [
]
`;
exports[`ts-jest logging deprecation warning with astTransformers config as an object should pass using template "default" 1`] = `
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "default" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Expand All @@ -142,12 +149,13 @@ exports[`ts-jest logging deprecation warning with astTransformers config as an o
================================================================================
`;
exports[`ts-jest logging deprecation warning with astTransformers config as an object should pass using template "with-babel-7" 1`] = `
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "with-babel-7" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Expand All @@ -160,12 +168,13 @@ exports[`ts-jest logging deprecation warning with astTransformers config as an o
================================================================================
`;
exports[`ts-jest logging deprecation warning with astTransformers config as an object should pass using template "with-babel-7-string-config" 1`] = `
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "with-babel-7-string-config" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Expand All @@ -178,13 +187,13 @@ exports[`ts-jest logging deprecation warning with astTransformers config as an o
================================================================================
`;
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "default" 1`] = `
exports[`ts-jest logging deprecation warning with packageJson config should pass using template "default" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
ts-jest[config] (WARN) The option \`packageJson\` is deprecated and will be removed in ts-jest 27. This option is not used by internal \`ts-jest\`
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Expand All @@ -197,13 +206,13 @@ exports[`ts-jest logging deprecation warning with astTransformers config as stri
================================================================================
`;
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "with-babel-7" 1`] = `
exports[`ts-jest logging deprecation warning with packageJson config should pass using template "with-babel-7" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
ts-jest[config] (WARN) The option \`packageJson\` is deprecated and will be removed in ts-jest 27. This option is not used by internal \`ts-jest\`
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Expand All @@ -216,13 +225,70 @@ exports[`ts-jest logging deprecation warning with astTransformers config as stri
================================================================================
`;
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "with-babel-7-string-config" 1`] = `
exports[`ts-jest logging deprecation warning with packageJson config should pass using template "with-babel-7-string-config" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
ts-jest[config] (WARN) The option \`packageJson\` is deprecated and will be removed in ts-jest 27. This option is not used by internal \`ts-jest\`
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;
exports[`ts-jest logging deprecation warning with tsConfig config should pass using template "default" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
ts-jest[config] (WARN) The option \`tsConfig\` is deprecated and will be removed in ts-jest 27, use \`tsconfig\` instead
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;
exports[`ts-jest logging deprecation warning with tsConfig config should pass using template "with-babel-7" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
ts-jest[config] (WARN) The option \`tsConfig\` is deprecated and will be removed in ts-jest 27, use \`tsconfig\` instead
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;
exports[`ts-jest logging deprecation warning with tsConfig config should pass using template "with-babel-7-string-config" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
ts-jest[config] (WARN) The option \`tsConfig\` is deprecated and will be removed in ts-jest 27, use \`tsconfig\` instead
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Expand Down
24 changes: 20 additions & 4 deletions e2e/__tests__/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LogContexts, LogLevels } from 'bs-logger'
import { existsSync } from 'fs'

import { PackageSets, allValidPackageSets } from '../__helpers__/templates'
import { PackageSets, allValidPackageSets, allPackageSetsWithPreset } from '../__helpers__/templates'
import { configureTestCase } from '../__helpers__/test-case'

describe('ts-jest logging', () => {
Expand Down Expand Up @@ -108,14 +108,30 @@ describe('ts-jest logging', () => {
})
})

describe('with astTransformers config as an object', () => {
describe('with packageJson config', () => {
const testCase = configureTestCase('simple', {
tsJestConfig: {
astTransformers: {}
packageJson: true,
}
})

testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => {
testCase.runWithTemplates(allPackageSetsWithPreset, 0, (runTest, { testLabel }) => {
it(testLabel, () => {
const result = runTest()
expect(result.status).toBe(0)
expect(result).toMatchSnapshot()
})
})
})

describe('with tsConfig config', () => {
const testCase = configureTestCase('simple', {
tsJestConfig: {
tsConfig: true,
}
})

testCase.runWithTemplates(allPackageSetsWithPreset, 0, (runTest, { testLabel }) => {
it(testLabel, () => {
const result = runTest()
expect(result.status).toBe(0)
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@ export const createCompilerInstance = (configs: ConfigSet): TsCompiler => {
const logger = configs.logger.child({ namespace: 'ts-compiler' })
const {
parsedTsConfig: { options: compilerOptions },
tsJest,
} = configs
const extensions = ['.ts', '.tsx']
// Enable `allowJs` when flag is set.
if (compilerOptions.allowJs) {
extensions.push('.js')
extensions.push('.jsx')
}
const compilerInstance: CompilerInstance = !tsJest.isolatedModules
const compilerInstance: CompilerInstance = !configs.isolatedModules
? initializeLanguageServiceInstance(configs, logger) // Use language services by default
: initializeTranspilerInstance(configs, logger)
const compile = compileAndUpdateOutput(compilerInstance.compileFn, logger)
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/language-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function doTypeChecking(
service: _ts.LanguageService,
logger: Logger,
): void {
if (configs.shouldReportDiagnostic(fileName)) {
if (configs.shouldReportDiagnostics(fileName)) {
// Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`.
const diagnostics = service.getSemanticDiagnostics(fileName).concat(service.getSyntacticDiagnostics(fileName))
diagnosedFiles.push(fileName)
Expand Down Expand Up @@ -200,7 +200,7 @@ export const initializeLanguageServiceInstance = (configs: ConfigSet, logger: Lo
getCurrentDirectory: () => cwd,
getCompilationSettings: () => options,
getDefaultLibFileName: () => ts.getDefaultLibFilePath(options),
getCustomTransformers: () => configs.tsCustomTransformers,
getCustomTransformers: () => configs.customTransformers,
resolveModuleNames,
}

Expand Down
6 changes: 3 additions & 3 deletions src/compiler/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export const initializeTranspilerInstance = (configs: ConfigSet, logger: Logger)

const result: _ts.TranspileOutput = ts.transpileModule(code, {
fileName,
transformers: configs.tsCustomTransformers,
transformers: configs.customTransformers,
compilerOptions: options,
reportDiagnostics: configs.shouldReportDiagnostic(fileName),
reportDiagnostics: configs.shouldReportDiagnostics(fileName),
})
if (result.diagnostics && configs.shouldReportDiagnostic(fileName)) {
if (result.diagnostics && configs.shouldReportDiagnostics(fileName)) {
configs.raiseDiagnostics(result.diagnostics, fileName, logger)
}

Expand Down

0 comments on commit 7468e51

Please sign in to comment.