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

Debug Jest in react when module alias are defined #7319

Open
mnegahi opened this issue Jul 4, 2019 · 2 comments
Open

Debug Jest in react when module alias are defined #7319

mnegahi opened this issue Jul 4, 2019 · 2 comments

Comments

@mnegahi
Copy link

mnegahi commented Jul 4, 2019

I have a react application which is create by CRA and using typescript. I define several module mapper in tsconfig.json

tsconfig.path.json

    {
      "compilerOptions": {
        "baseUrl": "./src",
        "paths": {
          "@constants/*": ["constants/*"],
          "@components/*": ["components/*"],
          "@grid/*": ["components/Grid/*"],
          "@grid-share/*": ["components/Grid/Share/*"],
          "@utils/*": ["util/*"],
          "@services/*": ["Services/*"]
        }
      },
      "extends": "../tsconfig.json"
    }

then I define same alias in package.json for JEST

     "jest": {
        "snapshotSerializers": [
          "enzyme-to-json/serializer"
        ],
        "moduleNameMapper": {
          "@constants/(.*)": "<rootDir>/src/constants/$1",
          "@utils/(.*)": "<rootDir>/src/util/$1",
          "@grid-share/(.*)": "<rootDir>/src/components/Grid/Share/$1",
          "@grid/(.*)": "<rootDir>/src/components/Grid/$1",
          "@services/(.*)": "<rootDir>/src/Services/$1",
          "@components/(.*)": "<rootDir>/src/components/$1"
        }
      },

when I use yarn test everything is okey. However, I want to debug a test in vs code my VSCode luncher config file is like the following:

    {
      "version": "0.2.0",
      "configurations": [{
        "name": "Debug All Tests",
        "type": "node",
        "request": "launch",
        "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts",
        "args": [
          "test",
          "--runInBand",
          "--no-cache",
          "--watchAll=false"
        ],
        "cwd": "${workspaceRoot}",
        "protocol": "inspector",
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen"
      }, {
        "name": "Debug Current Test",
        "type": "node",
        "request": "launch",
        "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts",
        "args": [
          "test",
          "--runInBand",
          "--no-cache",
          "${fileBasenameNoExtension}",
          "--watchAll=true"
        ],
        "cwd": "${workspaceRoot}",
        "protocol": "inspector",
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen"
      }]
    }

the problem is when I try to debug a test case in VSCode, I have following error

Out of the box, Create React App only supports overriding these Jest options:

• collectCoverageFrom
• coverageReporters
• coverageThreshold
• extraGlobals
• globalSetup
• globalTeardown
• resetMocks
• resetModules
• snapshotSerializers
• watchPathIgnorePatterns.

These options in your package.json Jest configuration are not currently supported by Create React App:

• moduleNameMapper

If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.

is there any way to solve this problem without ejecting??

@danielkcz
Copy link

According to docs the moduleNameMapper is configurable now...

https://create-react-app.dev/docs/running-tests#configuration

@yordis
Copy link

yordis commented Jul 10, 2021

I just tested what @FredyC use package.json/moduleNameMapper to achieve such goal

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

No branches or pull requests

3 participants