firsttris / vscode-jest-runner

Simple way to run or debug one or more tests from context menu, codelens or command plalette
https://marketplace.visualstudio.com/items?itemName=firsttris.vscode-jest-runner
MIT License
265 stars 124 forks source link

Debug file pattern is missing quotes #201

Open Elias-Graf opened 3 years ago

Elias-Graf commented 3 years ago

The debug option does not surround the pattern with (single) quotes. This is an issue because I'm using NextJs, and NextJs uses brackets ([]) in the file path, which are used in regex. This leads to no matches found.

How to reproduce

Execute the following commands:

mkdir missing_quote_on_debug
cd missing_quote_on_debug
npm init -y
npm install -D jest
mkdir pages
mkdir tests/pages -p
touch "pages/[[...params]].js"
touch "tests/pages/[[...params]].test.js"

Fill the files with content:

// "pages/[[...params]].js"
module.exports = function page() {
  throw new Error('this is so hard to debug');
}
// "tests/pages/[[...params]].test.js"
const page = require("../../pages/[[...params]]");

it('does not throw a complicated error', () => {
  expect(page()).not.toThrow();
})

Then press the debug option in the test.

The output of run is:

node '/home/elias/Desktop/missing_quote_on_debug/node_modules/.bin/jest' '/home/elias/Desktop/missing_quote_on_debug/tests/pages/\[\[...params\]\].test.js' -t 'does not throw a complicated error'

Which has quotations enclosing the file pattern.

And the output of debug is:

/usr/bin/env 'NODE_OPTIONS=--require /snap/code-insiders/771/usr/share/code-insiders/resources/app/extensions/ms-vscode.js-debug/src/bootloader.bundle.js --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/tmp/node-cdp.65306-1.sock","deferredMode":false,"waitForDebugger":"","execPath":"/usr/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"/tmp/node-debug-callback-4bc4223479dffd02"}' /usr/bin/node ./node_modules/.bin/jest /home/elias/Desktop/missing_quote_on_debug/tests/pages/\\[\\[...params\\]\\].test.js -t "does not throw a complicated error" --runInBand

Which does not have quotations enclosing the file pattern.

Possible related issue

While writing this issue I found https://github.com/firsttris/vscode-jest-runner/issues/30 which is possible related.

Elias-Graf commented 3 years ago

I have no understanding of your code base, but buildJestArgs withQuotes seems to be hard coded to false here:

https://github.com/firsttris/vscode-jest-runner/blob/95e98f397aff4ec4b588aa0382dea1e4fbcca616/src/jestRunner.ts#L151

I would honestly recommend just always quoting the string.

PhiLhoSoft commented 3 years ago

I have a slightly related bug: if I have double quotes in the description string, it makes the run of the test to fail.

Test:

describe('ApiErrorHandlerService error handling', () => {
    describe('Test base behavior', () => {
        it('should "handle API error" for 403 / detailled error', (done) => {

Click on the Run button above the it:

> node "c:/_Projets/front/ng-oo-core/node_modules/jest/bin/jest.js" "c:/_Projets/front/ng-oo-core/utils/http/src/lib/api-error-handler.service.spec.ts" -t "ApiErrorHandlerService error handling Test base behavior should "handle API error" for 403 / detailled error"

Test Suites: 2 skipped, 0 of 2 total
Tests:       5 skipped, 5 total
Snapshots:   0 total
Time:        7.593 s
Ran all test suites matching /c:\\_Projets\\front\\ng-oo-core\\utils\\http\\src\\lib\\api-error-handler.service.spec.ts|API|error for 403 \\ detailled error/i with tests matching "ApiErrorHandlerService error handling Test base behavior should handle".

One way to avoid this is to backslash double quotes in the descriptions when building the command line. Unless they are escaped? Like in it("should \"handle API error\" for 403 / detailled error", (done) => {? I will let you test this case.

Elias-Graf commented 3 years ago

@PhiLhoSoft I don't think that your issue is related to mine as they occur when pressing the "Run" button. Mine only do when I "Debug". Your issue sounds more like an escaping issue (??).

Elias-Graf commented 3 years ago

Also, your tests seem to run, no? I don't see any failed test in the output you posted.

PhiLhoSoft commented 3 years ago

They skip all the tests instead of running the one I wanted… :-) Ie. it finds no matches. It is, somehow, related as both issues look like missing escaping in the command line, unless I am mistaken. Otherwise, I can open my own issue.

daibar commented 1 year ago

They skip all the tests instead of running the one I wanted… :-) Ie. it finds no matches. It is, somehow, related as both issues look like missing escaping in the command line, unless I am mistaken. Otherwise, I can open my own issue.

I can confirm I'm having the same issue with using double quotes in the describe argument. When I try to run the test it runs fine but it is skipped when trying to debug.

blaisn commented 7 months ago

It can be quotes also. For example, this test:

describe("This is always 'true'", () => {
    it("1 is less than 2", () => {
        expect(1).toBeLessThanOrEqual(2);
    });
});

when invoking the "Run" codelens everything is fine. The cpmmand line argument is: -t 'This is always '\''true'\'' 1 is less than 2' When clicking the debug codelens, the test is skipped. The commmand line argument is: -t This\ is\ always\ \'\\\'\'true\'\\\'\'\ 1\ is\ less\ than\ 2 There is probably too much escaping in there :-)

gisegalaburri commented 4 months ago

They skip all the tests instead of running the one I wanted… :-) Ie. it finds no matches. It is, somehow, related as both issues look like missing escaping in the command line, unless I am mistaken. Otherwise, I can open my own issue.

I can confirm I'm having the same issue with using double quotes in the describe argument. When I try to run the test it runs fine but it is skipped when trying to debug.

I have the same issue. I had to replace double quotes with single quotes to be able to debug