-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
163 lines (139 loc) · 4.44 KB
/
core.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const os = require('os');
const spawn = require('cross-spawn');
const path = require('path');
const chokidar = require('chokidar');
const fs = require('fs');
/**
*
* @param {Object } conf
* @param {string} conf.webpack-config Path to webpack config
* @param {boolean} conf.watch Run mocha with watch
* @param {string} conf.spec Glob to mocha spec files.
*/
const preProcess = (conf) => ({
watchedDirectories: conf.watch && conf.watch.length ? conf.watch.split(',') : [],
webpackConfig: `${conf['webpack-config'] ? conf['webpack-config'] : path.join(__dirname, 'webpack.config.js')}`,
specGlob: `${conf.spec ? conf.spec : ''}`,
coverage: conf.coverage,
report: conf.report,
require: conf.require ? conf.require : '',
reporter: conf.reporter ? conf.reporter : 'spec',
});
module.exports.preProcess = preProcess;
// Path to client's NYC config (may or may not actually exist)
const localNYCConfigPath = path.join(process.cwd(), '.nycrc');
// Helper function to return passed in path if it exists or a default path.
const fileExistsOrDefault = (path1, defaultPath, fslocal = fs) => (fslocal.existsSync(path1)
? path1
: defaultPath
);
/**
* Get the full path to NYC config in order to point the client to .nycrc in @rei/vunit.
* If a local .nycrc exists, use that, otherwise use the path to @rei/vunit's .nycrc.
* @returns {string}
*/
const pathToNYCConfig = fileExistsOrDefault(localNYCConfigPath, path.join(__dirname, '.nycrc'));
/**
* Check if file is is valid JSON.
* @param pth
* @param fslocal
* @returns {boolean}
*/
const isValidJSON = (pth, fslocal = fs) => {
try {
JSON.parse(fslocal.readFileSync(pth, 'utf8'));
} catch (e) {
return false;
}
return true;
};
// Check JSON config.
if (!isValidJSON(pathToNYCConfig)) {
console.error(`.nycrc is invalid, ${pathToNYCConfig}`);
process.exit(1);
}
module.exports.isValidJSON = isValidJSON;
module.exports.fileExistsOrDefault = fileExistsOrDefault;
module.exports.run = (conf) => {
let watcher;
if (!conf) {
return;
}
const confPreprocessed = preProcess(conf);
console.log('Running tests:');
console.log('--------------------------------');
console.log(`watched directories: ${confPreprocessed.watchedDirectories}`);
console.log(`webpack config: ${conf['webpack-config'] ? conf['webpack-config'] : 'Using built-in config'}`);
console.log(`specGlob: ${confPreprocessed.specGlob}`);
console.log(`Coverage: ${confPreprocessed.coverage}`);
if (confPreprocessed.coverage) {
console.log(`NYC Config: ${pathToNYCConfig}`);
}
if (conf.require)console.log(`Required files: ${confPreprocessed.require}`);
console.log('--------------------------------');
/**
* Compile via webpack config and run unit tests.
*/
const run = () => {
// Base spawn command.
const spawnCmd = [
'cross-env',
'BABEL_ENV=test',
'NODE_ENV=test',
'nyc',
`--nycrc-path=${pathToNYCConfig}`, // Use local .nycrc
'--reporter=lcov',
'--reporter=text',
'--reporter=json-summary', // Required by @rei/cov-stats
'--report-dir=coverage',
'mochapack',
'--require', path.join(__dirname, 'setup.js'),
'--colors',
'--webpack-config',
confPreprocessed.webpackConfig,
'--require', path.join(__dirname, 'setup.js'),
'--require', 'ignore-styles',
`--reporter=${confPreprocessed.reporter}`,
confPreprocessed.specGlob,
];
// Remove nyc and its options if not running coverage.
if (!confPreprocessed.coverage) {
spawnCmd.splice(3, 6);
}
if (conf.require) {
spawnCmd.push('--require');
spawnCmd.push(confPreprocessed.require);
}
// Execute mochapack.
const cmd = spawn('npx', spawnCmd);
cmd.stdout.on('data', (data) => {
const d = data.toString();
if (d !== os.EOL) {
process.stdout.write(d);
}
});
// Log errors.
cmd.stdout.on('error', (error) => {
if (error) console.log(error);
});
// Log errors.
cmd.stderr.on('data', (data) => {
console.log(data.toString());
});
cmd.on('exit', (code) => {
process.exitCode = code;
});
};
// Re-run tests on file change.
if (confPreprocessed.watchedDirectories.length > 0) {
if (!watcher) {
watcher = chokidar.watch(confPreprocessed.watchedDirectories);
watcher.on('change', (pathToFile) => {
console.log(`${pathToFile} changed.`);
run();
});
}
}
// Run tests.
run();
};