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

transformIgnorePatterns setting doesn't work when using a .babelrc instead of babel.config.js #10256

Closed
amclin opened this issue Jul 7, 2020 · 5 comments · Fixed by gzuidhof/zarr.js#105

Comments

@amclin
Copy link

amclin commented Jul 7, 2020

🐛 Bug Report

Jest fails to respect the transformIgnorePatterns setting in package.json when when babel is configured using .babelrc. However, if .babelrc is converted to babel.config.js then Jest runs just fine.

To Reproduce

Steps to reproduce the behavior:

  1. Setup a project using a .babelrc
{
  "presets": [
    [
      "next/babel",
      {
        // This tells the preset to look for browserslist in package.json
        // See: https://babeljs.io/docs/en/babel-preset-env#browserslist-integration
        "useBuiltIns": "entry"
      }
    ]
  ],
  "plugins": [
    "graphql-tag"
  ],
  "env": {
    "production": {
      "plugins": [
        "jsx-remove-data-test-id"
      ]
    }
  }
}
  1. Include an npm package that needs transpiling
  2. Setup Jest and specify the package be transformed using the transformIgnorePattern in package.json
  "jest": {
    "setupFilesAfterEnv": [
      "jest-expect-message"
    ],
    "collectCoverage": true,
    "collectCoverageFrom": [
      "**/*.{js,jsx,ts,tsx}"
    ],
    "coverageReporters": [
      "clover",
      "json",
      "json-summary",
      "lcov",
      "text"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 44,
        "functions": 38,
        "lines": 29,
        "statements": 29
      }
    },
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/html/sites/",
      "/out/",
      "/.next/",
      "/.github/"
    ],
    "coveragePathIgnorePatterns": [
      "<rootDir>/node_modules/",
      "<rootDir>/coverage/",
      "<rootDir>/sql/",
      "<rootDir>/html/",
      "/__mocks__/",
      "/out/",
      "/.next/",
      "/.github/"
    ],
    "transform": {
      "^.+\\.[t|j]sx?$": "babel-jest"
    },
    "transformIgnorePatterns": [
      "node_modules/(?!(@bau-design/components-react|@bau-design)/)"
    ]
  },
  1. run jest
    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:

    /Users/amclin/Documents/git/anthonymclin.com/node_modules/@bau-design/components-react/src/components/index.js:1
    export { Card } from './molecules/card'
    ^^^^^^

    SyntaxError: Unexpected token 'export'
        at compileFunction (<anonymous>)

      4 | import PropTypes from 'prop-types'
      5 | import parse from 'parse-md'
    > 6 | import { Card } from '@bau-design/components-react'
        | ^

Expected behavior

Expect Jest to work the same for either a .babelrc or babel.config.js. If the .babelrc is replaced with the equivalent babel.config.js, then things magically start working:

module.exports = {
  presets: [
    [
      'next/babel',
      {
        // This tells the preset to look for browserslist in package.json
        // See: https://babeljs.io/docs/en/babel-preset-env#browserslist-integration
        useBuiltIns: 'entry'
      }
    ]
  ],
  plugins: ['graphql-tag'],
  env: {
    production: {
      plugins: ['jsx-remove-data-test-id']
    }
  }
}

Link to repl or repo (highly encouraged)

envinfo

  System:
    OS: macOS 10.15.5
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
    Memory: 869.08 MB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 13.6.0 - ~/.nvm/versions/node/v13.6.0/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.4 - ~/Documents/git/anthonymclin.com/node_modules/.bin/npm
  Managers:
    Homebrew: 2.4.1 - /usr/local/bin/brew
    Maven: 3.6.3 - /usr/local/bin/mvn
    pip3: 19.0.3 - /usr/bin/pip3
    RubyGems: 3.0.3 - /usr/bin/gem
  Utilities:
    Make: 3.81 - /usr/bin/make
    GCC: 4.2.1 - /usr/bin/gcc
    Git: 2.24.3 - /usr/bin/git
    Clang: 1103.0.32.62 - /usr/bin/clang
    Subversion: 1.10.4 - /usr/bin/svn
  Servers:
    Apache: 2.4.41 - /usr/sbin/apachectl
  Virtualization:
    Docker: 19.03.8 - /usr/local/bin/docker
    Parallels: 15.1.2 - /usr/local/bin/prlctl
  SDKs:
    iOS SDK:
      Platforms: iOS 13.5, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
  IDEs:
    Nano: 2.0.6 - /usr/bin/nano
    Vim: 8.1 - /usr/bin/vim
    Xcode: 11.5/11E608c - /usr/bin/xcodebuild
  Languages:
    Bash: 3.2.57 - /bin/bash
    Perl: 5.18.4 - /usr/bin/perl
    PHP: 7.3.11 - /usr/bin/php
    Python: 2.7.16 - /usr/bin/python
    Python3: 3.7.3 - /usr/bin/python3
    Ruby: 2.6.3 - /usr/bin/ruby
  Databases:
    SQLite: 3.28.0 - /usr/bin/sqlite3
  Browsers:
    Brave Browser: 83.1.10.97
    Chrome: 83.0.4103.116
    Safari: 13.1.1
@ChiefORZ
Copy link

ChiefORZ commented Sep 4, 2020

thanks! saved me a lot of time debugging

thebestnom added a commit to neo4j-contrib/neovis.js that referenced this issue Nov 2, 2020
 (changed .babelrc because jestjs/jest#10256)
@josokinas
Copy link

josokinas commented Mar 8, 2021

Thanks!

Can't believe this is still an issue!

Was causing me a lot of headache today... 😞

EDIT: probably good to read README: https://github.com/facebook/jest#using-babel

@leandrooriente
Copy link

Thanks!

@SimenB
Copy link
Member

SimenB commented Mar 17, 2021

This (transpiling node_modules) is documented by babel: https://babeljs.io/docs/en/configuration#whats-your-use-case

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

Successfully merging a pull request may close this issue.

5 participants