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

Incorrect transform for multiple projects with different transform in the same process #5855

Closed
amosyuen opened this issue Mar 22, 2018 · 17 comments

Comments

@amosyuen
Copy link

amosyuen commented Mar 22, 2018

Do you want to request a feature or report a bug?
Report a bug

What is the current behavior?
Jest uses the same transform for projects with different transform behavior if they run in the same process.

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

Repository https://github.com/amosyuen/jest-projects-transform-error.
Running yarn test succeeds. But running yarn test --runInBand will fail. Error message indicates that the incorrect babel transform was used.

What is the expected behavior?
Correct transform for the project should be used. Both yarn test and yarn test --runInBand should succeed.

Please provide your exact Jest configuration
https://github.com/amosyuen/jest-projects-transform-error/blob/master/jest.config.js

Run npx envinfo --preset jest in your project directory and paste the
results here

Environment:
OS: Windows 10
Node: 8.9.4
Yarn: 1.5.1
npm: 5.6.0
Watchman: Not Found
Xcode: N/A
Android Studio: Version 3.0.0.0 AI-171.4443003

@thymikee
Copy link
Collaborator

The issue with your configuration is using custom transformer, which is not fully functional (e.g. doesn't have getCacheKey implemented) – that's probably why you have issues with mixing cache.

You should rather stick with default transformer and use moduleNameMapper to alias react-native to react-native-web:

const os = require("os");
const path = require("path");

const nativeConfig = {
  displayName: "native",
  preset: "jest-expo",
  testMatch: ['**/*.test.native.js']
};
const webConfig = {
  displayName: "web",
  moduleNameMapper: {
    "react-native": "react-native-web"
  },
  setupFiles: ["jest-canvas-mock"],
  testMatch: ['**/*.test.web.js']
};

module.exports = {
  projects: [nativeConfig, webConfig]
};

@amosyuen
Copy link
Author

That simple example doesn't quite represent our real transformers, there's many other differences between the transformers for our web and native config besides just module mapping, it's not possible to do them using a default transformer and other options.

@amosyuen
Copy link
Author

I tried modifying the cache key, and the problem persists. Is this the right way? amosyuen/jest-projects-transform-error@e75baeb

@amosyuen
Copy link
Author

As far as I can tell, modifying the getCacheKey() function doesn't affect this problem. I even tried setting the cache key to a large random string, same problem.

I think the problem is that the transformer is being cached between tests which require a different transformer.

@amosyuen
Copy link
Author

Pleas re-open until it's actually solved.

@thymikee thymikee reopened this May 29, 2018
@viknes-interana
Copy link

I can confirm this issue still occurs. I have a similar use case where I have 2 projects - one that runs just unit tests and another that runs e2e screenshot tests (uses jest-image-snapshot) and I want to mock out scss files when my unit tests are run but use a custom transformer for scss files when e2e screenshot tests are run. When there are a large number of tests, the transformed result of 1 project gets used in the other project even though they both have different cache keys.
unit.test.config.js

moduleNameMapper: {
    '\\.(css|scss)$': 'identity-obj-proxy',
},
transform: {
    '\\.jsx?$': 'babel-jest',
},

screenshot.test.config.js

moduleNameMapper: {
    '\\.css$': 'identity-obj-proxy',
},
transform: {
    '\\.scss$': '<rootDir>/jest/scssTransformer.js',
    '\\.jsx?$': 'babel-jest',
},

@SimenB
Copy link
Member

SimenB commented Dec 29, 2018

This should be fixed in #7186, can you install jest@beta (currently resolving to jest@24.0.0-alpha.9) and see if the issue is fixed?

@viknes-interana
Copy link

viknes-interana commented Dec 30, 2018

I tried upgrading to the beta package. But I ran in to some other issues before I can even get to testing this specific bug.
First error I got was

TypeError: Cannot read property 'concat' of undefined
    at Object.<anonymous> (/myproject/node_modules/jest-resolve/build/isBuiltinModule.js:29:27)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/myproject/node_modules/jest-resolve/build/index.js:25:47)
    at Module._compile (module.js:570:32)

which I got past with a temporary hack (adding a (foo || []) in front of the .concat by manually editing the troubled node_modules file.
But then I ran in to this when I tried to run my tests

Plugin/Preset files are not allowed to export objects, only functions. In /myproject/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 (native)
      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)

I am on babel@6.26.0 but seems like it is expecting babel 7? I am not sure. I tried upgrading to babel 7 but there are some incompatible packages that needed upgrading as well. If I ever upgrade to babel 7, I will test it out and let you know. In the meantime, the approach in the linked ticket seems promising and I'm confident it will fix this bug. Thank you for looking in to this!

@SimenB
Copy link
Member

SimenB commented Dec 30, 2018

Yes, the new version of Jest requires Babel 7.

The first error is an unintended breaking change from #7408, wanna send a PR to fix it?

@necolas
Copy link

necolas commented Dec 30, 2018

I'm also seeing TypeError: Cannot read property 'concat' of undefined. I installed the beta to resolve various issues I encountered while upgrading to Babel 7. It seemed to be working last week, but when I ran a fresh yarn install it started breaking; perhaps yarn resolved a different dependency tree.

@SimenB
Copy link
Member

SimenB commented Dec 30, 2018

Yeah, the concat bug is pretty bad, I'll put up a PR.

It'll work in node 9, 10 and 11, you probably ran one of those?

@necolas
Copy link

necolas commented Dec 30, 2018

I found the reason for the TypeError in my case: running an older version of Node.js 8.

When I upgraded eslint it failed to install and printed this error:

error eslint@5.11.1: The engine "node" is incompatible with this module. Expected version "^6.14.0 || ^8.10.0 || >=9.10.0".
error Found incompatible module
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

After upgrading Node.js, Jest started running without errors too. You may want to update the engines field in your package.json files to match the last version of Node.js that can run the jest beta.

@SimenB
Copy link
Member

SimenB commented Dec 30, 2018

PR: #7565

@thymikee
Copy link
Collaborator

thymikee commented Jan 7, 2019

Closing as #7565 is merged now (and hopefully fixes the problem, please let us know).

@thymikee thymikee closed this as completed Jan 7, 2019
@SimenB
Copy link
Member

SimenB commented Jan 7, 2019

Closed by #7186 really, #7565 just fixed a regression introduced for older nodes in #7408

@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.
Projects
None yet
Development

No branches or pull requests

5 participants