Re-opening #8155
I've been unsuccessful at following the documentation for running Jest 24 with Babel 6. Specifically, I'm seeing this error:
Plugin/Preset files are not allowed to export objects, only functions.
If using Yarn, one possible workaround is adding "resolutions": {"babel-jest": "23.6.0"}
to package.json.
yarn install
yarn run test
Jest 24 should transpile test and src files using Babel 6
https://github.com/jochakovsky/Jest-24-Babel-6-preset-bug
npx envinfo --preset jest
$ npx envinfo --preset jest
npx: installed 1 in 3.065s
System:
OS: macOS 10.14.4
CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
Binaries:
Node: 8.11.2 - ~/.nvm/versions/node/v8.11.2/bin/node
Yarn: 1.15.2 - /usr/local/bin/yarn
npm: 6.4.1 - ~/.nvm/versions/node/v8.11.2/bin/npm
npmPackages:
jest: ^24.5.0 => 24.5.0
That's weird, I could have sworn I tested it... #5932 explicitly uses require.resolve
instead of trying to resolve from a user's project.
@arcanis could we do something like
try {
babelJest = Resolver.findNodeModule('babel-jest', {basedir});
} catch (e) {
// ignored
}
if (!babelJest) {
babelJest = require.resolve('babel-jest');
}
options.transform = {
[DEFAULT_JS_PATTERN]: babelJest,
};
instead of just require.resolve
?
@jochakovsky workaround is to add a jest.config.js
file
module.exports = {
transform: {
'^.+\\.[jt]sx?$': require.resolve('babel-jest')
}
};
@SimenB Thank you, the workaround works for me!
@jochakovsky workaround is to add a
jest.config.js
filemodule.exports = { transform: { '^.+\\.[jt]sx?$': require.resolve('babel-jest') } };
@SimenB can you add this to official document ?
I met this issue with babel@6 / jest@24 / babel-jest@23 at the same time
update this setting work fine finally !
......
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"@babel/preset-typescript": "^7.3.3",
"babel": "^6.23.0",
"babel-core": "^6.26.3",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.8.0",
"babel-loader": "^7.1.4",
"babel-plugin-react-transform": "^3.0.0",
"babel-plugin-transform-es3-member-expression-literals": "^6.22.0",
"babel-plugin-transform-es3-property-literals": "^6.22.0",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-register": "^6.26.0",
"babel-runtime": "^6.26.0",
"happypack": "^5.0.0",
"jest": "^24.8.0"
......
{
"presets": ["react", "es2015", "stage-2"],
"plugins": ["transform-es3-member-expression-literals", "transform-es3-property-literals"],
"env": {
"development": {
"plugins": [
[
"react-transform",
{
"transforms": [
{
"transform": "react-transform-hmr",
"imports": ["react"],
"locals": ["module"]
}
]
}
]
]
}
}
}
yarn run test
yarn run v1.15.2
$ jest
FAIL src/__tests__/sum.test.js
● Test suite failed to run
Plugin/Preset files are not allowed to export objects, only functions. In /Users/colintao/paas/code/tke-fe/node_modules/babel-preset-
react/lib/index.js
at createDescriptor (node_modules/@babel/core/lib/config/config-descriptors.js:178:11)
at items.map (node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
at Array.map (<anonymous>)
at createDescriptors (node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
at createPresetDescriptors (node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
at presets (node_modules/@babel/core/lib/config/config-descriptors.js:47:19)
at mergeChainOpts (node_modules/@babel/core/lib/config/config-chain.js:320:26)
at node_modules/@babel/core/lib/config/config-chain.js:283:7
at buildRootChain (node_modules/@babel/core/lib/config/config-chain.js:120:22)
at loadPrivatePartialConfig (node_modules/@babel/core/lib/config/partial.js:85:55)
npx envinfo --preset jest
npx: installed 1 in 0.875s
System:
OS: macOS 10.14.5
CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
Binaries:
Node: 10.16.0 - /usr/local/bin/node
Yarn: 1.15.2 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm
npmPackages:
jest: ^24.8.0 => 24.8.0
@jochakovsky @SimenB :help: the workaround doesn't work for me.
@lin-credible The issue might be that you installed version 24 of babel-jest as opposed to version 23. I just installed version 23 and added the transform to my package.json file and it works as expected.
yarn add babel-jest@^23.6.0 -D
package.json
{
"jest": {
"transform": {
"^.+\\.[jt]sx?$": "<rootDir>/node_modules/babel-jest"
}
}
}
No it does not.
require.resolve('babel-jest')
resolves to 23.6.0
but without "workaround" I'm getting Plugin/Preset files are not allowed to export objects, only functions
and with it I'm getting Requires Babel "^7.0.0-0", but was loaded with "6.26.3".
Like literally adding jest.config.js causes Requires Babel "^7.0.0-0"
immediately
Hi there, i'm using NPM here, and this issue still happens, but my error message is
unexpected token import
using jest@24, babel-jest@23, and babel 6
EDIT:
Solved the issue by downgrade jest to jest@23 👌
using jest 24 with babel 6 and babel-jest 23 with the workaround I'm getting this error Plugin / Preset files are not allowed to export objects only functions .. !
using jest 24 with babel 6 and babel-jest 23 with the workaround I'm getting this error Plugin / Preset files are not allowed to export objects only functions .. !
I got passed that error by adding the following at the end of my babelrc:
"test": [
"jest"
]
This way, my whole babelrc file content looks like this:
{
"presets": [
"env",
"react"
],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread"
],
"test": [
"jest"
]
}
But I can't get passed the following error when trying to write test cases for some React action generators:
FAIL src/tests/actions/expenses.test.js
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/home/site/expensify-app/src/tests/actions/expenses.test.js:8
import { addExpense, editExpense, removeExpense } from '../../actions/expenses'; // Test cases
^
SyntaxError: Unexpected token {
at Runtime._execModule (node_modules/jest-runtime/build/index.js:1179:56)
Apparently, jest doesn't understand ES6 import statement, and I've tried many different things, including downgrading jest, none of them worked.
Most helpful comment
@jochakovsky workaround is to add a
jest.config.js
file