Setting up basic configuration in launch.json to run all Mocha tests successfully in VS Code on Windows was successful.
However, when attempting to add the --grep option for pattern matching behavior, I encountered issues. Despite trying various combinations, I either received a "no tests found" error or all tests ran instead of just the matching ones.
Interestingly, I was able to get the grep option working via the command line using the test:grep script, although the pattern text had to be manually inputted.
For instance, my expectation was that --grep 'CURRENTTEST' would only execute the test with this specific string within its describe block (e.g., the passing test in the example).
When running the command line with the grep option like this:
mocha -r ts-node/register -r tsconfig-paths/register "spec/**/*.ts" --grep CURRENTTEST
This resulted in the expected behavior. However, the launch.json setup displayed the following error:
Error: No test files found: "C:\\temp\\Min code grep test/spec/**/*.spec.ts --grep 'CURRENTTEST'"
I tried several other argument combinations as well:
- Placing the grep option on the same line as the test location and on a separate line below it
- Using single quotes, double quotes (with escape slashes), and no quotes around the pattern
I also came across some related questions worth checking out:
Stack Overflow Link 1 Stack Overflow Link 2 Mocha Documentation LinkCode;
export function testFn(): number { return 1; }
Tests;
describe('CURRENTTEST test pass', () => {
it('should pass', () => {
expect(testFn()).to.equal(1);
});
});
describe('test fail', () => {
it('should fail', () => {
expect(testFn()).to.equal(2);
});
});
launch.json
{
"version": "0.2.0",
"configurations": [
////////////////////////////// basic config to run all tests - works //////////////////////////////////////
{
"name": "mocha tests",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": [
"-r",
"ts-node/register",
"${workspaceRoot}/spec/**/*.spec.ts",
"--no-timeouts",
"--colors",
"--recursive",
],
"cwd": "${workspaceRoot}",
// "internalConsoleOptions": "openOnSessionStart",
// "console": "integratedTerminal",
},
/////////////////////// grep config to run CURRENTTEST only - doesn't work ////////////////////////////
{
"name": "mocha CURRENTTEST",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": [
"-r",
"ts-node/register",
"${workspaceRoot}/spec/**/*.spec.ts --grep 'CURRENTTEST'",
"--no-timeouts",
"--colors",
"--recursive",
],
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart",
// "console": "integratedTerminal",
}
]
}
package.json
{
"name": "min code grep test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"@types/chai": "latest",
"@types/mocha": "latest",
"@types/node": "latest",
"chai": "latest",
"eslint-import-resolver-typescript": "latest",
"eslint-plugin-jsx-a11y": "latest",
"eslint-plugin-react": "latest",
"eslint-plugin-react-hooks": "latest",
"ts-node": "latest",
"typescript": "latest"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "latest",
"@typescript-eslint/parser": "latest",
"eslint": "latest",
"eslint-config-airbnb-base": "latest",
"eslint-config-airbnb-typescript": "latest",
"eslint-config-google": "latest",
"eslint-config-standard": "latest",
"eslint-plugin-import": "latest",
"eslint-plugin-node": "latest",
"eslint-plugin-promise": "latest",
"mocha": "latest"
},
"scripts": {
"test": "mocha -r ts-node/register -r tsconfig-paths/register './spec/**/*.spec.ts'",
"test:grep": "mocha -r ts-node/register -r tsconfig-paths/register \"spec/**/*.ts\" --grep"
},
"author": "",
"license": "ISC"
}