forked from mrsone40/vets-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmocha.js
More file actions
110 lines (100 loc) · 3.32 KB
/
mocha.js
File metadata and controls
110 lines (100 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const Mocha = require('mocha');
const { Writable } = require('stream');
const path = require('path');
const chalk = require('chalk');
// babel-register only looks for environment variables
process.env.BABEL_ENV = process.env.BABEL_ENV || 'test';
// use babel-register to compile files on the fly
// require('babel-register');
require("@babel/register");
require('babel-polyfill');
// require mocha setup files
require('../src/platform/testing/unit/mocha-setup.js');
require('../src/platform/testing/unit/enzyme-setup.js');
let showErrors = false;
// keys for current require.cache
const unwatchedModules = Object.keys(require.cache).map(file => file);
process.on('message', ({ tests, shouldShowErrors }) => {
showErrors = shouldShowErrors;
runMochaTests(tests).then(() => {
// create a list of src and test files required by mocha
const requiredFiles = Object.keys(require.cache).filter(
file =>
!unwatchedModules.includes(file) && !file.includes('node_modules'),
);
// send list of imported files and unit tests for each src file to the runner
process.send({
requiredFiles,
unitTestsForSrc: getUnitTestsForSrc(requiredFiles),
});
});
});
async function runMochaTests(files) {
// show the file names if fewer than ten
if (files.length < 10) {
files.forEach(file => {
let fileParts = path.basename(file).split('.');
console.log(
`${chalk.cyan(fileParts.shift())}.${chalk.blue(fileParts.join('.'))}`,
);
});
}
console.log(' ');
// keep errors from polluting the reporter by redirecting the error stream
let errorStream;
if (!showErrors) {
errorStream = new Writable({
write(chunk, encoding, callback) {
callback();
},
});
process.stderr.write = errorStream;
}
mocha = new Mocha();
mocha.reporter('nyan');
// sets up globals
mocha.addFile('./src/platform/testing/unit/helpers.js');
files.forEach(file => mocha.addFile(file));
return new Promise((fulfill, reject) => {
console.log(chalk.green('Starting mocha (this can take a few seconds)...'));
mocha.run().once('end', function() {
// reattach error stream
fulfill();
});
}).catch(error => {
console.log(error);
process.send({
error,
});
});
}
function getUnitTestsForSrc(requiredFiles) {
return (
requiredFiles
// only use files in test directory
.filter(file => file.includes('test'))
// iterate over each test file
.reduce((acc, testFile) => {
// get the test files require.cache
const requiredSourceFiles = require.cache[testFile].children // children is an array of modules the test file imports
.forEach(childModule => {
// filter anything that isn't in the src directory
if (!childModule.id.includes('src')) {
return;
}
// add the file path as a key to the accumulator
// add the test file to the tests array
if (acc[childModule.id]) {
acc[childModule.id].push(testFile);
} else {
acc[childModule.id] = [testFile];
}
});
return acc;
}, {})
);
}
// ensures errors that blow up test watcher make it to console reporter
process.on('uncaughtException', function(err) {
console.error(err && err.stack ? err.stack : err);
});