Skip to content

Commit

Permalink
Add i flag to ignore any tests inside node_module directories
Browse files Browse the repository at this point in the history
  • Loading branch information
kepikoi committed Dec 13, 2018
1 parent 1225d01 commit ee08b49
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,6 +1,6 @@
# gitignore

/node_modules
!/test/ignore_node_modules/node_modules/

# Only apps should have lockfiles
yarn.lock
Expand Down
20 changes: 14 additions & 6 deletions bin/tape
Expand Up @@ -6,23 +6,23 @@ var parseOpts = require('minimist');
var glob = require('glob');

var opts = parseOpts(process.argv.slice(2), {
alias: { r: 'require' },
string: 'require',
default: { r: [] }
});
alias: {r: 'require', i: "ignore"},
string: 'require',
default: {r: []}
});

var cwd = process.cwd();

if (typeof opts.require === 'string') {
opts.require = [opts.require];
}

opts.require.forEach(function(module) {
opts.require.forEach(function (module) {
if (module) {
/* This check ensures we ignore `-r ""`, trailing `-r`, or
* other silly things the user might (inadvertently) be doing.
*/
require(resolveModule(module, { basedir: cwd }));
require(resolveModule(module, {basedir: cwd}));
}
});

Expand All @@ -35,6 +35,14 @@ opts._.forEach(function (arg) {
throw new TypeError('unknown error: glob.sync did not return an array or throw. Please report this.');
}

if (opts.i) {
//ignore every file that resides inside a node_modules folder
//Note: glob option "ignore" does not support regex so this might be the simplest solution
files = files.filter(function (f) {
return !/\/node_modules\//.test(f);
})
}

files.forEach(function (file) {
require(resolvePath(cwd, file));
});
Expand Down
8 changes: 8 additions & 0 deletions readme.markdown
Expand Up @@ -84,6 +84,14 @@ $ tape -r ./my/local/module tests/**/*.js

Please note that all modules loaded using the `-r` flag will run *before* any tests, regardless of when they are specified. For example, `tape -r a b -r c` will actually load `a` and `c` *before* loading `b`, since they are flagged as required modules.

## Co-locating tests

When co-locating tests with their modules adding the `-i` flag will allow to ignore any tests that reside inside /node_modules/ directories. This will allow tape processing tests recursively in the project:

```sh
$ tape **/*.spec.js -i
```

# things that go well with tape

tape maintains a fairly minimal core. Additional features are usually added by using another module alongside tape.
Expand Down
36 changes: 36 additions & 0 deletions test/ignore_node_modules.js
@@ -0,0 +1,36 @@
var spawn = require("child_process").spawn;
var path = require("path");
var testFile = path.join(__dirname, "/ignore_node_modules/**/*.spec.js");
var bin = path.join(__dirname, "../bin/tape");

execute("node", [bin, testFile, "-i"]);

/**
* execute given command in a node child process
* @param {String} proc - process command name
* @param {[String]} args - array of process arguments
* @return {Promise<any>}
*/
function execute(proc, args) {
console.info("spawning ", proc, args.join(" "));
var cmd = spawn(proc, args, {
cwd: path.join(__dirname, ".."),
env: process.env
});

cmd.stdout.on("data", function (data) {
console.log(data.toString());
});

cmd.stderr.on("data", function (data) {
console.warn(data.toString());
});

cmd.on("close", function (code) {
console.log('Child exited with code ' + code);
});

cmd.on("error", function (code) {
console.error('Error while spawning ' + proc + ' ' + code + ' ' + arguments.join(" "));
});
}
5 changes: 5 additions & 0 deletions test/ignore_node_modules/module.spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/ignore_node_modules/node_modules/lib/lib.spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ee08b49

Please sign in to comment.