/
watch-run.js
191 lines (170 loc) · 4.34 KB
/
watch-run.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict';
const path = require('path');
const chokidar = require('chokidar');
const Context = require('../context');
const collectFiles = require('./collect-files');
/**
* Exports the `watchRun` function that runs mocha in "watch" mode.
* @see module:lib/cli/run-helpers
* @module
* @private
*/
/**
* Run Mocha in "watch" mode
* @param {Mocha} mocha - Mocha instance
* @param {Object} opts - Options
* @param {string[]} [opts.watchFiles] - List of paths and patterns to
* watch. If not provided all files with an extension included in
* `fileColletionParams.extension` are watched. See first argument of
* `chokidar.watch`.
* @param {string[]} opts.watchIgnore - List of paths and patterns to
* exclude from watching. See `ignored` option of `chokidar`.
* @param {Object} fileCollectParams - Parameters that control test
* file collection. See `lib/cli/collect-files.js`.
* @param {string[]} fileCollectParams.extension - List of extensions
* to watch if `opts.watchFiles` is not given.
* @private
*/
module.exports = (mocha, {watchFiles, watchIgnore}, fileCollectParams) => {
if (!watchFiles) {
watchFiles = fileCollectParams.extension.map(ext => `**/*.${ext}`);
}
const watcher = chokidar.watch(watchFiles, {
ignored: watchIgnore,
ignoreInitial: true
});
const rerunner = createRerunner(mocha, () => {
getWatchedFiles(watcher).forEach(file => {
delete require.cache[file];
});
mocha.files = collectFiles(fileCollectParams);
});
watcher.on('ready', () => {
rerunner.run();
});
watcher.on('all', () => {
rerunner.scheduleRun();
});
console.log();
hideCursor();
process.on('exit', () => {
showCursor();
});
process.on('SIGINT', () => {
showCursor();
console.log('\n');
process.exit(128 + 2);
});
// Keyboard shortcut for restarting when "rs\n" is typed (ala Nodemon)
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', data => {
const str = data
.toString()
.trim()
.toLowerCase();
if (str === 'rs') rerunner.scheduleRun();
});
};
/**
* Create an object that allows you to rerun tests on the mocha
* instance. `beforeRun` is called everytime before `mocha.run()` is
* called.
*
* @param {Mocha} mocha - Mocha instance
* @param {function} beforeRun - Called just before `mocha.run()`
*/
const createRerunner = (mocha, beforeRun) => {
// Set to a `Runner` when mocha is running. Set to `null` when mocha is not
// running.
let runner = null;
let rerunScheduled = false;
const run = () => {
try {
beforeRun();
resetMocha(mocha);
runner = mocha.run(() => {
runner = null;
if (rerunScheduled) {
rerun();
}
});
} catch (e) {
console.log(e.stack);
}
};
const scheduleRun = () => {
if (rerunScheduled) {
return;
}
rerunScheduled = true;
if (runner) {
runner.abort();
} else {
rerun();
}
};
const rerun = () => {
rerunScheduled = false;
eraseLine();
run();
};
return {
scheduleRun,
run
};
};
/**
* Return the list of absolute paths watched by a chokidar watcher.
*
* @param watcher - Instance of a chokidar watcher
* @return {string[]} - List of absolute paths
*/
const getWatchedFiles = watcher => {
const watchedDirs = watcher.getWatched();
let watchedFiles = [];
Object.keys(watchedDirs).forEach(dir => {
watchedFiles = watchedFiles.concat(
watchedDirs[dir].map(file => path.join(dir, file))
);
});
return watchedFiles;
};
/**
* Reset the internal state of the mocha instance so that tests can be rerun.
*
* @param {Mocha} mocha - Mocha instance
* @private
*/
const resetMocha = mocha => {
mocha.unloadFiles();
mocha.suite = mocha.suite.clone();
mocha.suite.ctx = new Context();
// Registers a callback on `mocha.suite` that wires new context to the DSL
// (e.g. `describe`) that is exposed as globals when the test files are
// reloaded.
mocha.ui(mocha.options.ui);
};
/**
* Hide the cursor.
* @ignore
* @private
*/
const hideCursor = () => {
process.stdout.write('\u001b[?25l');
};
/**
* Show the cursor.
* @ignore
* @private
*/
const showCursor = () => {
process.stdout.write('\u001b[?25h');
};
/**
* Erases the line on stdout
* @private
*/
const eraseLine = () => {
process.stdout.write('\u001b[2K');
};