From b7eb1811a927fe1e870d095a43e80e333248d1d4 Mon Sep 17 00:00:00 2001 From: Martijn The Date: Sun, 12 Nov 2017 16:38:20 +0100 Subject: [PATCH] - Fix #367: work-around bug in babel-core (/~https://github.com/babel/babel/pull/6524) to avoid hitting "Cannot find module '../../package' from 'node.js'" error - Change scripts/tests.js to copy the package sources, replacing the require('../../../../etc') hack (which for some reason caused #367 not to trip!?) --- index.js | 11 ++++++++ package.json | 4 +-- scripts/tests.js | 25 ++++++++++--------- src/preprocessor.ts | 2 -- .../__tests__/no-json-module-file-ext.spec.ts | 10 ++++++++ tests/babel-config-invalid/package.json | 2 +- .../package.json | 2 +- .../package.json | 2 +- tests/babel-config/package.json | 2 +- tests/button/package.json | 2 +- tests/deprecated-tsconfig/package.json | 2 +- .../jest.allowdefaultimports.json | 2 +- tests/dynamic-imports/package.json | 2 +- tests/hoist-errors/package.json | 2 +- tests/hoist-test/package.json | 2 +- tests/imports-test/package.json | 7 ++++-- .../__tests__/transpile-me.test.ts | 1 + tests/no-json-module-file-ext/package.json | 19 ++++++++++++++ tests/no-json-module-file-ext/tsconfig.json | 11 ++++++++ tests/no-synthetic-default/package.json | 2 +- tests/simple-async/package.json | 2 +- tests/simple-long-path/package.json | 2 +- tests/simple/package.json | 2 +- tests/skip-babelrc/package.json | 2 +- tests/synthetic-default/package.json | 2 +- tests/use-babelrc/package.json | 2 +- tests/use-strict/package.json | 2 +- tests/watch-test/package.json | 2 +- yarn.lock | 4 +-- 29 files changed, 93 insertions(+), 39 deletions(-) create mode 100644 index.js create mode 100644 tests/__tests__/no-json-module-file-ext.spec.ts create mode 100644 tests/no-json-module-file-ext/__tests__/transpile-me.test.ts create mode 100644 tests/no-json-module-file-ext/package.json create mode 100644 tests/no-json-module-file-ext/tsconfig.json diff --git a/index.js b/index.js new file mode 100644 index 0000000000..278fa174c5 --- /dev/null +++ b/index.js @@ -0,0 +1,11 @@ +module.exports = { + process: function() { + // Avoid require()'ing the preprocessor when index.js gets loaded as part + // of the transpiled test file output, to avoid tripping this bug in + // babel-core: /~https://github.com/babel/babel/pull/6524 which is to be + // fixed in babel-core 7.0. + // Related ts-jest issue: /~https://github.com/kulshekhar/ts-jest/issues/367 + return require('./dist/preprocessor').process.apply(null, arguments); + }, + install: require('./dist/install').install, +}; diff --git a/package.json b/package.json index 1da330311e..d25431aab6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ts-jest", "version": "21.2.2", - "main": "preprocessor.js", + "main": "index.js", "types": "./dist/index.d.ts", "description": "A preprocessor with sourcemap support to help use Typescript with Jest", "scripts": { @@ -70,7 +70,7 @@ "babel-plugin-istanbul": "^4.1.4", "babel-plugin-transform-es2015-modules-commonjs": "^6.24.1", "babel-preset-jest": "^21.2.0", - "fs-extra": "^4.0.0", + "fs-extra": "^4.0.2", "jest-config": "^21.2.1", "jest-util": "^21.2.1", "pkg-dir": "^2.0.0", diff --git a/scripts/tests.js b/scripts/tests.js index 96b09f1981..0232ec0900 100644 --- a/scripts/tests.js +++ b/scripts/tests.js @@ -5,6 +5,7 @@ process.env.PUBLIC_URL = ''; const jest = require('jest'); const fs = require('fs'); +const fsx = require('fs-extra'); const path = require('path'); function dirExists(dirPath) { @@ -38,9 +39,6 @@ function createIntegrationMock() { const testCaseFolders = getDirectories(testsRoot).filter(function(testDir) { return !(testDir.startsWith('__') && testDir.endsWith('__')); }); - const filesToMock = getFiles('dist').filter(function(fileName) { - return fileName.endsWith('.js'); - }); for (let i = 0; i < testCaseFolders.length; i++) { const testCaseNodeModules = path.join( testsRoot, @@ -49,16 +47,19 @@ function createIntegrationMock() { ); if (!dirExists(testCaseNodeModules)) { fs.mkdirSync(testCaseNodeModules); - const testCaseModuleFolder = path.join(testCaseNodeModules, 'ts-jest'); - fs.mkdirSync(testCaseModuleFolder); - filesToMock.forEach(function(fileName) { - const integrationMockPath = path.join(testCaseModuleFolder, fileName); - fs.appendFileSync( - integrationMockPath, - getIntegrationMockContent(fileName) - ); - }); } + const testCaseModuleFolder = path.join(testCaseNodeModules, 'ts-jest'); + fsx.copySync('.', testCaseModuleFolder, { + overwrite: true, + filter: function(src, dest) { + const shouldCopy = + src === '.' || + src.startsWith('dist') || + src === 'package.json' || + src.endsWith('.js'); + return shouldCopy; + }, + }); } } diff --git a/src/preprocessor.ts b/src/preprocessor.ts index 906d0a9aa0..6a86b0034c 100644 --- a/src/preprocessor.ts +++ b/src/preprocessor.ts @@ -6,8 +6,6 @@ import { JestConfig, Path, TransformOptions } from './jest-types'; import { getPostProcessHook } from './postprocess'; import { getTSConfig, getTSJestConfig } from './utils'; -export { install } from './install'; - export function process( src: string, path: Path, diff --git a/tests/__tests__/no-json-module-file-ext.spec.ts b/tests/__tests__/no-json-module-file-ext.spec.ts new file mode 100644 index 0000000000..a01197869b --- /dev/null +++ b/tests/__tests__/no-json-module-file-ext.spec.ts @@ -0,0 +1,10 @@ +import runJest from '../__helpers__/runJest'; + +// Regression test for +// /~https://github.com/kulshekhar/ts-jest/issues/367 +describe('no json in moduleFileExtensions', () => { + it('should run successfully', () => { + const result = runJest('../no-json-module-file-ext', ['--no-cache']); + expect(result.status).toBe(0); + }); +}); diff --git a/tests/babel-config-invalid/package.json b/tests/babel-config-invalid/package.json index f37ba46fd5..509fb6ebf6 100644 --- a/tests/babel-config-invalid/package.json +++ b/tests/babel-config-invalid/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - ".(ts|tsx)": "../../preprocessor.js" + ".(ts|tsx)": "ts-jest" }, "moduleDirectories": [ "node_modules", diff --git a/tests/babel-config-merge-ignore-babelrc/package.json b/tests/babel-config-merge-ignore-babelrc/package.json index 92300b7d27..5faee4f9e1 100644 --- a/tests/babel-config-merge-ignore-babelrc/package.json +++ b/tests/babel-config-merge-ignore-babelrc/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - ".(ts|tsx)": "../../preprocessor.js" + ".(ts|tsx)": "ts-jest" }, "moduleDirectories": [ "node_modules", diff --git a/tests/babel-config-merge-with-babelrc/package.json b/tests/babel-config-merge-with-babelrc/package.json index e8595dd3d8..8660f6eb9c 100644 --- a/tests/babel-config-merge-with-babelrc/package.json +++ b/tests/babel-config-merge-with-babelrc/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - ".(ts|tsx)": "../../preprocessor.js" + ".(ts|tsx)": "ts-jest" }, "moduleDirectories": [ "node_modules", diff --git a/tests/babel-config/package.json b/tests/babel-config/package.json index 59bbeaf952..7e2ecc37c8 100644 --- a/tests/babel-config/package.json +++ b/tests/babel-config/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - ".(ts|tsx)": "../../preprocessor.js" + ".(ts|tsx)": "ts-jest" }, "moduleDirectories": [ "node_modules", diff --git a/tests/button/package.json b/tests/button/package.json index 691af28ec3..3c6d32dd17 100644 --- a/tests/button/package.json +++ b/tests/button/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", "moduleFileExtensions": [ diff --git a/tests/deprecated-tsconfig/package.json b/tests/deprecated-tsconfig/package.json index 7ef6c897ac..a467c52f3c 100644 --- a/tests/deprecated-tsconfig/package.json +++ b/tests/deprecated-tsconfig/package.json @@ -4,7 +4,7 @@ "__TS_CONFIG__": "tsconfig.json" }, "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", "coverageReporters": [ diff --git a/tests/dynamic-imports/jest.allowdefaultimports.json b/tests/dynamic-imports/jest.allowdefaultimports.json index 6916ba629d..91f4164974 100644 --- a/tests/dynamic-imports/jest.allowdefaultimports.json +++ b/tests/dynamic-imports/jest.allowdefaultimports.json @@ -6,7 +6,7 @@ } }, "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", "moduleFileExtensions": [ diff --git a/tests/dynamic-imports/package.json b/tests/dynamic-imports/package.json index d3d2dda0e5..da4ca2764d 100644 --- a/tests/dynamic-imports/package.json +++ b/tests/dynamic-imports/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", "moduleFileExtensions": [ diff --git a/tests/hoist-errors/package.json b/tests/hoist-errors/package.json index f5eead7638..d2d426d10d 100644 --- a/tests/hoist-errors/package.json +++ b/tests/hoist-errors/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", "coverageReporters": [ diff --git a/tests/hoist-test/package.json b/tests/hoist-test/package.json index 417a0f0199..da7414e70f 100644 --- a/tests/hoist-test/package.json +++ b/tests/hoist-test/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "moduleDirectories": ["node_modules", "src"], "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", diff --git a/tests/imports-test/package.json b/tests/imports-test/package.json index 86dcdbcfe3..be12d7069a 100644 --- a/tests/imports-test/package.json +++ b/tests/imports-test/package.json @@ -1,9 +1,12 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, - "moduleDirectories": ["node_modules", "src"], + "moduleDirectories": [ + "node_modules", + "src" + ], "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", "moduleFileExtensions": [ "ts", diff --git a/tests/no-json-module-file-ext/__tests__/transpile-me.test.ts b/tests/no-json-module-file-ext/__tests__/transpile-me.test.ts new file mode 100644 index 0000000000..4fb1464536 --- /dev/null +++ b/tests/no-json-module-file-ext/__tests__/transpile-me.test.ts @@ -0,0 +1 @@ +test('no json in moduleFileExtensions', () => {}); diff --git a/tests/no-json-module-file-ext/package.json b/tests/no-json-module-file-ext/package.json new file mode 100644 index 0000000000..57c0c175ba --- /dev/null +++ b/tests/no-json-module-file-ext/package.json @@ -0,0 +1,19 @@ +{ + "jest": { + "transform": { + "^.+\\.tsx?$": "ts-jest" + }, + "moduleDirectories": [ + "node_modules", + "src" + ], + "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", + "moduleFileExtensions": [ + "ts", + "tsx", + "js", + "jsx", + "the.important.thing.is.that.json.is.missing.here" + ] + } +} diff --git a/tests/no-json-module-file-ext/tsconfig.json b/tests/no-json-module-file-ext/tsconfig.json new file mode 100644 index 0000000000..6a8b14571b --- /dev/null +++ b/tests/no-json-module-file-ext/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "ES5", + "module": "commonjs", + "moduleResolution": "node", + "noEmitOnError": false, + "jsx": "react", + "allowJs": true, + "baseUrl": "src" + } +} \ No newline at end of file diff --git a/tests/no-synthetic-default/package.json b/tests/no-synthetic-default/package.json index 417a0f0199..da7414e70f 100644 --- a/tests/no-synthetic-default/package.json +++ b/tests/no-synthetic-default/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "moduleDirectories": ["node_modules", "src"], "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", diff --git a/tests/simple-async/package.json b/tests/simple-async/package.json index 6e385a394c..cca7acd1d2 100644 --- a/tests/simple-async/package.json +++ b/tests/simple-async/package.json @@ -2,7 +2,7 @@ "jest": { "rootDir": "./", "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", "coverageReporters": [ diff --git a/tests/simple-long-path/package.json b/tests/simple-long-path/package.json index ad247e9580..0b4b74c76f 100644 --- a/tests/simple-long-path/package.json +++ b/tests/simple-long-path/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", "coverageReporters": [ diff --git a/tests/simple/package.json b/tests/simple/package.json index 4b10ef648c..9529a0f3ab 100644 --- a/tests/simple/package.json +++ b/tests/simple/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "ts-jest" }, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", "coverageReporters": [ diff --git a/tests/skip-babelrc/package.json b/tests/skip-babelrc/package.json index f5eead7638..d2d426d10d 100644 --- a/tests/skip-babelrc/package.json +++ b/tests/skip-babelrc/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", "coverageReporters": [ diff --git a/tests/synthetic-default/package.json b/tests/synthetic-default/package.json index 5ec1bfb8d9..eee0ad2384 100644 --- a/tests/synthetic-default/package.json +++ b/tests/synthetic-default/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "moduleDirectories": [ "node_modules", diff --git a/tests/use-babelrc/package.json b/tests/use-babelrc/package.json index eca1887afd..df8c03a58a 100644 --- a/tests/use-babelrc/package.json +++ b/tests/use-babelrc/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "moduleDirectories": [ "node_modules", diff --git a/tests/use-strict/package.json b/tests/use-strict/package.json index a0b913d1a6..4d13cfeca8 100644 --- a/tests/use-strict/package.json +++ b/tests/use-strict/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "mapCoverage": true, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", diff --git a/tests/watch-test/package.json b/tests/watch-test/package.json index ae6d82a9ef..b3bdfd5346 100644 --- a/tests/watch-test/package.json +++ b/tests/watch-test/package.json @@ -1,7 +1,7 @@ { "jest": { "transform": { - "^.+\\.tsx?$": "../../preprocessor.js" + "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" }, "mapCoverage": true, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", diff --git a/yarn.lock b/yarn.lock index 3856c04b10..2942c79193 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1404,7 +1404,7 @@ from@~0: version "0.1.7" resolved "https://registry.yarnpkg.com/from/-/from-0.1.7.tgz#83c60afc58b9c56997007ed1a768b3ab303a44fe" -fs-extra@^4.0.0: +fs-extra@^4.0.0, fs-extra@^4.0.2: version "4.0.2" resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-4.0.2.tgz#f91704c53d1b461f893452b0c307d9997647ab6b" dependencies: @@ -2279,7 +2279,7 @@ levn@~0.3.0: prelude-ls "~1.1.2" type-check "~0.3.2" -lint-staged@5.0.0: +lint-staged@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/lint-staged/-/lint-staged-5.0.0.tgz#f1c670e03e2fdf3f3d0eb81f72d3bcf658770e54" dependencies: