Skip to content

Commit

Permalink
Fix jest crashing when potentialError is null (#7049)
Browse files Browse the repository at this point in the history
  • Loading branch information
natealcedo authored and SimenB committed Sep 26, 2018
1 parent 0cd757b commit 2b216e4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- `[jest-mock]` Fix inheritance of static properties and methods in mocks ([#7003](https://github.com/facebook/jest/pull/7003))
- `[jest-mock]` Fix mocking objects without `Object.prototype` in their prototype chain ([#7003](https://github.com/facebook/jest/pull/7003))
- `[jest-cli]` Update jest-cli to show git ref in message when using `changedSince` ([#7028](https://github.com/facebook/jest/pull/7028))
- `[jest-jasmine2`] Fix crash when test return Promise rejected with null ([#7049](https://github.com/facebook/jest/pull/7049))

### Chore & Maintenance

Expand Down
32 changes: 32 additions & 0 deletions e2e/__tests__/promise_reject.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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';
import runJest from '../runJest';
import path from 'path';

const {cleanup, writeFiles} = require('../Utils');
const DIR = path.resolve('../promise-reject');

beforeEach(() => cleanup(DIR));
afterEach(() => cleanup(DIR));

test('', () => {
writeFiles(DIR, {
'package.json': '{}',
'promise_reject.test.js': `
test('test', () => {
return Promise.reject(null)
});
`,
});
const {stdout, stderr, status} = runJest(DIR);
expect(stdout).toBe('');
expect(stderr).toMatch(/(Failed|thrown): null/);
expect(status).toBe(1);
});
5 changes: 5 additions & 0 deletions e2e/promise-reject/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
2 changes: 2 additions & 0 deletions packages/jest-jasmine2/src/is_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import prettyFormat from 'pretty-format';
export default function isError(potentialError: any) {
// duck-type Error, see #2549
const isError =
potentialError !== null &&
typeof potentialError === 'object' &&
typeof potentialError.message === 'string' &&
typeof potentialError.name === 'string';

const message = isError
? null
: `Failed: ${prettyFormat(potentialError, {maxDepth: 3})}`;
Expand Down

0 comments on commit 2b216e4

Please sign in to comment.