Skip to content

Commit

Permalink
- Fix #367: work-around bug in babel-core (babel/babel#6524) to avoid…
Browse files Browse the repository at this point in the history
… 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!?)
  • Loading branch information
Martijn The committed Nov 12, 2017
1 parent 895b919 commit b7eb181
Show file tree
Hide file tree
Showing 29 changed files with 93 additions and 39 deletions.
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
25 changes: 13 additions & 12 deletions scripts/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
},
});
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions tests/__tests__/no-json-module-file-ext.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion tests/babel-config-invalid/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
".(ts|tsx)": "../../preprocessor.js"
".(ts|tsx)": "ts-jest"
},
"moduleDirectories": [
"node_modules",
Expand Down
2 changes: 1 addition & 1 deletion tests/babel-config-merge-ignore-babelrc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
".(ts|tsx)": "../../preprocessor.js"
".(ts|tsx)": "ts-jest"
},
"moduleDirectories": [
"node_modules",
Expand Down
2 changes: 1 addition & 1 deletion tests/babel-config-merge-with-babelrc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
".(ts|tsx)": "../../preprocessor.js"
".(ts|tsx)": "ts-jest"
},
"moduleDirectories": [
"node_modules",
Expand Down
2 changes: 1 addition & 1 deletion tests/babel-config/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
".(ts|tsx)": "../../preprocessor.js"
".(ts|tsx)": "ts-jest"
},
"moduleDirectories": [
"node_modules",
Expand Down
2 changes: 1 addition & 1 deletion tests/button/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
Expand Down
2 changes: 1 addition & 1 deletion tests/deprecated-tsconfig/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"__TS_CONFIG__": "tsconfig.json"
},
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"coverageReporters": [
Expand Down
2 changes: 1 addition & 1 deletion tests/dynamic-imports/jest.allowdefaultimports.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
},
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
Expand Down
2 changes: 1 addition & 1 deletion tests/dynamic-imports/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
Expand Down
2 changes: 1 addition & 1 deletion tests/hoist-errors/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"coverageReporters": [
Expand Down
2 changes: 1 addition & 1 deletion tests/hoist-test/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"moduleDirectories": ["node_modules", "src"],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
Expand Down
7 changes: 5 additions & 2 deletions tests/imports-test/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"moduleDirectories": ["node_modules", "src"],
"moduleDirectories": [
"node_modules",
"src"
],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test('no json in moduleFileExtensions', () => {});
19 changes: 19 additions & 0 deletions tests/no-json-module-file-ext/package.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
11 changes: 11 additions & 0 deletions tests/no-json-module-file-ext/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": false,
"jsx": "react",
"allowJs": true,
"baseUrl": "src"
}
}
2 changes: 1 addition & 1 deletion tests/no-synthetic-default/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"moduleDirectories": ["node_modules", "src"],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
Expand Down
2 changes: 1 addition & 1 deletion tests/simple-async/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"jest": {
"rootDir": "./",
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"coverageReporters": [
Expand Down
2 changes: 1 addition & 1 deletion tests/simple-long-path/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"coverageReporters": [
Expand Down
2 changes: 1 addition & 1 deletion tests/simple/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"coverageReporters": [
Expand Down
2 changes: 1 addition & 1 deletion tests/skip-babelrc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"coverageReporters": [
Expand Down
2 changes: 1 addition & 1 deletion tests/synthetic-default/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"moduleDirectories": [
"node_modules",
Expand Down
2 changes: 1 addition & 1 deletion tests/use-babelrc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"moduleDirectories": [
"node_modules",
Expand Down
2 changes: 1 addition & 1 deletion tests/use-strict/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"mapCoverage": true,
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
Expand Down
2 changes: 1 addition & 1 deletion tests/watch-test/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "../../preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"mapCoverage": true,
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit b7eb181

Please sign in to comment.