diff --git a/CHANGELOG.md b/CHANGELOG.md index ced77a5d4a04..f0fa49a26b8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,8 @@ ### Fixes +* `[jest-jasmine2]` Added assertion error handling inside `afterAll hook` + ([#5884](https://github.com/facebook/jest/pull/5884)) * `[jest-cli]` Remove the notifier actions in case of failure when not in watch mode. ([#5861](https://github.com/facebook/jest/pull/5861)) * `[jest-mock]` Extend .toHaveBeenCalled return message with outcome diff --git a/integration-tests/__tests__/lifecycles.js b/integration-tests/__tests__/lifecycles.js new file mode 100644 index 000000000000..c2979d9c2e55 --- /dev/null +++ b/integration-tests/__tests__/lifecycles.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ +'use strict'; + +const runJest = require('../runJest'); + +test('suite with invalid assertions in afterAll', () => { + const {stderr, status} = runJest('lifecycles'); + + expect(status).toBe(1); + expect(stderr).toMatch(/afterAll just failed!/); +}); diff --git a/integration-tests/lifecycles/__tests__/index.js b/integration-tests/lifecycles/__tests__/index.js new file mode 100644 index 000000000000..b56049f6930b --- /dev/null +++ b/integration-tests/lifecycles/__tests__/index.js @@ -0,0 +1,12 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + */ + +afterAll(() => { + throw new Error('afterAll just failed!'); +}); +test('one', () => {}); +test('two', () => {}); diff --git a/integration-tests/lifecycles/index.js b/integration-tests/lifecycles/index.js new file mode 100644 index 000000000000..4fd52298391e --- /dev/null +++ b/integration-tests/lifecycles/index.js @@ -0,0 +1,9 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + */ + +module.exports = () => {}; diff --git a/integration-tests/lifecycles/package.json b/integration-tests/lifecycles/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/integration-tests/lifecycles/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/jest-jasmine2/src/jasmine/js_api_reporter.js b/packages/jest-jasmine2/src/jasmine/js_api_reporter.js index 980f15ee6d98..e2fca8ef35e6 100644 --- a/packages/jest-jasmine2/src/jasmine/js_api_reporter.js +++ b/packages/jest-jasmine2/src/jasmine/js_api_reporter.js @@ -54,7 +54,14 @@ export default function JsApiReporter(options: Object) { let executionTime; + function validateAfterAllExceptions({failedExpectations}) { + if (failedExpectations && failedExpectations.length > 0) { + throw failedExpectations[0]; + } + } + this.jasmineDone = function(runDetails) { + validateAfterAllExceptions(runDetails); this.finished = true; this.runDetails = runDetails; executionTime = timer.elapsed();