Skip to content

Commit

Permalink
Merge pull request #373 from martijnthe/change-entry-to-preprocessor.js
Browse files Browse the repository at this point in the history
Enable using 'ts-jest' package name as transform (Take 2) + fix #367
  • Loading branch information
kulshekhar authored Nov 17, 2017
2 parents 36875a4 + 91a1f0e commit 958d84d
Show file tree
Hide file tree
Showing 40 changed files with 304 additions and 53 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Justin Bay <jwbay@users.noreply.github.com>
Kulshekhar Kabra <kulshekhar@users.noreply.github.com>
Kyle Roach <kroach.work@gmail.com>
Marshall Bowers <elliott.codes@gmail.com>
Martijn The <post@martijnthe.nl>
Matheus Gambati <matheusgambati@gmail.com>
Maxim Samoilov <samoilowmaxim@gmail.com>
Mohammad Rajabifard <mo.rajbi@gmail.com>
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Modify your project's `package.json` so that the `jest` section looks something
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
Expand Down Expand Up @@ -225,7 +225,7 @@ In `package.json`, inside `jest` section, the `transform` should be like this:
```json
"transform": {
"^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest",
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "ts-jest"
}
```

Expand All @@ -236,7 +236,7 @@ Fully completed jest section should look like this:
"preset": "react-native",
"transform": {
"^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest",
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
Expand Down Expand Up @@ -268,7 +268,7 @@ You'll also need to extend your `transform` regex with `html` extension:
{
"jest": {
"transform": {
"^.+\\.(tsx?|html)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.(tsx?|html)$": "ts-jest"
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as _install from './install';
import * as preprocessor from './preprocessor';

interface TsJestModule {
install: typeof _install.install;
process: typeof preprocessor.process;
getCacheKey: typeof preprocessor.getCacheKey;
}

declare const tsJestModule: TsJestModule;
export = tsJestModule;
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
module.exports = require('./dist/index');
// 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
function lazyRequire(fnName) {
return function() {
return require('./dist/preprocessor')[fnName].apply(null, arguments);
};
}

module.exports = {
process: lazyRequire('process'),
getCacheKey: lazyRequire('getCacheKey'),
install: require('./dist/install').install,
};
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "ts-jest",
"version": "21.2.2",
"version": "21.2.3",
"main": "index.js",
"types": "./dist/index.d.ts",
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
"scripts": {
"build": "tsc -p .",
"build:watch": "tsc -p . -w",
"build": "cpx index.d.ts dist/ && tsc -p .",
"build:watch": "cpx index.d.ts dist/ && tsc -p . -w",
"clean": "rimraf dist/**/* && rimraf tests/simple/coverage && rimraf tests/simple-async/coverage",
"clean-build": "npm run clean && npm run build",
"pretest": "npm run tslint && npm run clean-build",
Expand Down Expand Up @@ -70,15 +70,16 @@
"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",
"cpx": "^1.5.0",
"fs-extra": "^4.0.2",
"jest-config": "^21.2.1",
"jest-util": "^21.2.1",
"pkg-dir": "^2.0.0",
"source-map-support": "^0.5.0",
"yargs": "^10.0.3"
},
"peerDependencies": {
"jest": "^21.1.0 || ^21.1.0-alpha.1 || ^22.0.0-alpha.1",
"jest": "^21.1.0 || ^21.3.0-alpha.1 || ^22.0.0-alpha.1",
"typescript": "2.x"
},
"devDependencies": {
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
1 change: 0 additions & 1 deletion src/index.ts → src/install.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as sourceMapSupport from 'source-map-support';
import { defaultRetrieveFileHandler } from './default-retrieve-file-handler';

export { transpileIfTypescript } from './transpile-if-ts';
export function install() {
const options: sourceMapSupport.Options = {};

Expand Down
5 changes: 4 additions & 1 deletion src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export function process(
const outputFilePath = nodepath.join(
config.cacheDirectory,
'/ts-jest/',
crypto.createHash('md5').update(path).digest('hex'),
crypto
.createHash('md5')
.update(path)
.digest('hex'),
);

fs.outputFileSync(outputFilePath, outputText);
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/__tests__/transpile-if-ts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transpileIfTypescript } from '../../src';
import { transpileIfTypescript } from '../../src/transpile-if-ts';

describe('transpileIfTypescript', () => {
it('should ignore anything non-TS', () => {
Expand Down
8 changes: 8 additions & 0 deletions tests/__tests__/ts-jest-module-interface.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import runJest from '../__helpers__/runJest';

describe('ts-jest module interface', () => {
it('should run successfully', () => {
const result = runJest('../ts-jest-module-interface', ['--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
2 changes: 1 addition & 1 deletion tests/imports-test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"allowJs": true,
"baseUrl": "src"
}
}
}
2 changes: 1 addition & 1 deletion tests/jestconfig-test/jest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"rootDir": "./",
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "ts-jest"
},
"mapCoverage": true,
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
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
Loading

0 comments on commit 958d84d

Please sign in to comment.