Skip to content

Commit

Permalink
Fix transformer signature in babel-jest (#7945)
Browse files Browse the repository at this point in the history
* Assigned createTransformer function only to the transformer exported by babel-jest

* Improved typing

* Updated changelog

* Added e2e test
  • Loading branch information
rubennorte committed Feb 21, 2019
1 parent 46bf566 commit bc892ed
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -47,7 +47,7 @@
- `[@jest/transform]`: New package extracted from `jest-runtime` ([#7915](https://github.com/facebook/jest/pull/7915))
- `[babel-plugin-jest-hoist]`: Migrate to TypeScript ([#7898](https://github.com/facebook/jest/pull/7898))
- `[@jest/core]` Create new package, which is `jest-cli` minus `yargs` and `prompts` ([#7696](https://github.com/facebook/jest/pull/7696))
- `[@jest/transform]`: Migrate to TypeScript ([#7918](https://github.com/facebook/jest/pull/7918))
- `[@jest/transform]`: Migrate to TypeScript ([#7918](https://github.com/facebook/jest/pull/7918), [#7945](https://github.com/facebook/jest/pull/7945))
- `[docs]` Add missing import to docs ([#7928](https://github.com/facebook/jest/pull/7928))
- `[jest-resolve-dependencies]`: Migrate to TypeScript ([#7922](https://github.com/facebook/jest/pull/7922))
- `[expect]`: Migrate to TypeScript ([#7919](https://github.com/facebook/jest/pull/7919))
Expand Down
15 changes: 15 additions & 0 deletions e2e/__tests__/transform.test.js
Expand Up @@ -57,6 +57,21 @@ describe('babel-jest ignored', () => {
});
});

describe('babel-jest with manual transformer', () => {
const dir = path.resolve(__dirname, '..', 'transform/babel-jest-manual');

beforeEach(() => {
run('yarn', dir);
});

it('runs transpiled code', () => {
// --no-cache because babel can cache stuff and result in false green
const {json} = runWithJson(dir, ['--no-cache']);
expect(json.success).toBe(true);
expect(json.numTotalTests).toBeGreaterThanOrEqual(1);
});
});

// babel-jest is automatically linked at the root because it is a workspace now
// a way to test this in isolation is to move the test suite into a temp folder
describe('no babel-jest', () => {
Expand Down
15 changes: 15 additions & 0 deletions e2e/transform/babel-jest-manual/__tests__/babelJest.test.js
@@ -0,0 +1,15 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

require('../foo');

it('strips flowtypes using babel-jest and .babelrc', () => {
const a: string = 'a';
expect(a).toBe('a');
});
10 changes: 10 additions & 0 deletions e2e/transform/babel-jest-manual/foo.js
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const someFunction = (): null => null;

module.exports = someFunction();
11 changes: 11 additions & 0 deletions e2e/transform/babel-jest-manual/package.json
@@ -0,0 +1,11 @@
{
"dependencies": {
"@babel/preset-flow": "^7.0.0"
},
"jest": {
"testEnvironment": "node",
"transform": {
"\\.js$": "<rootDir>/transformer.js"
}
}
}
13 changes: 13 additions & 0 deletions e2e/transform/babel-jest-manual/transformer.js
@@ -0,0 +1,13 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const {createTransformer} = require('babel-jest');

module.exports = createTransformer({
presets: ['@babel/preset-flow'],
root: __dirname,
});
31 changes: 31 additions & 0 deletions e2e/transform/babel-jest-manual/yarn.lock
@@ -0,0 +1,31 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@babel/helper-plugin-utils@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.0.0.tgz#bbb3fbee98661c569034237cc03967ba99b4f250"
integrity sha512-CYAOUCARwExnEixLdB6sDm2dIJ/YgEAKDM1MOeMeZu9Ld/bDgVo8aiWrXwcY7OBh+1Ea2uUcVRcxKk0GJvW7QA==

"@babel/plugin-syntax-flow@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-flow/-/plugin-syntax-flow-7.0.0.tgz#70638aeaad9ee426bc532e51523cff8ff02f6f17"
integrity sha512-zGcuZWiWWDa5qTZ6iAnpG0fnX/GOu49pGR5PFvkQ9GmKNaSphXQnlNXh/LG20sqWtNrx/eB6krzfEzcwvUyeFA==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-transform-flow-strip-types@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-flow-strip-types/-/plugin-transform-flow-strip-types-7.0.0.tgz#c40ced34c2783985d90d9f9ac77a13e6fb396a01"
integrity sha512-WhXUNb4It5a19RsgKKbQPrjmy4yWOY1KynpEbNw7bnd1QTcrT/EIl3MJvnGgpgvrKyKbqX7nUNOJfkpLOnoDKA==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-flow" "^7.0.0"

"@babel/preset-flow@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/preset-flow/-/preset-flow-7.0.0.tgz#afd764835d9535ec63d8c7d4caf1c06457263da2"
integrity sha512-bJOHrYOPqJZCkPVbG1Lot2r5OSsB+iUOaxiHdlOeB1yPWS6evswVHwvkDLZ54WTaTRIk89ds0iHmGZSnxlPejQ==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-transform-flow-strip-types" "^7.0.0"
15 changes: 10 additions & 5 deletions packages/babel-jest/src/index.ts
Expand Up @@ -26,7 +26,6 @@ const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul');
// Narrow down the types
interface BabelJestTransformer extends Transformer {
canInstrument: true;
createTransformer: (options?: TransformOptions) => BabelJestTransformer;
}

const createTransformer = (
Expand Down Expand Up @@ -64,9 +63,8 @@ const createTransformer = (
return babelConfig;
}

const transformer: BabelJestTransformer = {
return {
canInstrument: true,
createTransformer,
getCacheKey(
fileData,
filename,
Expand Down Expand Up @@ -130,8 +128,15 @@ const createTransformer = (
return src;
},
};
};

return transformer;
const transformer: BabelJestTransformer & {
createTransformer: (options?: TransformOptions) => BabelJestTransformer;
} = {
...createTransformer(),
// Assigned here so only the exported transformer has `createTransformer`,
// instead of all created transformers by the function
createTransformer,
};

export = createTransformer();
export = transformer;

0 comments on commit bc892ed

Please sign in to comment.