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

Custom Jest transformations are applied after default transformations #8654

Comments

@JereKuusela
Copy link

Describe the bug

Custom Jest transformations are applied after default transformations.

Did you try recovering your dependencies?

Yes
npm --version: 6.14.2

Which terms did you search for in User Guide?

transform, jest

Environment

current version of create-react-app: 3.4.0
running from C:\Users*****\AppData\Roaming\npm-cache_npx\2056\node_modules\create-react-app

System:
OS: Windows 10 10.0.18363
CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
Binaries:
Node: 12.2.0 - C:\Program Files\nodejs\node.EXE
Yarn: Not Found
npm: 6.14.2 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: 44.18362.449.0
Internet Explorer: 11.0.18362.1
npmPackages:
react: ^16.12.0 => 16.13.0 (16.2.0)
react-dom: ^16.12.0 => 16.13.0
react-scripts: 3.4.0 => 3.4.0
npmGlobalPackages:
create-react-app: Not Found

Steps to reproduce

  1. Import a text file in a test file: import text from './text.txt'
  2. Log output of the text file: console.log(text) in any test.
  3. Install jest-raw-loader: npm i jest-raw-loader --save-dev
  4. Add loader to package.json:
"jest": {
    "transform": {
      "\\.txt$": "jest-raw-loader"
    }
  }
  1. Run tests

Expected behavior

Console should show contents of the text file.

Actual behavior

Console shows file name of the text file.

Source of the issue

The issue is caused in file createJestConfig.js:

// for object types, extend gracefully
config[key] = Object.assign({}, config[key], overrides[key]);

This causes custom transfomers to be applied after default transfomers. This makes it impossible to apply more specific transformations because the more general transformation has a higher priority,

In this case:

'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': resolve('config/jest/fileTransform.js')

prevents my "\.txt$": "jest-raw-loader" from working.

Possible fix

I would fix this by following change:

// for object types, extend gracefully
config[key] = Object.assign({}, overrides[key], config[key], overrides[key]);

This would give higher priority to custom transformers while still allowing overriding default transformers.

But not 100% sure if this a bug in the first place (I'm not very familiar with Jest configuration).

@stale
Copy link

stale bot commented Jul 18, 2020

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Jul 18, 2020
@stale
Copy link

stale bot commented Jul 25, 2020

This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment