Skip to content

Commit

Permalink
Fix missing error after test
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 28, 2019
1 parent df3eb5e commit 47fc113
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,8 @@
- `[jest-worker]` Fix `jest-worker` when using pre-allocated jobs ([#7934](https://github.com/facebook/jest/pull/7934))
- `[jest-changed-files]` Fix `getChangedFilesFromRoots` to not return parts of the commit messages as if they were files, when the commit messages contained multiple paragraphs ([#7961](https://github.com/facebook/jest/pull/7961))
- `[static]` Remove console log '-' on the front page
- `[jest-jasmine2]`: Throw explicit error when errors happen after test is considered complete ([#8005](https://github.com/facebook/jest/pull/8005))
- `[jest-circus]`: Throw explicit error when errors happen after test is considered complete ([#8005](https://github.com/facebook/jest/pull/8005))

### Chore & Maintenance

Expand Down
13 changes: 11 additions & 2 deletions e2e/__tests__/failures.test.ts
Expand Up @@ -12,9 +12,9 @@ import runJest from '../runJest';

const dir = path.resolve(__dirname, '../failures');

const normalizeDots = text => text.replace(/\.{1,}$/gm, '.');
const normalizeDots = (text: string) => text.replace(/\.{1,}$/gm, '.');

function cleanStderr(stderr) {
function cleanStderr(stderr: string) {
const {rest} = extractSummary(stderr);
return rest
.replace(/.*(jest-jasmine2|jest-circus).*\n/g, '')
Expand Down Expand Up @@ -182,3 +182,12 @@ test('works with named snapshot failures', () => {
wrap(result.substring(0, result.indexOf('Snapshot Summary'))),
).toMatchSnapshot();
});

test('errors after test has completed', () => {
const {stderr} = runJest(dir, ['errorAfterTestComplete.test.js']);

expect(stderr).toMatch(
/Error: Caught error after test environment was torn down/,
);
expect(stderr).toMatch(/Failed: "fail async"/);
});
14 changes: 14 additions & 0 deletions e2e/failures/__tests__/errorAfterTestComplete.test.js
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+jsinfra
*/
'use strict';

test('a failing test', done => {
setTimeout(() => done('fail async'), 5);
done();
});
37 changes: 28 additions & 9 deletions packages/jest-circus/src/utils.ts
Expand Up @@ -153,12 +153,17 @@ const _makeTimeoutMessage = (timeout: number, isHook: boolean) =>
// the original values in the variables before we require any files.
const {setTimeout, clearTimeout} = global;

function checkIsError(error: any): error is Error {
return !!(error && (error as Error).message && (error as Error).stack);
}

export const callAsyncCircusFn = (
fn: AsyncFn,
testContext: TestContext | undefined,
{isHook, timeout}: {isHook?: boolean | null; timeout: number},
): Promise<any> => {
let timeoutID: NodeJS.Timeout;
let completed = false;

return new Promise((resolve, reject) => {
timeoutID = setTimeout(
Expand All @@ -170,15 +175,26 @@ export const callAsyncCircusFn = (
// soon as `done` called.
if (fn.length) {
const done = (reason?: Error | string): void => {
const isError =
reason && (reason as Error).message && (reason as Error).stack;
return reason
? reject(
isError
? reason
: new Error(`Failed: ${prettyFormat(reason, {maxDepth: 3})}`),
)
: resolve();
let errorAsErrorObject: Error;

if (checkIsError(reason)) {
errorAsErrorObject = reason;
} else {
errorAsErrorObject = new Error(
`Failed: ${prettyFormat(reason, {maxDepth: 3})}`,
);
}

// Consider always throwing, regardless if `reason` is set or not
if (completed && reason) {
errorAsErrorObject.message =
'Caught error after test environment was torn down\n\n' +
errorAsErrorObject.message;

throw errorAsErrorObject;
}

return reason ? reject(errorAsErrorObject) : resolve();
};

return fn.call(testContext, done);
Expand Down Expand Up @@ -230,6 +246,9 @@ export const callAsyncCircusFn = (
timeoutID.unref && timeoutID.unref();
clearTimeout(timeoutID);
throw error;
})
.finally(() => {
completed = true;
});
};

Expand Down
15 changes: 13 additions & 2 deletions packages/jest-jasmine2/src/jasmine/Env.js
Expand Up @@ -586,13 +586,24 @@ export default function(j$) {
message = check.message;
}

currentRunnable().addExpectationResult(false, {
const errorAsErrorObject = checkIsError ? error : new Error(message);
const runnable = currentRunnable();

if (!runnable) {
errorAsErrorObject.message =
'Caught error after test environment was torn down\n\n' +
errorAsErrorObject.message;

throw errorAsErrorObject;
}

runnable.addExpectationResult(false, {
matcherName: '',
passed: false,
expected: '',
actual: '',
message,
error: checkIsError ? error : new Error(message),
error: errorAsErrorObject,
});
};
}
Expand Down

0 comments on commit 47fc113

Please sign in to comment.