-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does mocha watch the test files modified only? #2928
Comments
Hi @mrdulin - very interesting! Mocha doesn't necessarily want to know about the GIT domain, because what about the Mercurial people and such. Can I suggest you look into Husky or Lint Staged. Lint staged is probably what you're after, because it gives you a list of staged files. |
@maraisr I am looking for a way that re-run the test file I modified , not all test files. Like |
Hi @mrdulin, I had a similar issue and this is how I avoid re-running entire test suites. // package.json
"tdd": "mocha ${TEST_FILES:-test/} --watch --recursive" # terminal
TEST_FILES='test/a.spec.js test/b.spec.js' npm run tdd This will only test files specified in |
@junpos This seems like a manually work. My thought is If |
@mrdulin I think
|
@junpos Yeah. Can you take a look about
|
This can be done without environment variables: // package.json
"test": "mocha --watch --recursive" # terminal
npm test test/a.spec.js test/b.spec.js OR in --watch --recursive # terminal
mocha test/a.spec.js test/b.spec.js
Mocha doesn't know anything about Git, Mercurial, SVN etc. To answer the question as generally as possible:
Mocha doesn't have any knowledge about which tests are testing which files; such a tool could presumably be built, but... The built-in |
I am a bot that watches issues for inactivity. |
Hi, this script will allow you to execute only affected files. Please modify:
KNOWN ISSUE This will not work if you are using imports by variables, such as var watch = require('node-watch');
var execSync = require('child_process').execSync;
var dependencyTree = require('dependency-tree');
var glob = require('glob');
var changedFiles = [];
var timeout = null;
var trees = {};
const testFilter = '.test';
const fileFilter = /\.tsx?/;
function buildDependencyTree(filename) {
return dependencyTree({
filename,
isListForm: true,
directory: './src',
filter: path => path.indexOf('node_modules') === -1
});
}
function scan() {
glob('./src/**/*.test.ts?', {}, function(er, files) {
trees = {};
for (let file of files) {
trees[file.replace('./', '')] = buildDependencyTree(file);
}
});
}
scan();
watch('./', { recursive: true, filter: fileFilter }, function(evt, name) {
changedFiles.push(name);
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
try {
// build test list
let tests = [];
for (let file of changedFiles) {
if (file.indexOf(testFilter) >= 0) {
tests.push(file);
} else {
// find it in dependency tree
for (let f of Object.getOwnPropertyNames(trees)) {
if (trees[f].some(n => n.indexOf(file) >= 0)) {
tests.push(f);
}
}
}
}
changedFiles = [];
if (tests.length) {
let files = tests.join(',');
// CHANGE YOUR COMMAND
const command = `CI=true TS_NODE_FAST=true ./node_modules/.bin/mocha --exit '${files}' -P`;
console.log('\n-- Running --\n');
console.log(command);
execSync(command);
}
} catch (ex) {
} finally {
scan();
}
}, 100);
}); save as 'mocha-watch.js' in your root file and just add it to package.json as {
"watch": "node mocha-watch"
} |
@tomitrescak Hello Sir, I am using node js API project using javascript only not typescript. Then may I use this script and how? Please help, urgently needed. |
I use
mocha -w --recursive
cli to watch my test files changed.But it will re-run all my tests, even I just modify a single test file.
I want something like just watch and re-run the modified test file tracked by
git
?The text was updated successfully, but these errors were encountered: