12

I am trying to debug Jest tests in VSCode, but it can't resolve some modules. I set a breakpoint where jest.run is called below is the argv

"--runInBand"
"--no-cache"
"--watchAll=false"
"--config"
"{\"roots\":[\"<rootDir>/src\"],\"collectCoverageFrom\":[\"src/**/*.{js,jsx,ts,tsx}\",\"!src/**/*.d.ts\"],\"setupFiles\":[\"/Users/username/projects/project-name/CSharpProjectName/src/ClientApp/node_modules/react-app-polyfill/jsdom.js\"],\"setupFilesAfterEnv\":[],\"testMatch\":[\"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}\",\"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}\"],\"testEnvironment\":\"jsdom\",\"testRunner\":\"/Users/username/projects/project-name/CSharpProjectName/src/ClientApp/node_modules/jest-circus/runner.js\",\"transform\":{\"^.+\\\\.(js|jsx|mjs|cjs|ts|tsx)$\":\"/Users/username/projects/project-name/CSharpProjectName/src/ClientApp/node_modules/react-scripts/config/jest/babelTransform.js\",\"^.+\\\\.css$\":\"/Users/username/projects/project-name/CSharpProjectName/src/ClientApp/node_modules/react-scripts/config/jest/cssTransform.js\",\"^(?!.*\\\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)\":\"/Users/username/projects/project-name/CSharpProjectName/src/ClientApp/node_modules/react-scripts/config/jest/fileTransform.js\"},\"transformIgnorePatterns\":[\"[/\\\\\\\\]node_modules[/\\\\\\\\].+\\\\.(js|jsx|mjs|cjs|ts|tsx)$\",\"^.+\\\\.module\\\\.(css|sass|scss)$\"],\"modulePaths\":[],\"moduleNameMapper\":{\"^react-native$\":\"react-native-web\",\"^.+\\\\.module\\\\.(css|sass|scss)$\":\"identity-obj-proxy\"},\"moduleFileExtensions\":[\"web.js\",\"js\",\"web.ts\",\"ts\",\"web.tsx\",\"tsx\",\"json\",\"web.jsx\",\"jsx\",\"node\"],\"watchPlugins\":[\"jest-watch-typeahead/filename\",\"jest-watch-typeahead/testname\"],\"resetMocks\":true,\"rootDir\":\"/Users/username/projects/project-name/CSharpProjectName/src/ClientApp\"}"
"--env"
"/Users/username/projects/project-name/CSharpProjectName/src/ClientApp/node_modules/jest-environment-jsdom/build/index.js"

My suspicion is that the value for --env is what is causing the problem.

For reference, I my config files:

jest.config.js

module.exports = {
  transform: {
    "^.+\\.tsx?$": "ts-jest",
  },
  testRegex: "(.*)\\.(test|spec)\\.(jsx?|tsx?)$",
  rootDir: "/Users/username/projects/project-name/CSharpProjectName/src/ClientApp",
  modulePaths: [
    "<rootDir>"
  ],
  moduleDirectories: ["node_modules", "src"],
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
  testEnvironment: "jsdom"
};

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug CRA Tests",
            "type": "node",
            "request": "launch",
            "runtimeExecutable": "${workspaceRoot}/src/ClientApp/node_modules/.bin/react-scripts",
            "args": ["test", "--runInBand", "--no-cache", "--watchAll=false", "--env=jsdom"],
            "cwd": "${workspaceRoot}/src/ClientApp",
            "protocol": "inspector",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "env": { "CI": "true" },
            "disableOptimisticBPs": true
        }
    ]
}

Directory structure:

project-name   
└── CSharpProjectName
    ├── .vscode
    │   ├── tasks.json
    │   ├── launch.json    
    │   └── settings.json 
    ├── Dockerfile
    └── src
        ├── ClientApp
        │   ├── jest.config.js
        │   ├── package-lock.json
        │   ├── package.json
        │   ├── public
        │   │   ├── favicon.ico
        │   │   ├── index.html
        │   │   └── manifest.json
        │   ├── src
        │   │   ├── App.test.tsx
        │   │   ├── App.tsx
        │   │   ├── components
        │   │   │   ├── Home.tsx
        │   │   │   ├── Layout.tsx
        │   │   │   └── NavMenu.tsx
        │   │   ├── index.tsx
        │   │   ├── react-app-env.d.ts
        │   │   └── registerServiceWorker.ts
        │   └── tsconfig.json
        ├── Program.cs
        ├── Startup.cs
        ├── CSharpProjectName.csproj
        └── appsettings.json

And I open VS Code from the CSharpProjectName directory.

EDIT:

Tried using cross-env as the executable in launch.json. Also did not work:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Cross Env Debug CRA Tests",
            "type": "node",
            "request": "launch",
            "runtimeExecutable": "${workspaceRoot}/src/ClientApp/node_modules/.bin/cross-env",
            "args": ["CI=true", "${workspaceRoot}/src/ClientApp/node_modules/.bin/react-scripts", "test", "--env=jsdom"],
            "cwd": "${workspaceRoot}/src/ClientApp",
            "protocol": "inspector",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "disableOptimisticBPs": true
        }
    ]
}
rstackhouse
  • 2,238
  • 24
  • 28
  • I have looked at https://stackoverflow.com/questions/66406512/jest-cannot-find-module and it did not help. – rstackhouse Dec 15 '21 at 15:35
  • 1
    Also looked at https://stackoverflow.com/questions/48887321/vscode-jest-extension-doesnt-work-properly when I couldn't get the VS Code Jest extension to work either. Tried a clean VS Code install. No dice. – rstackhouse Dec 15 '21 at 15:36
  • Have read through this: https://create-react-app.dev/docs/debugging-tests/ – rstackhouse Dec 15 '21 at 15:36
  • What is the output when you run it via the command line? – Sanil Khurana Dec 15 '21 at 15:38
  • `$ node_modules/.bin/react-scripts test --runInBand --no-cache --watchAll=false` works – rstackhouse Dec 16 '21 at 03:45
  • `node_modules/.bin/cross-env CI=true node_modules/.bin/react-scripts test —env=jsdom` works – rstackhouse Dec 16 '21 at 04:15
  • Also fails: ```{ "name": "Cross Env Debug CRA Tests", "type": "node", "request": "launch", "runtimeExecutable": "${workspaceRoot}/src/ClientApp/node_modules/.bin/cross-env", "args": ["CI=true", "${workspaceRoot}/src/ClientApp/node_modules/.bin/react-scripts", "test", "--env=jsdom"], "cwd": "${workspaceRoot}/src/ClientApp", "protocol": "inspector", "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", "disableOptimisticBPs": true }``` – rstackhouse Dec 16 '21 at 14:58

1 Answers1

17

Something eerily similar started happening to me, today.

I finally discovered that, in VS Code, I had accidentally ticked on "Breakpoints --> Caught Exceptions" in the "Run and Debug" view at some point. This caused the appearance of this error, but one that could be ignored by continuing to run (or by unticking that "Caught Exceptions" option).

mrcrowl
  • 1,265
  • 12
  • 14