Skip to content

Commit

Permalink
Merge pull request #370 from GoodForOneFare/fix-commonjs-transform-as…
Browse files Browse the repository at this point in the history
…signment

Don't accumulate commonjs module transforms
  • Loading branch information
kulshekhar committed Nov 12, 2017
2 parents 7334fb6 + 20fadf2 commit 36875a4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

Alex Jover Morales <alexjovermorales@gmail.com>
Andreas Krummsdorf <andreas.krummsdorf@doksafe.de>
Anonymous <goodforonefare@gmail.com>
Bartosz Gościński <bargosc@gmail.com>
Blake Embrey <hello@blakeembrey.com>
Brian Ruddy <briancruddy@gmail.com>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-jest",
"version": "21.2.1",
"version": "21.2.2",
"main": "index.js",
"types": "./dist/index.d.ts",
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
Expand Down
4 changes: 1 addition & 3 deletions src/postprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ export const getPostProcessHook = (
return src => src; // Identity function
}

const plugins = tsJestConfig.babelConfig
? tsJestConfig.babelConfig.plugins || []
: [];
const plugins = Array.from(tsJestConfig.babelConfig && tsJestConfig.babelConfig.plugins || []);
// If we're not skipping babel
if (tsCompilerOptions.allowSyntheticDefaultImports) {
plugins.push('transform-es2015-modules-commonjs');
Expand Down
71 changes: 71 additions & 0 deletions tests/__tests__/postprocess.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
jest.mock('babel-core', () => {
return {
transform: jest.fn(() => {
return {code: 'stubbed_code'};
}),
};
});

import { getPostProcessHook } from '../../src/postprocess';

describe('postprocess', () => {
function runHook(tsCompilerOptions = {}, jestConfig = {}, tsJestConfig = {}) {
return getPostProcessHook(tsCompilerOptions, jestConfig, tsJestConfig)(
'input_code',
'fake_file',
{},
{instrument: null},
);
}

it('skips postprocess when skipBabel=true', () => {
const transformMock = require.requireMock('babel-core').transform;

runHook({}, {}, {skipBabel: true});
expect(transformMock).not.toBeCalled();
});

it('skips commonjs module transform by default', () => {
const transformMock = require.requireMock('babel-core').transform;

runHook();
getPostProcessHook({}, {}, {})('input_code', 'fake_file', {}, {instrument: null});
expect(transformMock).lastCalledWith(
expect.any(String),
expect.objectContaining({
plugins: [],
}),
);
});

it('uses commonjs module transform when allowSyntheticDefaultImports=true', () => {
const transformMock = require.requireMock('babel-core').transform;

runHook({allowSyntheticDefaultImports: true});
expect(transformMock).lastCalledWith(
expect.any(String),
expect.objectContaining({plugins: ['transform-es2015-modules-commonjs']}),
);
});

it('doesn`t accumulate commonjs module transforms on consecutive calls', () => {
const transformMock = require.requireMock('babel-core').transform;
const tsCompilerOptions = {allowSyntheticDefaultImports: true};
const tsJestConfig = {
babelConfig: {
plugins: [],
},
skipBabel: false,
};

runHook(tsCompilerOptions, {}, tsJestConfig);
runHook(tsCompilerOptions, {}, tsJestConfig);

expect(transformMock).lastCalledWith(
expect.any(String),
expect.objectContaining({
plugins: ['transform-es2015-modules-commonjs'],
}),
);
});
});

0 comments on commit 36875a4

Please sign in to comment.