Running and troubleshooting in VS Code on Windows- Mocha's grep option through launch.json is causing an issue with the message "No test files were located" instead of running only the specified pattern

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 Link

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"
}

Answer №1

Each argument must be a separate element within the array, such as:

{
      "name": "mocha NEWTEST",
      "type": "node",
      "request": "launch",
      "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
      "stopOnEntry": false,
      "args": [
        "-r",
        "ts-node/register",
        "${workspaceRoot}/spec/**/*.spec.ts",
        "--grep",
        "NEWTEST",
        "--no-timeouts",
        "--colors",
        "--recursive",
      ],
      "cwd": "${workspaceRoot}",
      "internalConsoleOptions": "openOnSessionStart",
      // "console": "integratedTerminal",
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unexpected TypeError when using Response.send()

Here is a snippet of my simple express code: const api = Router() api.post('/some-point', async (req, res, next) => { const someStuffToSend = await Promise.resolve("hello"); res.json({ someStuffToSend }); }) In my development environmen ...

Objecttype is described as a component within a different class or interface

I have a specific Data Interface that has the following structure: interface Data { message: string; Id: string; uid: string; } When uploading data with this structure to a database, everything works smoothly. However, when it comes to updatin ...

Sorting a custom numeric column in Jquery Datatables: Tips and Tricks

Within my data table, there exists a column that displays numeric values in formatted currency format such as "$5,66666.77 USD". Here is the segment of the Jquery table definition responsible for this column: data: "amount", ordera ...

Can a method be called within another method?

Here is the situation I am facing: export class Test { public Method_A() { console.log("Method_A"); } } Now, in a separate file, I have the following code: const test = new Test(); test.MethodA() However, I also require the following ...

Navigating through nested JSON Objects for dropdown functionality in Angular 6 - a step-by-step guide

Currently, I am facing a challenge in Angular 6.0 where I am trying to use HttpClient to iterate through JSON data retrieved from a local file within my assets folder. Below is the sample JSON Data: [{ "configKey": [{ "user1": [{ ...

Executing a TypeScript function through a package.json script: A guide

I'm currently using a javascript function from my package.json script, but I want to switch it to a typescript function in a typescript file. Can someone advise me on how to make this adjustment? This is the javascript code that runs the desired func ...

When using Angular with mdbootstrap, the mdb-tabs directive will move to the end if the ngIf condition is true

Currently facing a challenge with a significant amount of code here. It is referenced as follows: "ng-uikit-pro-standard": "file:ng-uikit-pro-standard-8.3.0.tgz", I am attempting to display a tab between 1 and 3 if a certain condition ...

Validating time selection for exceeding limits

I'm looking to implement time checking in my project but I'm unsure how to proceed. I have 3 different operation hours for my shop, outlined as follows: Operation hour 1: Open at 08:30 and close at 12:30 Operation hour 2: Open at 14:30 and close ...

Removing items with properties that are null or undefined

My current situation involves using the AWS SDK, and I've noticed that many of its objects have members that can be undefined. Take for example S3.Object: export interface Object { /** * */ Key?: ObjectKey; /** * * ...

Guide on setting up factories with pre-existing relationships in MIKRO-ORM

Hey there! I'm currently exploring how to set up a factory and establish relationships between models. For instance, I have a UserFactory that corresponds to the User entity which is connected to the userType table. However, in the factory, I'm ...

Typescript displays an error message when attempting to assign a list of string variants to a defined type

Encountering an interesting TS error in the code snippet below: interface Bar { pictureType: "X" | "Y" } interface RT { output: Bar[] } const func = (): RT => { const list = [{ pictureType: 'X', }] r ...

Angular compilation error - Invalid selector: %

Whenever I execute npm run build, which in turn runs ng build -c production, the build process completes successfully. However, throughout this process, the command prompt displays a warning message: Warning: 303 rules skipped due to selector errors: 0% ...

The error message "The type 'DynamicModule' from Nest.js cannot be assigned to the type 'ForwardReference' within the nest-modules/mailer" was encountered during development

Recently, I decided to enhance my Nest.js application by integrating the MailerModule. I thought of using the helpful guide provided at this link: Acting on this idea, I went ahead and performed the following steps: To start with, I executed the command ...

Errors in TypeScript type definition IntelliSense

While using Visual Studio 2022, I am encountering numerous IntelliSense errors related to type definitions from the node_modules folder appearing in the error list window. I have already excluded the node_modules folder in my tsconfig.json file like this ...

The importance of displaying doughnut chart tooltips in Angular 5 console

Is there a way to consistently display tooltips for a doughnut chart? This code snippet might help: Chart.pluginService.register({ beforeRender: function(chart) { if (chart.config.options.showAllTooltips) { // create an array of tooltips // we ...

Guide to adding a loading spinner into your Angular project

I've been attempting to incorporate a spinner into my application, but unfortunately, the spinner isn't showing up. Despite checking the console and terminal for errors, there doesn't seem to be any indication as to why the spinner is not a ...

How to perform complex relational queries in TypeORM using relationship properties?

Currently, I am immersing myself in Nest.js using TypeORM and exploring its query builder capabilities. I recently came across the relations property, which proved to be quite useful. However, I'm wondering if it's possible to achieve the same o ...

The functionality of TypeScript's .entries() method is not available for the type 'DOMTokenList'

I'm currently working with a stack that includes Vue3, Vite, and TypeScript. I've encountered an issue related to DOMTokenList where I'm trying to utilize the .entries() method but TypeScript throws an error saying Property 'entries&apo ...

Using Cypress fixtures with TypeScript

After transitioning from using Cypress with Javascript specs to Typescript, I encountered a challenge in working with Fixtures. In Javascript, the approach below worked; however, I faced difficulties when switching to Typescript. Fixture JSON file: I sto ...

How can I take photos in bulk when I open the camera on Ionic 3?

Is there a way to capture multiple images at once using the camera? Currently, I am only able to capture one image when the user clicks. However, I would like to capture four images when the user clicks the success button. let options: CaptureImageOption ...