jest-community / vscode-jest

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

Running individual test on windows uses a caret (^) as escape character in the testNamePattern #1175

Closed necjosh closed 5 days ago

necjosh commented 1 week ago

Describe the bug Instead of a backslash, a caret is used to escape special characters in the testNamePattern when running an individual test through the UI. This causes the test to be skipped.

What I also find curious is that the testNamePattern is included in the command at all. I'm running the entire test file, I don't understand why it's including it since the testPathPattern already narrows it down to the file exclusively.

describe("foo >", () => {
    it("bar", () => {
        expect(1).toBe(1);
    });
});
Test Suites: 1 skipped, 0 of 1 total
Tests:       1 skipped, 1 total
Snapshots:   0 total
Time:        1.262 s
Ran all test suites matching [omitted]jest-repro\\src\\App\.test\.js/i with tests matching "foo ^> bar$".

Spawn command: [Extension Host] spawning process with command=npm test -- --testLocationInResults --json --useStderr --outputFile [omitted] --testNamePattern "foo ^> bar$" --no-coverage --reporters default --reporters "[omitted]" --colors --watchAll=false --testPathPattern "[omitted]\\src\\App\.test\.js"

I added more special characters to see which ones are affected:

describe("foo ` ~ ! @ # $ % ^ & * ( ) { } [ ] / = ? + _ - , . < > ' \"", () => {
    it("bar", () => {
        expect(1).toBe(1);
    });
});
Test Suites: 1 skipped, 0 of 1 total
Tests:       1 skipped, 1 total
Snapshots:   0 total
Time:        1.712 s
Ran all test suites matching [omitted]\\src\\App\.test\.js/i with tests matching "foo ` ~ ^! @ # \$ % \^^ ^& \* \( \) \{ \} \[ \] / = \? \+ _ - , \. ^< ^> ' " bar$".

! ^ & < > are affected.

To Reproduce Sample repo

Steps to reproduce the behavior:

  1. Run the single test in the UI
  2. See error

Expected behavior Backslashes are used to escape special characters in the testNamePattern.

Environment (please complete the following information):

Prerequisite

necjosh commented 1 week ago

I had no idea that ^ is used as an escape character in CMD.... Anyway, it seems that there is no need to use an escape character in the testNamePattern, I suppose because it's in quotes?

CMD does not interpret ^ as an escape character in this portion of the command, so it's passed on as an actual part of the regexp.

necjosh commented 1 week ago

I confirmed this by running a few commands in CMD.

#command:
Echo THIS & THAT

#output:
THIS
'THAT' is not recognized as an internal or external command,
operable program or batch file.

#command:
Echo THIS ^& THAT

#output:
THIS & THAT

#command:
Echo "THIS & THAT"
#output:
"THIS & THAT"

#command:
Echo "THIS ^& THAT"
#output:
"THIS ^& THAT"

As you can see in the last example, the caret does not act as an escape character in this case.

connectdotz commented 1 week ago

yes, this looks like a bug... thanks for raising the issue, I will take a look.

connectdotz commented 6 days ago

@necjosh, I think I have a fix. Do you mind confirming if the new build fixes your issue: vscode-jest-6.3.0.vsix.zip