Skip to content
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

jest 24 with babel 6 and babel-jest 23 #8230

Closed
jochakovsky opened this issue Mar 28, 2019 · 13 comments
Closed

jest 24 with babel 6 and babel-jest 23 #8230

jochakovsky opened this issue Mar 28, 2019 · 13 comments

Comments

@jochakovsky
Copy link

🐛 Bug Report

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.

To Reproduce

Expected behavior

Jest 24 should transpile test and src files using Babel 6

Link to repl or repo (highly encouraged)

https://github.com/jochakovsky/Jest-24-Babel-6-preset-bug

Run 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 
@SimenB
Copy link
Member

SimenB commented Apr 3, 2019

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?

@SimenB
Copy link
Member

SimenB commented Apr 3, 2019

@jochakovsky workaround is to add a jest.config.js file

module.exports = {
  transform: {
    '^.+\\.[jt]sx?$': require.resolve('babel-jest')
  }
};

@jochakovsky
Copy link
Author

@SimenB Thank you, the workaround works for me!

@satsuya0114
Copy link

satsuya0114 commented May 30, 2019

@jochakovsky workaround is to add a jest.config.js file

module.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 !

@lin-credible
Copy link

lin-credible commented Jul 2, 2019

package.json

  ......
  "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"
    ......

.babelrc

{
  "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"]
              }
            ]
          }
        ]
      ]
    }
  }
}

Run 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)

Run 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.

@nathanarritt
Copy link

nathanarritt commented Jul 9, 2019

@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" } } }

@pkit
Copy link

pkit commented Nov 21, 2019

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

@ahmad-reza619
Copy link

ahmad-reza619 commented Mar 2, 2020

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 👌

@StioDevolpper
Copy link

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 .. !

@mbdebian
Copy link

mbdebian commented Jul 5, 2020

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.

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

github-actions bot commented May 2, 2022

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants