Skip to content
Permalink
Browse files

Make watch mode dependency tracking work with custom require hooks

Fixes #2049.
  • Loading branch information...
lo1tuma authored and novemberborn committed May 12, 2019
1 parent 08e99e5 commit cb4c8095952a970fb9bf5d1620810d5e172080ae
@@ -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 {
@@ -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) {
@@ -0,0 +1,3 @@
export default {
sources: ['source.custom-ext', 'setup.js']
};
@@ -0,0 +1 @@
{}
@@ -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;
@@ -0,0 +1,6 @@
import test from '../../../..';
import dependency from './source.custom-ext';

test('works', t => {
t.truthy(dependency);
});
@@ -0,0 +1,5 @@
import test from '../../../..';

test('works', t => {
t.pass();
});
@@ -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;

0 comments on commit cb4c809

Please sign in to comment.
You can’t perform that action at this time.