How to run an individual test in mocha? with or without hardhat

In the “describe” callback, you provide multiple tests using the “test” function or “it” function callbacks. How can you run only one “test” or “it” function?

One way is to use it.only that depends on jest. But to make mocha run only one test using the command line you run the following command.

mocha --require ts-node/register --require hardhat/register --timeout 10000 --ui bdd src/mytest.ts --grep "^MyTestSuit should output the expected$"

Where “MyTestSuit” is what you provide as the first argument to “describe” function and the rest of the “should output the expected” is what you provide in your test or it functions callbacks as follows:

describe('MyTestSuit', () => {
test("should output the expected", () => {} )
});

Remember to combine them using space.

And src/mytest.ts is your test file.

This is how WebStorm runs your individual tests.

If you are not using hardhat, remove –require hardhat/register

If you are not using typescript, remove –require ts-node/register

Good luck.

Leave a Comment

Your email address will not be published. Required fields are marked *