read-modify-write
Version:
Node module for reading, modifying and writing files recursively
59 lines (53 loc) • 1.62 kB
JavaScript
// `tests` is a singleton variable that will contain all our tests
const tests = [];
const path = require('path');
// The test function accepts a name and a function
function test(name, fn) {
// it pushes the name and function as an object to
// the `tests` array
tests.push({ name, fn })
}
function run() {
// `run` runs all the tests in the `tests` array
tests.forEach(t => {
// For each test, we try to execute the
// provided function.
try {
const output = t.fn();
if(output instanceof Promise) {
return output
.then(() => console.log('OK ', t.name))
.catch((e) => {
// Exceptions, if any, are caught
// and the test is considered failed
console.log('FAIL ', t.name)
// log the stack of the error
console.log(e.stack)
}); // Error: Whoops!
}
// If there is no exception
// that means it ran correctly
console.log('OK ', t.name)
} catch (e) {
// Exceptions, if any, are caught
// and the test is considered failed
console.log('FAIL ', t.name)
// log the stack of the error
console.log(e.stack)
}
})
}
// Get the list of files from the command line
// arguments
const files = process.argv.slice(2)
// expose the test function as a global variable
global.test = test;
// Load each file using `require`
files.forEach(file => {
// Once a file is loaded, it's tests are
// added to the `tests` singleton variable
require(path.join(__dirname, file))
})
// Now that all the tests from all the files are
// added, run them one after the other
run();