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

chore: reject waitUntil if process exits #11909

Merged
merged 2 commits into from Sep 29, 2021
Merged
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
59 changes: 47 additions & 12 deletions e2e/runJest.ts
Expand Up @@ -13,6 +13,7 @@ import * as fs from 'graceful-fs';
import stripAnsi = require('strip-ansi');
import type {FormattedTestResults} from '@jest/test-result';
import type {Config} from '@jest/types';
import {ErrorWithStack} from 'jest-util';
import {normalizeIcons} from './Utils';

const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js');
Expand All @@ -34,7 +35,10 @@ export default function runJest(
args?: Array<string>,
options: RunJestOptions = {},
): RunJestResult {
return normalizeStdoutAndStderr(spawnJest(dir, args, options), options);
return normalizeStdoutAndStderrOnResult(
spawnJest(dir, args, options),
options,
);
}

function spawnJest(
Expand Down Expand Up @@ -104,16 +108,24 @@ export interface RunJestJsonResult extends RunJestResult {
json: FormattedTestResults;
}

function normalizeStdoutAndStderr(
function normalizeStreamString(
stream: string,
options: RunJestOptions,
): string {
if (options.stripAnsi) stream = stripAnsi(stream);
stream = normalizeIcons(stream);

return stream;
}

function normalizeStdoutAndStderrOnResult(
result: RunJestResult,
options: RunJestOptions,
): RunJestResult {
if (options.stripAnsi) result.stdout = stripAnsi(result.stdout);
result.stdout = normalizeIcons(result.stdout);
if (options.stripAnsi) result.stderr = stripAnsi(result.stderr);
result.stderr = normalizeIcons(result.stderr);
const stdout = normalizeStreamString(result.stdout, options);
const stderr = normalizeStreamString(result.stderr, options);

return result;
return {...result, stderr, stdout};
}

// Runs `jest` with `--json` option and adds `json` property to the result obj.
Expand Down Expand Up @@ -146,6 +158,7 @@ export const json = function (

type StdErrAndOutString = {stderr: string; stdout: string};
type ConditionFunction = (arg: StdErrAndOutString) => boolean;
type CheckerFunction = (arg: StdErrAndOutString) => void;

// Runs `jest` continously (watch mode) and allows the caller to wait for
// conditions on stdout and stderr and to end the process.
Expand All @@ -158,7 +171,21 @@ export const runContinuous = function (

let stderr = '';
let stdout = '';
const pending = new Set<(arg: StdErrAndOutString) => void>();
const pending = new Set<CheckerFunction>();
const pendingRejection = new WeakMap<CheckerFunction, () => void>();

jestPromise.addListener('exit', () => {
for (const fn of pending) {
const reject = pendingRejection.get(fn);

if (reject) {
console.log('stdout', normalizeStreamString(stdout, options));
console.log('stderr', normalizeStreamString(stderr, options));

reject();
}
}
});

const dispatch = () => {
for (const fn of pending) {
Expand Down Expand Up @@ -186,7 +213,7 @@ export const runContinuous = function (
}),
);

return {
const continuousRun = {
async end() {
jestPromise.kill();

Expand All @@ -196,7 +223,7 @@ export const runContinuous = function (
result.stdout = stdout;
result.stderr = stderr;

return normalizeStdoutAndStderr(result, options);
return normalizeStdoutAndStderrOnResult(result, options);
},

getCurrentOutput(): StdErrAndOutString {
Expand All @@ -208,17 +235,25 @@ export const runContinuous = function (
},

async waitUntil(fn: ConditionFunction) {
await new Promise<void>(resolve => {
const check = (state: StdErrAndOutString) => {
await new Promise<void>((resolve, reject) => {
const check: CheckerFunction = state => {
if (fn(state)) {
pending.delete(check);
pendingRejection.delete(check);
resolve();
}
};
const error = new ErrorWithStack(
'Process exited',
continuousRun.waitUntil,
);
Comment on lines +246 to +249
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create the error here to point back to the waitUntil

image

pendingRejection.set(check, () => reject(error));
pending.add(check);
});
},
};

return continuousRun;
};

// return type matches output of logDebugMessages
Expand Down