refs: https://github.com/mochajs/mocha/issues/1780#issuecomment-276908498
When we use --watch, mocha watches cwd(and all files under it). So, how about to specify watching directories and just watch them?
example)
If I want to run test/*/.spec.js when src/directory/ and lib/directory/ have changed, type this:
mocha --watch src/directory:lib/directory test/**/*.spec.js
I'm glad if someone considers about this.
Thanks.
I think you can run various directories with watch already:
# run mocha watch with a specific directory and all files within
mocha --watch test/app/
# run mocha watch with a multiple directories and all files within
mocha --watch test/app/ test/services/
# run mocha watch with multiple files
mocha --watch test/app/application.js test/services/serviceSuite.js
@dennisbaskin
Thank you for your reply.
I re-installed mocha 3.2.0 and tried mocha --watch src/directory/ lib/directory/ test/ , but it did not work correctly.
After that trial, I take a look at bin/_mocha and it is written like:
var program = require('commander');
program
.option('-w, --watch', 'watch files for changes')
Therefore, I think --watch accepts no parameters.
Hi @ShiMeiWo,
I don't think --watch was intended to have parameters. The files / directories specified are arguments for mocha. in the docs the list the following usage:
Usage: mocha [debug] [options] [files]
Watch just allows you to watch those files specified.
One thing you mentioned is that it did not work correctly. Could you describe a little bit more as to what was not working?
Just for reference, the following is the section that deals with files in the _mocha file:
var args = program.args;
// default files to test/*.{js,coffee}
if (!args.length) {
args.push('test');
}
args.forEach(function (arg) {
var newFiles;
try {
newFiles = utils.lookupFiles(arg, extensions, program.recursive);
} catch (err) {
if (err.message.indexOf('cannot resolve path') === 0) {
console.error('Warning: Could not find any test files matching pattern: ' + arg);
return;
}
throw err;
}
files = files.concat(newFiles);
});
if (!files.length) {
console.error('No test files found');
process.exit(1);
}
// resolve
files = files.map(function (path) {
return resolve(path);
});
You can look in the utils file to see how lookupFiles works, that will hopefully also help you resolve your issue.
One thing you mentioned is that it did not work correctly. Could you describe a little bit more as to what was not working?
Once test runs, compiler shows an error:
var a:HTMLElement = document.createElement('div');
^
ReferenceError: document is not defined
When I used single target path, this error was not shown.
As https://github.com/mochajs/mocha/issues/1780#issuecomment-276908498, I want to run tests when one of specified directories is changed, not cwd(= under workspace).
What I want to do is like this (it's just an example!):
https://github.com/ShiMeiWo/mocha/commit/726fd943babf67f33d357022d599aa358f393b27#diff-789b16a197fe1df6ad7686554bb32265
I see, I have not tried mocha with browser setup. I would make the assumption though that the environment is setup once when all suites are run. Not sure exactly how it is setup, but you might be able to create a specHelper.js file which you could require inside each spec file. The specHelper should contain any additional setup beyond mocha.
If the test runs outside of browser, you might not have globals such as document setup. To fix that you can always stub out a document. Not 100% sure, since this is unique to your case.
Have you tried running mocha with your changes?
I see, I have not tried mocha with browser setup. I would make the assumption though that the environment is setup once when all suites are run.
Hmm, my environment is almost same as you think. Once I save TS files, test runs automatically and display the results on console screen.
Well, I show 'real' command I've actually typed:
mocha -S --compilers ts:ts-node/register -w ./src/scripts/ ./test/
I think, tsconfig's path-resolving might not be effective at TS files in ./test directory.
Not sure exactly how it is setup, but you might be able to create a specHelper.js file which you could require inside each spec file. The specHelper should contain any additional setup beyond mocha.
Okay, I will try specHelper.js ASAP.
Now I am able to run test with multi target path (I've changed $NODE_PATH into './test:./src/scripts'). Thank you @dennisbaskin!
But. the first issue( https://github.com/mochajs/mocha/issues/2702#issue-205487855 ) is not solved yet. Once I save a ts file, test runs multiple times.
Have you tried running mocha with your changes?
When I try https://github.com/mochajs/mocha/issues/2702#issuecomment-278906894 and execute mocha -S --compilers ts:ts-node/register -w --watch-dirs src/scripts/ ./test/**/*.ts , then it works as I expect.
What does make wrong??
Interesting, I am not sure, to be honest. It would take some time to dig into this. Just help me clarify the issues, so I better understand:
Changing NODE_PATH you run the command: ______________________
and it does not work.
But when you run mocha -S --compilers ts:ts-node/register -w --watch-dirs src/scripts/ ./test/**/*.ts everything works okay.
And both of these commands are with, or without your proposed code change?
And am I missing anything else? This will help look into it.
OS: Windows 10 Pro
Node: 7.7.1
Mocha: 3.2.0
I am puzzled by this as well.
Running the following mocha dist/tests/ -w starts mocha in watch mode, correctly. However, mocha than reruns the entire test suite for any change in any JavaScript file in cwd, regardless of whether it is actually related to the tests.
My tests do not have any browser-related code or setup, as they are pure Node.
I wonder how come there is no much more reports on this. Is this some kind of recent regression?
My current solution is using gulp.watch instead of mocha watch
Any progress on this?
@dennisbaskin I cant find the option --watch-dirs in the docs, where is that from ?
Most helpful comment
Any progress on this?
@dennisbaskin I cant find the option
--watch-dirsin the docs, where is that from ?