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

Fix jest crashing when potentialError is null #7049

Merged
merged 2 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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