jest-community / vscode-jest

The optimal flow for Jest based testing in VS Code
MIT License
2.84k stars 294 forks source link

Debug mode doesn't work - Extension adds a prefix and an invalid path to jest command line #1101

Closed marviobezerra closed 10 months ago

marviobezerra commented 11 months ago

Environment

  1. vscode-jest version: [5.2.3]
  2. node -v: [20.9.0]
  3. npm -v or yarn --version: [10.1.0]
  4. npm ls jest or npm ls react-scripts (if you haven’t ejected): [29.7.0]
  5. your vscode-jest settings if customized:
    • jest.jestCommandLine? [NODE_OPTIONS=--experimental-vm-modules npx jest]
    • jest.autoRun? [not-defined]
  6. Operating system: [macOS 13.6.2]

Prerequisite

Steps to Reproduce

  1. Install Node Js using ASDF.

  2. Add a method, for example

    export const toTest = () => {
    return 'hello';
    };
  3. Add a test, for example:

    describe('CompanyService', () => {
    it('check value', () => {
    expect(toTest()).toBe('hello');
    });
    });
  4. Define the Jest Config

    
    import type { JestConfigWithTsJest } from 'ts-jest';

const config: JestConfigWithTsJest = { displayName: 'testerpals-server', preset: '../../jest.preset.js', coverageDirectory: '../../coverage/apps/feature', testEnvironment: 'node', transform: { '^.+\.[tj]s$': ['ts-jest', { useESM: true, tsconfig: '/tsconfig.spec.json' }], }, globalSetup: "./.scripts/jest.global.ts", setupFilesAfterEnv: ['./.scripts/jest.after.ts'], moduleFileExtensions: ['ts', 'js', 'html'], extensionsToTreatAsEsm: [".ts"], moduleNameMapper: { "^(\.{1,2}/.*)\.js$": "$1", } };

export default config;


5. Run the test above with the extension.
- All works here

6. Run the test above in **Debug Mode** with the extension
- Here it does not work <-- **Here is the issue!**

### Relevant Debug Info

The command line for debug is this:

/usr/bin/env 'NODE_OPTIONS= --require "/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/ms-vscode.js-debug/src/bootloader.js" --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS=:::{"inspectorIpc":"/var/folders/8f/__yz55dn123fw7_c87863h040000gq/T/node-cdp.43982-26f4e9af-13.sock","deferredMode":false,"waitForDebugger":"","execPath":"/Users/me/.asdf/shims/node","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"/var/folders/8f/__yz55dn123fw7_c87863h040000gq/T/node-debug-callback-d090df5f3bea03a8"}' /Users/me/.asdf/shims/node ./NODE_OPTIONS=--experimental-vm-modules npx jest --runInBand --watchAll=false --testNamePattern feature\ check\ value --runTestsByPath /Users/me/git/apps/server/src/feature/service.service.spec.ts


The command above does not work, I could identify two issues:

1. It's adding the Node path to the command line. `/Users/me/.asdf/shims/node` is the node binary file itself and not a folder.

2. It's adding a `./` in front of my test command. In this case the test command becomes `./NODE_OPTIONS=--experimental-vm-modules npx jest ...` which is not a valid command.

### Expected Behavior

It should not add prefixes or other paths to the jest command line configured. It should execute:

... NODE_OPTIONS=--experimental-vm-modules npx jest --runInBand ...



> Removed: `/Users/me/.asdf/shims/node ./` 

### Actual Behavior

It's modifying the jest command provided.
connectdotz commented 10 months ago

Hi, just want to make sure you are aware the debug config can be customized if the default one (best effort, generated from jest.jestCommandLine) does work for you. Please see how to debug; you might need to customize the debug config given your command line is a bit more complex.

marviobezerra commented 10 months ago

I'm aware that it's possible to customize the debug. However, I think that it's odd that the extension does not handle the node path properly. The ./ is being added in the wrong place. More over, my test command is not complex. It's just a "jest test"

connectdotz commented 10 months ago

It's true that the auto-generated debug config is kind of naive. It recognizes only limited patterns in jest.jestCommandline, such as npm or yarn, and treats the rest as a "command" to which it appends a relative path. "/" prefix.

This is expected behavior. We figure instead of deploying complex logic to guess what each user's command line is trying to do, it would be better to just let developers custom their debug config.

connectdotz commented 10 months ago

close as it's expected behavior. Feel free to reopen it if you have other related issues.