-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[babel-jest] Feature: support expanded .babelrc.js
api
#5324
Comments
If it helps, here's a transform file that looks like it works for our case, though we'll probably need more tweaks: /**
* A variant of babel-jest that will process some files with babel@6 and some with babel@7
* This code is based on babel-jest: https://github.com/facebook/jest/blob/master/packages/babel-jest/src/index.js
* TODO: Support instrument
*/
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const util = require('util');
const babel6 = require('babel-core');
const babel7 = require('@babel/core');
const jestPreset = require('babel-preset-jest');
const thisFileContent = fs.readFileSync(__filename);
function isBabel6Filename(filename) {
return filename.startsWith(modulesPath);
}
function applyJestOptions(babelOptions) {
babelOptions.presets = (babelOptions.presets || []).concat(jestPreset);
babelOptions.retainLines = true;
babelOptions.sourceMaps = 'inline';
}
const modulesPath = path.join(__dirname, 'node_modules');
const babel6Options = {
presets: ['react-native'],
};
applyJestOptions(babel6Options);
function getBabel7Options(filename) {
const babel7Options = babel7.loadOptions({
babelrc: true,
filename,
envName: 'test',
});
applyJestOptions(babel7Options);
return babel7Options;
}
function processBabel6(src, filename, config) {
if (!babel6.util.canCompile(filename, config.moduleFileExtensions.map(x => `.${x}`))) {
return src;
}
return babel6.transform(src, babel6Options).code;
}
function processBabel7(src, filename) {
const babel7Options = getBabel7Options(filename);
const result = babel7.transform(src, babel7Options);
return result ? result.code : src;
}
function process(src, filename, config, transformOptions) {
if (isBabel6Filename(filename)) {
return processBabel6(src, filename, config);
} else {
return processBabel7(src, filename);
}
}
function getCacheKey(fileData, filename, configString, {instrument}) {
return crypto
.createHash('md5')
.update(thisFileContent)
.update('\0', 'utf8')
.update(fileData)
.update('\0', 'utf8')
.update(filename)
.update('\0', 'utf8')
.update(configString)
.update('\0', 'utf8')
// util.inspect() shows more things than JSON.stringify().
.update(util.inspect(
isBabel6Filename(filename) ? babel6Options : getBabel7Options(filename),
{ depth: null, colors: false }))
.update('\0', 'utf8')
.update(instrument ? 'instrument' : '')
.digest('hex');
}
module.exports = { getCacheKey, process }; |
@simonbuchan That works, thank you! I added your tweaked transform code as // jest.config.js
const transform = {
".*": "./jest.transform.js"
}
module.exports = {transform} |
There's an incoming API in Babel 7 we could use (it documents the API mentioned in the OP): babel/babel#7472 Some way of serializing the return value there into something that is not lossy would be great |
This is fixed in Jest 24 which uses Babel's own API (the one I linked above) to load Babel config |
- add 1 simple test for existence of canvas and instance methods - change test script to use jest, and move prev test to test:pub - change CI to run test and test:pub - configure jest and enzyme for testing usage - make jest importable too - add .babelrc to configure babel-jest - can't use .babelrc.js as babel-jest@23 doesn't support it - jestjs/jest#5324 - can't use .babelrc for babel-loader because babel-loader@6 has some bugs with it and @7 doesn't support webpack@1 - babel/babel-loader#552 - use jest instead of ava mainly because there's less to configure / install (directly at least, still a lot indirectly), it's popular / well-supported in React community, and supports configuration outside of package.json - see #33 for some more details (deps): add jest, enzyme, and supporting deps to devDeps - add babel-jest@23 for Babel 6 support - and configure it for .js files due to a jest bug - add canvas-prebuilt@1 to support jest's jsdom v11 - canvas is used by jsdom for, well, canvas interactions - add enzyme-adapter-react-16 to configure Enzyme with React 16 - upgrade to React 16 in devDeps bc adapter-react-15 requires react-test-renderer and no drawbacks in upgrading
- add 1 simple test for existence of canvas and instance methods - change test script to use jest, and move prev test to test:pub - change CI to run test and test:pub - configure jest and enzyme for testing usage - make jest importable too - add .babelrc to configure babel-jest - can't use .babelrc.js as babel-jest@23 doesn't support it - jestjs/jest#5324 - can't use .babelrc for babel-loader because babel-loader@6 has some bugs with it and @7 doesn't support webpack@1 - babel/babel-loader#552 - use jest instead of ava mainly because there's less to configure / install (directly at least, still a lot indirectly), it's popular / well-supported in React community, and supports configuration outside of package.json - see #33 for some more details (deps): add jest, enzyme, and supporting deps to devDeps - add babel-jest@23 for Babel 6 support - and configure it for .js files due to a jest bug - add canvas-prebuilt@1 to support jest's jsdom v11 - canvas is used by jsdom for, well, canvas interactions - add enzyme-adapter-react-16 to configure Enzyme with React 16 - upgrade to React 16 in devDeps bc adapter-react-15 requires react-test-renderer and no drawbacks in upgrading
- add 1 simple test for existence of canvas and instance methods - (pkg): test code shouldn't be in the package, just source - currently preferring to keep source and tests co-located - change test script to use jest, and move prev test to test:pub - change CI to run test and test:pub - configure jest and enzyme for testing usage - make jest importable too - add .babelrc to configure babel-jest - can't use .babelrc.js as babel-jest@23 doesn't support it - jestjs/jest#5324 - can't use .babelrc for babel-loader because babel-loader@6 has some bugs with it and @7 doesn't support webpack@1 - babel/babel-loader#552 - use jest instead of ava mainly because there's less to configure / install (directly at least, still a lot indirectly), it's popular / well-supported in React community, and supports configuration outside of package.json - see #33 for some more details (deps): add jest, enzyme, and supporting deps to devDeps - add babel-jest@23 for Babel 6 support - and configure it for .js files due to a jest bug - add canvas-prebuilt@1 to support jest's jsdom v11 - canvas is used by jsdom for, well, canvas interactions - add enzyme-adapter-react-16 to configure Enzyme with React 16 - upgrade to React 16 in devDeps bc adapter-react-15 requires react-test-renderer and no drawbacks in upgrading
- add 1 simple test for existence of canvas and instance methods - (pkg): test code shouldn't be in the package, just source - currently preferring to keep source and tests co-located - change test script to use jest, and move prev test to test:pub - (ci): change CI to run test and test:pub - configure jest and enzyme for testing usage - make jest importable too - add .babelrc to configure babel-jest - can't use .babelrc.js as babel-jest@23 doesn't support it - jestjs/jest#5324 - can't use .babelrc for babel-loader because babel-loader@6 has some bugs with it and @7 doesn't support webpack@1 - babel/babel-loader#552 - use jest instead of ava mainly because there's less to configure / install (directly at least, still a lot indirectly), it's popular / well-supported in React community, and supports configuration outside of package.json - see #33 for some more details (deps): add jest, enzyme, and supporting deps to devDeps - add babel-jest@23 for Babel 6 support - and configure it for .js files due to a jest bug - add canvas-prebuilt@1 to support jest's jsdom v11 - canvas is used by jsdom for, well, canvas interactions - add enzyme-adapter-react-16 to configure Enzyme with React 16 - upgrade to React 16 in devDeps bc adapter-react-15 requires react-test-renderer and no drawbacks in upgrading
- add 1 simple test for existence of canvas and instance methods - (pkg): test code shouldn't be in the package, just source - currently preferring to keep source and tests co-located - change test script to use jest, and move prev test to test:pub - (ci): change CI to run test and test:pub - configure jest and enzyme for testing usage - make jest importable too - add .babelrc to configure babel-jest - can't use .babelrc.js as babel-jest@23 doesn't support it - jestjs/jest#5324 - can't use .babelrc for babel-loader because babel-loader@6 has some bugs with it and @7 doesn't support webpack@1 - babel/babel-loader#552 - use jest instead of ava mainly because there's less to configure / install (directly at least, still a lot indirectly), it's popular / well-supported in React community, and supports configuration outside of package.json - see #33 for some more details (deps): add jest, enzyme, and supporting deps to devDeps - add babel-jest@23 for Babel 6 support - and configure it for .js files due to a jest bug - add canvas-prebuilt@1 to support jest's jsdom v11 - canvas is used by jsdom for, well, canvas interactions - add enzyme-adapter-react-16 to configure Enzyme with React 16 - upgrade to React 16 in devDeps bc adapter-react-15 requires react-test-renderer and no drawbacks in upgrading
- ensure that width/height of canvas are trimmed down after drawing a purple rectangle in the center - add test script that uses jest - (ci): change CI to run test and test:pub - configure jest and babel-jest - add coverage/ directory to gitignore - add babel-jest@23 for Babel 6 support - and configure it for .js files due to a jest bug - add .babelrc to configure babel-jest - can't use .babelrc.js as babel-jest@23 doesn't support it - jestjs/jest#5324 - can't use .babelrc for babel-loader (have to duplicate config) because babel-loader@6 has some bugs with it and babel-loader@7 doesn't support webpack@1 - babel/babel-loader#552 (deps): add jest, babel-jest, and canvas-prebuilt to devDeps - add canvas-prebuilt@1 to support jest's jsdom v11 - canvas is used by jsdom for, well, canvas interactions
- ensure that width/height of canvas are trimmed down after drawing a purple rectangle in the center - add test script that uses jest - (ci): change CI to run test and test:pub - configure jest and babel-jest - add coverage/ directory to gitignore - add babel-jest@23 for Babel 6 support - and configure it for .js files due to a jest bug - add .babelrc to configure babel-jest - can't use .babelrc.js as babel-jest@23 doesn't support it - jestjs/jest#5324 - can't use .babelrc for babel-loader (have to duplicate config) because babel-loader@6 has some bugs with it and babel-loader@7 doesn't support webpack@1 - babel/babel-loader#552 (deps): add jest, babel-jest, and canvas-prebuilt to devDeps - add canvas-prebuilt@1 to support jest's jsdom v11 - canvas is used by jsdom for, well, canvas interactions
- ensure that width/height of canvas are trimmed down after drawing a purple rectangle in the center - add test script that uses jest - (ci): change CI to run test and test:pub - configure jest and babel-jest - add coverage/ directory to gitignore - add babel-jest@23 for Babel 6 support - and configure it for .js files due to a jest bug - add .babelrc to configure babel-jest - can't use .babelrc.js as babel-jest@23 doesn't support it - jestjs/jest#5324 - can't use .babelrc for babel-loader (have to duplicate config) because babel-loader@6 has some bugs with it and babel-loader@7 doesn't support webpack@1 - babel/babel-loader#552 (deps): add jest, babel-jest, and canvas-prebuilt to devDeps - add canvas-prebuilt@1 to support jest's jsdom v11 - canvas is used by jsdom for, well, canvas interactions
- ensure that width/height of canvas are trimmed down after drawing a purple rectangle in the center - add test script that uses jest - (ci): change CI to run test and test:pub - configure jest and babel-jest - add coverage/ directory to gitignore - add babel-jest@23 for Babel 6 support - and configure it for .js files due to a jest bug - add .babelrc to configure babel-jest - can't use .babelrc.js as babel-jest@23 doesn't support it - jestjs/jest#5324 - can't use .babelrc for babel-loader (have to duplicate config) because babel-loader@6 has some bugs with it and babel-loader@7 doesn't support webpack@1 - babel/babel-loader#552 (deps): add jest, babel-jest, and canvas-prebuilt to devDeps - add canvas-prebuilt@1 to support jest's jsdom v11 - canvas is used by jsdom for, well, canvas interactions
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
Feature.
What is the current behavior?
.babelrc.js
export is flattened to JSONThis effectively ignores any features you would use
.babelrc.js
for, and shows up as things likeunexpected token 'import'
when the function export is treated as an empty config, oroverrides
are ignored because RegExptest
s are stripped, etc..., so no transforms are run.What is the expected behavior?
only
, newoverrides[].test
etc...), functions (e.g.getModuleId
) or even plugin instances are preserved..babelrc.js
config files can now export a function, which takes an API in order to support caching: Cache configs based on mtime and allow .babelrc.js functions babel/babel#5608In short,
babel-jest
looks like it should be using the newloadOptions()
API in@babel/core
with at least{ babelrc: true, filename, envName: 'test' }
, though it is not documented yet 😟Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
Example
loadOptions()
usage:The text was updated successfully, but these errors were encountered: