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

ts-jest encountered an unexpected token with camelcase #103

Closed
magiclen opened this issue Dec 14, 2022 · 2 comments
Closed

ts-jest encountered an unexpected token with camelcase #103

magiclen opened this issue Dec 14, 2022 · 2 comments

Comments

@magiclen
Copy link

magiclen commented Dec 14, 2022

After version 7, camelcase uses export default to export the camelCase function. This change causes Jest failed to run.

To reproduce:

package.json

{
  "name": "a",
  "version": "0.0.0",
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "@types/jest": "^29.2.4",
    "jest": "^29.3.1",
    "ts-jest": "^29.0.3",
    "typescript": "^4.9.4"
  },
  "dependencies": {
    "camelcase": "^7.0.1"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "commonjs", 
    "rootDir": "./src",                     
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
  }
}

jest.config.json

{
    "preset": "ts-jest",
    "testEnvironment": "node"
}

src/lib.ts

import camelcase from 'camelcase';

export const f = () => {
    return camelcase('a-b');
};

tests/tests.test.ts

import { f } from '../src/lib'

it("should success", () => {
    expect(f()).toBe('aB');
});

Run npm i and run npm test. The result is

$ npm test

> a@0.0.0 test
> jest

 FAIL  tests/test.test.ts
  ● 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

I would like to know a good way to solve this.

@sindresorhus
Copy link
Owner

This is not the place to ask for Jest support. Try https://github.com/facebook/jest/issues

@sindresorhus sindresorhus closed this as not planned Won't fix, can't repro, duplicate, stale Dec 14, 2022
@magiclen
Copy link
Author

I found a way to solve this.

Modify jest.config.json

{
    ...

    "transform": {
        "\\.m?jsx?$": [
            "babel-jest",
            {
                "plugins": [
                    "@babel/plugin-transform-modules-commonjs"
                ]
            }
        ]
    },
    "transformIgnorePatterns": [
        "/node_modules/?!(camelcase)/"
    ]
}

And install @babel/plugin-transform-modules-commonjs to transform the camelcase ESM package into CJS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants