diff --git a/scripts/tests.js b/scripts/tests.js index 2bd482195c..a06cdf7bb3 100644 --- a/scripts/tests.js +++ b/scripts/tests.js @@ -13,11 +13,19 @@ function getDirectories(rootDir) { }); } +function isJestFolder(basename) { + return basename.startsWith('__') && basename.endsWith('__'); +} + +// TODO: later we could add a `.test-case-keep` empty file in each folder? +// ...or move all into a `test-cases` dedicated directory +function isTestCaseFolder(basename) { + return !isJestFolder(basename); +} + function createIntegrationMock() { const testsRoot = 'tests'; - const testCaseFolders = getDirectories(testsRoot).filter(function(testDir) { - return !/^(?:utils|__.+__)$/.test(testDir); - }); + const testCaseFolders = getDirectories(testsRoot).filter(isTestCaseFolder); testCaseFolders.forEach(directory => { const testCaseNodeModules = path.join(testsRoot, directory, 'node_modules'); @@ -41,9 +49,6 @@ function createIntegrationMock() { createIntegrationMock(); -// HACK: allow us to change the `startDir()` during tests -process.env.__RUNNING_TS_JEST_TESTS = Date.now(); - const argv = process.argv.slice(2); argv.push('--no-cache'); argv.push('--testPathPattern', '^(?!(.*watch.spec.ts$)).*'); diff --git a/src/preprocess.ts b/src/preprocess.ts index 9e954bd46c..064e901001 100644 --- a/src/preprocess.ts +++ b/src/preprocess.ts @@ -22,7 +22,11 @@ export default function preprocess( const isHtmlFile = /\.html$/.test(filePath); // This is to support angular 2. See https://github.com/kulshekhar/ts-jest/pull/145 - if (isHtmlFile && (jestConfig.globals as any).__TRANSFORM_HTML__) { + if ( + isHtmlFile && + jestConfig.globals && + (jestConfig.globals as any).__TRANSFORM_HTML__ + ) { src = 'module.exports=' + JSON.stringify(src) + ';'; } diff --git a/src/utils/get-ts-config.ts b/src/utils/get-ts-config.ts index c556a4112e..6c018303ed 100644 --- a/src/utils/get-ts-config.ts +++ b/src/utils/get-ts-config.ts @@ -95,19 +95,6 @@ function readCompilerOptions(configPath: string): CompilerOptions { return options; } -// function getStartDir(jestConfig: jest.ProjectConfig): string { -// // This is needed because of the way our tests are structured. -// // If this is being executed as a library (under node_modules) -// // we want to start with the project directory that's three -// // levels above. -// // If this is being executed from the test suite, we want to start -// // in the directory of the test - -// // TODO: shouldn't we use the path of jest config here instead of '.' ? -// // return process.env.__RUNNING_TS_JEST_TESTS ? process.cwd() : '.'; -// return process.env.__RUNNING_TS_JEST_TESTS ? process.cwd() : (jestConfig.rootDir || process.cwd()); -// } - // we don't need any data, just its full path const tsConfigReader = { basename: TSCONFIG_FILENAME, read: () => 0 }; diff --git a/tests/__helpers__/jest-config.ts b/tests/__helpers__/jest-config.ts deleted file mode 100644 index 61b8c7bcff..0000000000 --- a/tests/__helpers__/jest-config.ts +++ /dev/null @@ -1,210 +0,0 @@ -import { TsJestConfig } from '../../dist/types'; - -const { resolve } = require.requireActual('path'); - -function createJestConfig( - testModulePath: string, - tsJestOptions: TsJestConfig | null = null, - jestOptions: jest.InitialOptions = {}, -): jest.ProjectConfig { - const rootDir = resolve(__dirname, '..', testModulePath); - let options = { ...jestOptions }; - if (tsJestOptions) - options.globals = { ...options.globals, 'ts-jest': tsJestOptions }; - return { rootDir, cwd: rootDir, ...options } as any; -} -require('fs').read; -const jestConfig = Object.assign(createJestConfig, { - babelConfig: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('babel-config', t, j), - babelConfigInvalid: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('babel-config-invalid', t, j), - babelConfigMergeIgnoreBabelrc: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => createJestConfig('babel-config-merge-ignore-babelrc', t, j), - babelConfigMergeWithBabelrc: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => createJestConfig('babel-config-merge-with-babelrc', t, j), - button: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('button', t, j), - dynamicImports: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('dynamic-imports', t, j), - hoistErrors: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('hoist-errors', t, j), - hoistTest: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('hoist-test', t, j), - importsTest: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('imports-test', t, j), - jestProjects: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('jest-projects', t, j), - jestProjectsWithWorkspace: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => createJestConfig('jest-projects-with-workspace', t, j), - jestconfigTest: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('jestconfig-test', t, j), - metadataEmit: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('metadata-emit', t, j), - noJsonModuleFileExt: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('no-json-module-file-ext', t, j), - noSourcemaps: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('no-sourcemaps', t, j), - noSyntheticDefault: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('no-synthetic-default', t, j), - simple: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('simple', t, j), - simpleAsync: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('simple-async', t, j), - simpleLongPath: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('simple-long-path', t, j), - skipBabelrc: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('skip-babelrc', t, j), - syntheticDefault: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('synthetic-default', t, j), - tsDiagnostics: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('ts-diagnostics', t, j), - tsJestModuleInterface: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('ts-jest-module-interface', t, j), - tsconfigTest: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('tsconfig-test', t, j), - useBabelrc: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('use-babelrc', t, j), - useConfigFromNodeModules: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => createJestConfig('use-config-from-node-modules', t, j), - useStrict: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('use-strict', t, j), - utils: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('utils', t, j), - watchTest: (t?: TsJestConfig | null, j?: jest.InitialOptions) => - createJestConfig('watch-test', t, j), -}); - -interface TestJestConfigHelpers { - ( - testModulePath: string, - tsJestOptions?: TsJestConfig | null, - jestOptions?: jest.InitialOptions, - ): jest.ProjectConfig; - babelConfig: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - babelConfigInvalid: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - babelConfigMergeIgnoreBabelrc: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - babelConfigMergeWithBabelrc: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - button: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - dynamicImports: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - hoistErrors: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - hoistTest: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - importsTest: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - jestProjects: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - jestProjectsWithWorkspace: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - jestconfigTest: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - metadataEmit: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - noJsonModuleFileExt: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - noSourcemaps: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - noSyntheticDefault: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - simple: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - simpleAsync: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - simpleLongPath: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - skipBabelrc: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - syntheticDefault: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - tsDiagnostics: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - tsJestModuleInterface: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - tsconfigTest: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - useBabelrc: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - useConfigFromNodeModules: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - useStrict: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - utils: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; - watchTest: ( - t?: TsJestConfig | null, - j?: jest.InitialOptions, - ) => jest.ProjectConfig; -} - -export default jestConfig as TestJestConfigHelpers; diff --git a/tests/__helpers__/mock-jest-config.ts b/tests/__helpers__/mock-jest-config.ts new file mode 100644 index 0000000000..8981a549ad --- /dev/null +++ b/tests/__helpers__/mock-jest-config.ts @@ -0,0 +1,26 @@ +import { TsJestConfig } from '../../dist/types'; + +const { resolve } = require.requireActual('path'); + +/** + * Mock a jest config object for the test case in given folder + * + * Basically it defines `rootDir` and `cwd` properties to the full path of that + * test case, as Jest would do. + * + * Accepts an optional config object, which will be defined on `globals.ts-jest` + */ +export default function mockJestConfig( + testCaseFolder: string, + tsJest: TsJestConfig | null = null, +): jest.ProjectConfig { + // resolves the path since jest would give a resolved path + const rootDir = resolve(__dirname, '..', testCaseFolder); + // create base jest config object + let options: any = { rootDir, cwd: rootDir }; + // adds TS Jest options if any given + if (tsJest != null) { + options.globals = { 'ts-jest': tsJest }; + } + return options; +} diff --git a/tests/__tests__/get-cache-key.spec.ts b/tests/__tests__/get-cache-key.spec.ts index 9fff410267..d71ccab5b6 100644 --- a/tests/__tests__/get-cache-key.spec.ts +++ b/tests/__tests__/get-cache-key.spec.ts @@ -1,5 +1,5 @@ import getCacheKey from '../../dist/utils/get-cache-key'; -import cfg from '../__helpers__/jest-config'; +import mockJestConfig from '../__helpers__/mock-jest-config'; import _getTSConfig from '../../dist/utils/get-ts-config'; jest.mock('../../dist/utils/get-ts-config', () => { @@ -13,10 +13,11 @@ const getTSConfig: jest.Mock = _getTSConfig as any; describe('getCacheKey', () => { const src = 'console.log(123);'; - const jestConfig = cfg.simple(null, { + const jestConfig = { + ...mockJestConfig('simple'), transform: { '^.+\\\\.tsx?$': '../../preprocessor.js' }, testRegex: '(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.(jsx?|tsx?)$', - }); + }; const filepath = `${jestConfig.rootDir}/some-file.ts`; const configStr = JSON.stringify(jestConfig); const options = { instrument: false, rootDir: jestConfig.rootDir }; diff --git a/tests/__tests__/html-transform.spec.ts b/tests/__tests__/html-transform.spec.ts index 718b5f797a..b25d88fa1c 100644 --- a/tests/__tests__/html-transform.spec.ts +++ b/tests/__tests__/html-transform.spec.ts @@ -1,32 +1,47 @@ import * as tsJest from '../../dist'; -import jestConfig from '../__helpers__/jest-config'; +import mockJestConfig from '../__helpers__/mock-jest-config'; -const config = jestConfig.simple({}); -const filePath = `${config.rootDir}/some-file.html`; +const TEST_CASE = 'simple'; +const FILENAME = 'some-file.html'; -// wrap a transformed source so that we can fake a `require()` on it by calling the returned wrapper -const wrap = (src: string) => - new Function(`var module={}; ${src} return module.exports;`) as any; - -const source = `
This is a backtilt \`