Skip to content

Custom Jest transformations are applied after default transformations #8654

Closed
@JereKuusela

Description

@JereKuusela

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions