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

babel 7 inconsistent transformation #7021

Closed
chrisblossom opened this issue Sep 21, 2018 · 17 comments
Closed

babel 7 inconsistent transformation #7021

chrisblossom opened this issue Sep 21, 2018 · 17 comments

Comments

@chrisblossom
Copy link
Contributor

🐛 Bug Report

When upgrading to babel 7 some files are not being transformed. I have both babel-core@7.0.0-bridge.0 and babel-jest installed.

Example error message:
screen shot 2018-09-21 at 1 23 08 pm

To Reproduce

https://github.com/chrisblossom/backtrack/tree/babel-7

git clone https://github.com/chrisblossom/backtrack.git
cd backtrack
git checkout babel-7
npm install
npm run test

jest --clearCache sometimes helps some tests pass.

Expected behavior

All tests pass.

Link to repl or repo (highly encouraged)

https://github.com/chrisblossom/backtrack/tree/babel-7
chrisblossom/backtrack@697617c

Run npx envinfo --preset jest

  System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  Binaries:
    Node: 8.11.4 - ~/.nvm/versions/node/v8.11.4/bin/node
    Yarn: 1.9.4 - /usr/local/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v8.11.4/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0
@ahrbil
Copy link

ahrbil commented Sep 30, 2018

I have the same issue

@milesj
Copy link

milesj commented Oct 1, 2018

Try using babel.config.js at the root.

Also, what is the output of npm ls babel-core?

@ahrbil
Copy link

ahrbil commented Oct 1, 2018

running npm ls babel-core returns empty
I have my babel.config.js at the root
I followed the instructions on jest website about getting jest and babel 7 work but still I can't get import statement work on my tests I get the same error as @chrisblossom
instead using require all tests pass

@milesj
Copy link

milesj commented Oct 1, 2018

npm ls babel-core shouldn't return empty if you have it as an explicit dep. Are you using yarn instead?

@ahrbil
Copy link

ahrbil commented Oct 1, 2018

yes I use yarn but same error

@milesj
Copy link

milesj commented Oct 1, 2018

In yarn it's yarn why babel-core.

@ahrbil
Copy link

ahrbil commented Oct 1, 2018

now I get this instead of empty

yarn why v1.7.0
[1/4] Why do we have the module "babel-core"...?
[2/4] Initialising dependency graph...
[3/4] Finding dependency...
[4/4] Calculating file sizes...
=> Found "babel-core@7.0.0-bridge.0"
info Has been hoisted to "babel-core"
info This module exists because it's specified in "devDependencies".
info Disk size without dependencies: "2.5KB"
info Disk size with unique dependencies: "2.5KB"
info Disk size with transitive dependencies: "2.5KB"
info Number of shared dependencies: 0
=> Found "jest-config#babel-core@6.26.3"
info This module exists because "jest#jest-cli#jest-config" depends on it.
info Disk size without dependencies: "118KB"
info Disk size with unique dependencies: "118KB"
info Disk size with transitive dependencies: "118KB"
info Number of shared dependencies: 41
=> Found "jest-runtime#babel-core@6.26.3"
info This module exists because "jest#jest-cli#jest-runtime" depends on it.
info Disk size without dependencies: "118KB"
info Disk size with unique dependencies: "3.92MB"
info Disk size with transitive dependencies: "118KB"
info Number of shared dependencies: 41
=> Found "babel-register#babel-core@6.26.3"
info This module exists because "jest#jest-cli#jest-runtime#babel-core#babel-register" depends on it.
info Disk size without dependencies: "118KB"
info Disk size with unique dependencies: "3.92MB"
info Disk size with transitive dependencies: "7.03MB"
info Number of shared dependencies: 41

@glebcha
Copy link

glebcha commented Oct 3, 2018

Faced exactly the same jest behaviour. Using pure babel-jest or calling createTramsformer with custom config. 20% of tests passed fine while others got undefined options argument in createTransformer.

@michaloo
Copy link

I think the problem is that babel-jest expects babel configuration file babel.config.js to simply return a JS object:

https://github.com/facebook/jest/blob/master/packages/babel-jest/src/index.js#L58

while babel js started to promote a config API which allows to return a function:

https://babeljs.io/docs/en/config-files#config-function-api

and this is what we have in the example repo project:

https://github.com/chrisblossom/backtrack/blob/babel-7/.babelrc.js

@valerie1995
Copy link

valerie1995 commented Oct 16, 2018 via email

@jahed
Copy link

jahed commented Oct 19, 2018

I "fixed" this on my end. In that, I deleted a bunch of stuff that I didn't see a need for -- likely due to my own ignorance. Of course, a proper fix would be nice, but this has worked fine for me so far.

Essentially, hand over Babel's config resolution to Babel and don't try to manually resolve it on Jest's end.

https://github.com/jahed/jest/commit/42075095732f35a5c02a3eb1c36df671a4eb0071

@jahed
Copy link

jahed commented Oct 27, 2018

I've found a better temporary solution to this problem. Install babel-core bridge. Instructions here:

https://github.com/babel/babel-bridge

@chrisblossom
Copy link
Contributor Author

Has anyone found a fix for this? I still am unable to get jest to work with babel 7.

I've tried using babel.config.js and here is the output of npm ls babel-core:

➜ npm ls babel-core
@backtrack/core@0.1.22 /Users/chris/github/backtrack/core
├── babel-core@7.0.0-bridge.0
└─┬ jest@23.6.0
  └─┬ jest-cli@23.6.0
    ├─┬ jest-config@23.6.0
    │ └─┬ babel-core@6.26.3
    │   └─┬ babel-register@6.26.0
    │     └── babel-core@6.26.3
    └─┬ jest-runtime@23.6.0
      └── babel-core@6.26.3

@airato
Copy link

airato commented Nov 29, 2018

@chrisblossom if you change the way how the module is being required here https://github.com/chrisblossom/backtrack/blob/697617c7f499c2c8e4c0dd2b7056c1d070a10fcf/src/stats-file/load-stats-file.test.js#L5
from

const loadStatsFile = () => require('./load-stats-file').loadStatsFile();

to

const loadStatsFile = require('./load-stats-file').loadStatsFile;

and run that test npm test -- --no-cache src/stats-file/load-stats-file.test.js
it would not complain about unknown syntax

Seems like something weird is going on with lazy require...

@chrisblossom
Copy link
Contributor Author

@airato thanks for the reply! Interestingly enough I just came to the same conclusion trying to move to Typescript.

I use the lazy require in combination with resetModules to ensure tests are sandboxed. I'll see if it causes me any issues trying to remove the lazy require.

@chrisblossom
Copy link
Contributor Author

chrisblossom commented Nov 29, 2018

Closing this in favor of #7438.

@github-actions
Copy link

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 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants