Skip to content

[Bug]: babelConfig: .cjs file extension not working #3335

@JumpLink

Description

@JumpLink

Version

27.1.3

Steps to reproduce

  1. Create a jest config file with
module.exports = {
  ...
  globals: {
    "ts-jest": {
      babelConfig: "babel.config.cjs",
    },
  },
...
}
  1. Create a babel.config.cjs file with some default babel settings.
  2. Make sure the jest config is working with babel.config.js (without .cjs)

Expected behavior

It should behave the same as if the filename is babel.config.js (.js instead of .cjs) and use the corresponding babel config

Actual behavior

All tests fail with the following error message:

Debug log

  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    SyntaxError: JSON5: invalid character 'm' at 1:1

      at syntaxError (.yarn/cache/json5-npm-2.1.3-b71ec6bcca-b2de57a665.zip/node_modules/json5/lib/parse.js:1083:17)
      at invalidChar (.yarn/cache/json5-npm-2.1.3-b71ec6bcca-b2de57a665.zip/node_modules/json5/lib/parse.js:1028:12)
      at Object.value (.yarn/cache/json5-npm-2.1.3-b71ec6bcca-b2de57a665.zip/node_modules/json5/lib/parse.js:287:15)
      at lex (.yarn/cache/json5-npm-2.1.3-b71ec6bcca-b2de57a665.zip/node_modules/json5/lib/parse.js:78:42)
      at Object.parse (.yarn/cache/json5-npm-2.1.3-b71ec6bcca-b2de57a665.zip/node_modules/json5/lib/parse.js:25:

Additional context

Workaround:

Require the config file directly in the config:

module.exports = {
  ...
  globals: {
    "ts-jest": {
      babelConfig: require("./babel.config.cjs"),
    },
  },
...
}

Environment

System:
    OS: Linux 5.16 Fedora Linux 35 (Workstation Edition)
    CPU: (8) x64 Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
  Binaries:
    Node: 16.14.0 - ~/.nvm/versions/node/v16.14.0/bin/node
    Yarn: 3.2.0 - ~/.nvm/versions/node/v16.14.0/bin/yarn
    npm: 8.3.1 - ~/.nvm/versions/node/v16.14.0/bin/npm

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions