Skip to content

Commit

Permalink
Make watch mode dependency tracking work with custom require hooks
Browse files Browse the repository at this point in the history
Fixes #2049.
  • Loading branch information
lo1tuma authored and novemberborn committed May 12, 2019
1 parent 08e99e5 commit cb4c809
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/worker/subprocess.js
Expand Up @@ -105,7 +105,6 @@ ipc.options.then(options => {

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

try {
Expand All @@ -120,6 +119,10 @@ ipc.options.then(options => {
} catch (_) {}
}

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

require(testPath);

if (accessedRunner) {
Expand Down
@@ -0,0 +1,3 @@
export default {
sources: ['source.custom-ext', 'setup.js']
};
@@ -0,0 +1 @@
{}
9 changes: 9 additions & 0 deletions test/fixture/watcher/with-custom-ext-dependencies/setup.js
@@ -0,0 +1,9 @@
'use strict';

const fs = require('fs');

// eslint-disable-next-line node/no-deprecated-api
require.extensions['.custom-ext'] = function (module, filename) {
const content = fs.readFileSync(filename, 'utf8');
module._compile(content, filename);
};
@@ -0,0 +1,2 @@
'use strict';
module.exports = true;
6 changes: 6 additions & 0 deletions test/fixture/watcher/with-custom-ext-dependencies/test-1.js
@@ -0,0 +1,6 @@
import test from '../../../..';
import dependency from './source.custom-ext';

test('works', t => {
t.truthy(dependency);
});
5 changes: 5 additions & 0 deletions test/fixture/watcher/with-custom-ext-dependencies/test-2.js
@@ -0,0 +1,5 @@
import test from '../../../..';

test('works', t => {
t.pass();
});
48 changes: 48 additions & 0 deletions test/integration/watcher.js
Expand Up @@ -82,6 +82,54 @@ test('watcher reruns test files when source dependencies change', t => {
});
});

test('watcher reruns ONLY test files that depend on a changed source with custom extension', t => {
let killed = false;

const child = execCli(['--verbose', '--require', './setup.js', '--watch', 'test-1.js', 'test-2.js'], {dirname: 'fixture/watcher/with-custom-ext-dependencies', env: {CI: ''}}, err => {
t.ok(killed);
t.ifError(err);
t.end();
});

let buffer = '';
let passedFirst = false;
child.stdout.on('data', str => {
buffer += str;
if (buffer.includes('2 tests passed') && !passedFirst) {
touch.sync(path.join(__dirname, '../fixture/watcher/with-custom-ext-dependencies/source.custom-ext'));
buffer = '';
passedFirst = true;
} else if (buffer.includes('1 test passed') && !killed) {
child.kill();
killed = true;
}
});
});

test('watcher reruns all tests when one of the configured files in the `require` option changes', t => {
let killed = false;

const child = execCli(['--verbose', '--require', './setup.js', '--watch', 'test-1.js', 'test-2.js'], {dirname: 'fixture/watcher/with-custom-ext-dependencies', env: {CI: ''}}, err => {
t.ok(killed);
t.ifError(err);
t.end();
});

let buffer = '';
let passedFirst = false;
child.stdout.on('data', str => {
buffer += str;
if (buffer.includes('2 tests passed') && !passedFirst) {
touch.sync(path.join(__dirname, '../fixture/watcher/with-custom-ext-dependencies/setup.js'));
buffer = '';
passedFirst = true;
} else if (buffer.includes('2 tests passed') && !killed) {
child.kill();
killed = true;
}
});
});

test('watcher does not rerun test files when they write snapshot files', t => {
let killed = false;

Expand Down

0 comments on commit cb4c809

Please sign in to comment.