Skip to content

Commit

Permalink
Replace "until" test helper with a continous run with control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
rubennorte committed Nov 9, 2018
1 parent b44e7f2 commit 9e54fbd
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 40 deletions.
45 changes: 25 additions & 20 deletions e2e/__tests__/detect_open_handles.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
'use strict';

import runJest, {until} from '../runJest';
import runJest, {runContinuous} from '../runJest';

try {
// $FlowFixMe: Node core
Expand All @@ -29,33 +29,36 @@ function getTextAfterTest(stderr) {
}

it('prints message about flag on slow tests', async () => {
const {stderr} = await until(
'detect-open-handles',
['outside'],
'Jest did not exit one second after the test run has completed.',
const run = runContinuous('detect-open-handles', ['outside']);
await run.waitUntil(({stderr}) =>
stderr.includes(
'Jest did not exit one second after the test run has completed.',
),
);

const {stderr} = await run.end();

const textAfterTest = getTextAfterTest(stderr);

expect(textAfterTest).toMatchSnapshot();
});

it('prints message about flag on forceExit', async () => {
const {stderr} = await until(
'detect-open-handles',
['outside', '--forceExit'],
'Force exiting Jest',
);
const run = runContinuous('detect-open-handles', ['outside', '--forceExit']);
await run.waitUntil(({stderr}) => stderr.includes('Force exiting Jest'));
const {stderr} = await run.end();
const textAfterTest = getTextAfterTest(stderr);

expect(textAfterTest).toMatchSnapshot();
});

it('prints out info about open handlers', async () => {
const {stderr} = await until(
'detect-open-handles',
['outside', '--detectOpenHandles'],
'Jest has detected',
);
const run = runContinuous('detect-open-handles', [
'outside',
'--detectOpenHandles',
]);
await run.waitUntil(({stderr}) => stderr.includes('Jest has detected'));
const {stderr} = await run.end();
const textAfterTest = getTextAfterTest(stderr);

expect(textAfterTest).toMatchSnapshot();
Expand All @@ -73,11 +76,13 @@ it('does not report promises', () => {
});

it('prints out info about open handlers from inside tests', async () => {
const {stderr} = await until(
'detect-open-handles',
['inside', '--detectOpenHandles'],
'Jest has detected',
);
const run = runContinuous('detect-open-handles', [
'inside',
'--detectOpenHandles',
]);
await run.waitUntil(({stderr}) => stderr.includes('Jest has detected'));
const {stderr} = await run.end();

const textAfterTest = getTextAfterTest(stderr);

expect(textAfterTest).toMatchSnapshot();
Expand Down
80 changes: 60 additions & 20 deletions e2e/runJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type RunJestOptions = {
stripAnsi?: boolean, // remove colors from stdout and stderr
};

type ConditionFunction = (...Array<any>) => boolean;

// return the result of the spawned process:
// [ 'status', 'signal', 'output', 'pid', 'stdout', 'stderr',
// 'envPairs', 'options', 'args', 'file' ]
Expand Down Expand Up @@ -94,11 +96,11 @@ export const json = function(
return result;
};

// Runs `jest` until a given output is achieved, then kills it with `SIGTERM`
export const until = async function(
// Runs `jest` continously (watch mode) and allows the caller to wait for
// conditions on stdout and stderr and to end the process.
export const runContinuous = function(
dir: string,
args?: Array<string>,
text: string,
options: RunJestOptions = {},
) {
const isRelative = dir[0] !== '/';
Expand Down Expand Up @@ -128,29 +130,67 @@ export const until = async function(
reject: false,
});

jestPromise.stderr.pipe(
new Writable({
write(chunk, encoding, callback) {
const output = chunk.toString('utf8');
let stderr = '';
let stdout = '';
const pending = new Set();

if (output.includes(text)) {
jestPromise.kill();
}
const dispatch = () => {
for (const fn of pending) {
fn({stderr, stdout});
}
};

jestPromise.stdout.pipe(
new Writable({
write(chunk, encoding, callback) {
stdout += chunk.toString('utf8');
dispatch();
callback();
},
}),
);

const result = await jestPromise;

// For compat with cross-spawn
result.status = result.code;

result.stdout = normalizeIcons(result.stdout);
if (options.stripAnsi) result.stdout = stripAnsi(result.stdout);
result.stderr = normalizeIcons(result.stderr);
if (options.stripAnsi) result.stderr = stripAnsi(result.stderr);
jestPromise.stderr.pipe(
new Writable({
write(chunk, encoding, callback) {
stderr += chunk.toString('utf8');
dispatch();
callback();
},
}),
);

return result;
return {
async end() {
jestPromise.kill();

const result = await jestPromise;

// For compat with cross-spawn
result.status = result.code;

result.stdout = normalizeIcons(stdout);
if (options.stripAnsi) result.stdout = stripAnsi(result.stdout);
result.stderr = normalizeIcons(stderr);
if (options.stripAnsi) result.stderr = stripAnsi(result.stderr);

return result;
},

getCurrentOutput() {
return {stderr, stdout};
},

waitUntil(fn: ConditionFunction): Promise<void> {
return new Promise(resolve => {
const check = state => {
if (fn(state)) {
pending.delete(check);
resolve();
}
};
pending.add(check);
});
},
};
};

0 comments on commit 9e54fbd

Please sign in to comment.