Skip to content

Commit cb4c809

Browse files
lo1tumanovemberborn
authored andcommitted
Make watch mode dependency tracking work with custom require hooks
Fixes #2049.
1 parent 08e99e5 commit cb4c809

File tree

8 files changed

+78
-1
lines changed

8 files changed

+78
-1
lines changed

lib/worker/subprocess.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ ipc.options.then(options => {
105105

106106
// Install before processing options.require, so if helpers are added to the
107107
// require configuration the *compiled* helper will be loaded.
108-
dependencyTracking.install(testPath);
109108
precompilerHook.install();
110109

111110
try {
@@ -120,6 +119,10 @@ ipc.options.then(options => {
120119
} catch (_) {}
121120
}
122121

122+
// Install dependency tracker after the require configuration has been evaluated
123+
// to make sure we also track dependencies with custom require hooks
124+
dependencyTracking.install(testPath);
125+
123126
require(testPath);
124127

125128
if (accessedRunner) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
sources: ['source.custom-ext', 'setup.js']
3+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
// eslint-disable-next-line node/no-deprecated-api
6+
require.extensions['.custom-ext'] = function (module, filename) {
7+
const content = fs.readFileSync(filename, 'utf8');
8+
module._compile(content, filename);
9+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict';
2+
module.exports = true;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import test from '../../../..';
2+
import dependency from './source.custom-ext';
3+
4+
test('works', t => {
5+
t.truthy(dependency);
6+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from '../../../..';
2+
3+
test('works', t => {
4+
t.pass();
5+
});

test/integration/watcher.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,54 @@ test('watcher reruns test files when source dependencies change', t => {
8282
});
8383
});
8484

85+
test('watcher reruns ONLY test files that depend on a changed source with custom extension', t => {
86+
let killed = false;
87+
88+
const child = execCli(['--verbose', '--require', './setup.js', '--watch', 'test-1.js', 'test-2.js'], {dirname: 'fixture/watcher/with-custom-ext-dependencies', env: {CI: ''}}, err => {
89+
t.ok(killed);
90+
t.ifError(err);
91+
t.end();
92+
});
93+
94+
let buffer = '';
95+
let passedFirst = false;
96+
child.stdout.on('data', str => {
97+
buffer += str;
98+
if (buffer.includes('2 tests passed') && !passedFirst) {
99+
touch.sync(path.join(__dirname, '../fixture/watcher/with-custom-ext-dependencies/source.custom-ext'));
100+
buffer = '';
101+
passedFirst = true;
102+
} else if (buffer.includes('1 test passed') && !killed) {
103+
child.kill();
104+
killed = true;
105+
}
106+
});
107+
});
108+
109+
test('watcher reruns all tests when one of the configured files in the `require` option changes', t => {
110+
let killed = false;
111+
112+
const child = execCli(['--verbose', '--require', './setup.js', '--watch', 'test-1.js', 'test-2.js'], {dirname: 'fixture/watcher/with-custom-ext-dependencies', env: {CI: ''}}, err => {
113+
t.ok(killed);
114+
t.ifError(err);
115+
t.end();
116+
});
117+
118+
let buffer = '';
119+
let passedFirst = false;
120+
child.stdout.on('data', str => {
121+
buffer += str;
122+
if (buffer.includes('2 tests passed') && !passedFirst) {
123+
touch.sync(path.join(__dirname, '../fixture/watcher/with-custom-ext-dependencies/setup.js'));
124+
buffer = '';
125+
passedFirst = true;
126+
} else if (buffer.includes('2 tests passed') && !killed) {
127+
child.kill();
128+
killed = true;
129+
}
130+
});
131+
});
132+
85133
test('watcher does not rerun test files when they write snapshot files', t => {
86134
let killed = false;
87135

0 commit comments

Comments
 (0)