Reputation: 1005
I've been trying to learn unit-testing with Mocha using Visual Studio Code, as per this tutorial. But when I click the Play button to start Mocha Tests, I get an error about not finding the test files.
It apparently wants me to put the test files in a directory that'd kind of mess up the way I have things organized in my project.
So, is there a way for me to point it to the test folder I have set up elsewhere?
Upvotes: 1
Views: 7512
Reputation: 75073
To tell Mocha to run the tests in another folder rather than run in the default ./test
folder, you could simply change the command and execute
mocha "./spec/**/*.js"
or, if your bash does not support **
run as
mocha --recursive "./spec/*.js"
this means, that you can easily change the VSCode launch script to:
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"./<YOUR_FOLDER_HERE>/**/*.js",
"--ui bdd",
"--timeout 15000",
"--sort",
"--colors",
"--bail",
"-R spec",
"--recursive",
"--exit",
],
"internalConsoleOptions": "openOnSessionStart"
}
remember to change <YOUR_FOLDER_HERE>
with the folder name where you have the tests
if you have tests in your code but they are named, for example as routes.spec.js
you can simply say ./**/*.spec.js
and will find every file in your project that ends with .spec.js
The example above would be the same as having a mocha.opts
file as
# mocha options
--ui bdd
--timeout 15000
--sort
--colors
--bail
-R spec
--require test/prepare
--recursive
--exit
just play with the arguments until you run it as you like ...
BTW, if you're using VSCode, I would strongly recommend a Mocha extension (there are several) as you will not need to run the tests as a launch command and you will have all the tests in your sidebar with the option to debug each one and add breakpoints to your code
as well in your tests
P.S. this project is in GitHub so you can see how everything works, including code coverage if you into looking how that also works :)
Upvotes: 2