Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make watch mode dependency tracking work with custom require hooks #2088

Merged
merged 3 commits into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/worker/subprocess.js
Original file line number Diff line number Diff line change
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);
novemberborn marked this conversation as resolved.
Show resolved Hide resolved

require(testPath);

if (accessedRunner) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
sources: ['source.custom-ext', 'setup.js']
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
9 changes: 9 additions & 0 deletions test/fixture/watcher/with-custom-ext-dependencies/setup.js
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../..';

test('works', t => {
t.pass();
});
48 changes: 48 additions & 0 deletions test/integration/watcher.js
Original file line number Diff line number Diff line change
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