Skip to content

Commit

Permalink
Handling pending calls inside async tests properly. (#6782)
Browse files Browse the repository at this point in the history
  • Loading branch information
matrushka authored and SimenB committed Oct 8, 2018
1 parent 2d972e5 commit c4646b9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- `[jest-circus]` Fail synchronous hook timeouts ([#7074](https://github.com/facebook/jest/pull/7074))
- `[jest-jasmine2]` Fail synchronous test timeouts ([#7074](https://github.com/facebook/jest/pull/7074))
- `[jest-jasmine2]` Better error message when a describe block is empty ([#6372](https://github.com/facebook/jest/pull/6372))
- `[jest-jasmine2]` Pending calls inside async tests are reported as pending not failed ([#6782](https://github.com/facebook/jest/pull/6782))
- `[jest-circus]` Better error message when a describe block is empty ([#6372](https://github.com/facebook/jest/pull/6372))
- `[jest-cli]` Fix unhandled error when a bad revision is provided to `changedSince` ([#7115](https://github.com/facebook/jest/pull/7115))

Expand Down
20 changes: 20 additions & 0 deletions e2e/__tests__/jasmine_async_with_pending_during_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* @flow */

'use strict';

import {json as runWithJson} from '../runJest';
import {skipSuiteOnJestCircus} from '../../scripts/ConditionalTest';

describe('async jasmine with pending during test', () => {
skipSuiteOnJestCircus();

it('should be reported as a pending test', () => {
const result = runWithJson('jasmine-async', ['pending_in_promise.test.js']);
const json = result.json;

expect(json.numTotalTests).toBe(1);
expect(json.numPassedTests).toBe(0);
expect(json.numFailedTests).toBe(0);
expect(json.numPendingTests).toBe(1);
});
});
6 changes: 6 additions & 0 deletions e2e/jasmine-async/__tests__/pending_in_promise.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

it('skips a test inside a promise', () =>
new Promise(() => {
pending('skipped a test inside a promise');
}));
14 changes: 10 additions & 4 deletions packages/jest-jasmine2/src/jasmine_async.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function promisifyLifeCycleFunction(originalFn, env) {

// Similar to promisifyLifeCycleFunction but throws an error
// when the return value is neither a Promise nor `undefined`
function promisifyIt(originalFn, env) {
function promisifyIt(originalFn, env, jasmine) {
return function(specName, fn, timeout) {
if (!fn) {
const spec = originalFn.call(env, specName);
Expand Down Expand Up @@ -102,7 +102,13 @@ function promisifyIt(originalFn, env) {
if (message) {
extraError.message = message;
}
done.fail(isError ? error : extraError);

if (jasmine.Spec.isPendingSpecException(error)) {
env.pending(message);
done();
} else {
done.fail(isError ? error : extraError);
}
});
} else if (returnValue === undefined) {
done();
Expand Down Expand Up @@ -146,8 +152,8 @@ export function install(global: Global) {
const jasmine = global.jasmine;

const env = jasmine.getEnv();
env.it = promisifyIt(env.it, env);
env.fit = promisifyIt(env.fit, env);
env.it = promisifyIt(env.it, env, jasmine);
env.fit = promisifyIt(env.fit, env, jasmine);
global.it.concurrent = makeConcurrent(env.it, env);
global.it.concurrent.only = makeConcurrent(env.fit, env);
global.it.concurrent.skip = makeConcurrent(env.xit, env);
Expand Down

0 comments on commit c4646b9

Please sign in to comment.