Basic configuration in launch.json is running all mocha tests ok in vs code (windows).
When try to add --grep option, unable to get expected pattern matching behavior (ie only run matching tests). With various combinations tried, I get no tests found error, or run all tests.
Note - I'm able to get grep option working with command line (test:grep script - though pattern text is manually input).
Expect --grep 'CURRENTTEST' to only run test with this string in describe (ie 1- passing test in example).
This is the behaviour I get from running command line with grep option as below;
mocha -r ts-node/register -r tsconfig-paths/register "spec/**/*.ts" --grep CURRENTTEST
Actual behaviour with launch.json as shown:
Error: No test files found: "C:\\temp\\Min code grep test/spec/**/*.spec.ts --grep 'CURRENTTEST'"
Some other combos attempted ran all tests (rather than pattern matched tests).
Other combinations of args attempted;
- Having grep option on same line as test location, and as separate line underneath.
- Surrounding pattern with single quotes, double quotes (with escape slash), and nothing.
Previous related (but not duplicate) questions; https://stackoverflow.com/a/39012417/20429097 Running test cases selectively with Mocha https://mochajs.org/#-grep-regexp-g-regexp
Code;
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"
}
CodePudding user response:
Every argument should be a new element in the array, like:
{
"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",
}