Skip to content

Commit 080df3d

Browse files
committed
add tests
1 parent 71e47b0 commit 080df3d

6 files changed

Lines changed: 25 additions & 6 deletions

File tree

e2e/__tests__/__snapshots__/failures.test.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ FAIL __tests__/duringTests.test.js
254254
at Object.test (__tests__/duringTests.test.js:45:1)
255255
`;
256256
257+
exports[`not throwing Error objects 6`] = `
258+
FAIL __tests__/throwObjectWithStackProp.test.js
259+
● Test suite failed to run
260+
261+
42
262+
`;
263+
257264
exports[`works with assertions in separate files 1`] = `
258265
FAIL __tests__/testMacro.test.js
259266
✕ use some imported macro to make assertion

e2e/__tests__/failures.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ test('not throwing Error objects', () => {
5151
}
5252

5353
expect(wrap(cleanStderr(stderr))).toMatchSnapshot();
54+
stderr = runJest(dir, ['throwObjectWithStackProp.test.js']).stderr;
55+
expect(wrap(cleanStderr(stderr))).toMatchSnapshot();
5456
});
5557

5658
test('works with node assert', () => {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
// eslint-disable-next-line no-throw-literal
11+
throw {stack: 42};

packages/jest-jasmine2/src/__tests__/__snapshots__/expectationResultFactory.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`expectationResultFactory returns the result if failed (with \`error.stack\` not as a string). 1`] = `
44
"thrown: Object {
5-
\\"stack\\": Array [],
5+
\\"stack\\": 42,
66
}"
77
`;
88

packages/jest-jasmine2/src/__tests__/expectationResultFactory.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('expectationResultFactory', () => {
8282
it('returns the result if failed (with `error.stack` not as a string).', () => {
8383
const options = {
8484
actual: 'Fail',
85-
error: {stack: []},
85+
error: {stack: 42},
8686
expected: 'Pass',
8787
matcherName: 'testMatcher',
8888
passed: false,

packages/jest-message-util/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ const removeBlankErrorLine = (str: string) =>
380380
// Error object, so we have to regexp out the message from the stack string
381381
// to format it.
382382
export const separateMessageFromStack = (
383-
content: string,
383+
content: unknown,
384384
): {message: string; stack: string} => {
385385
if (!content) {
386386
return {message: '', stack: ''};
@@ -390,9 +390,8 @@ export const separateMessageFromStack = (
390390
// (maybe it's a code frame instead), just the first non-empty line.
391391
// If the error is a plain "Error:" instead of a SyntaxError or TypeError we
392392
// remove the prefix from the message because it is generally not useful.
393-
const messageMatch = content.match(
394-
/^(?:Error: )?([\s\S]*?(?=\n\s*at\s.*:\d*:\d*)|\s*.*)([\s\S]*)$/,
395-
);
393+
const ERROR_REGEXP = /^(?:Error: )?([\s\S]*?(?=\n\s*at\s.*:\d*:\d*)|\s*.*)([\s\S]*)$/;
394+
const messageMatch = typeof content !== 'string' ? `${content}`.match(ERROR_REGEXP) : content.match(ERROR_REGEXP);
396395
if (!messageMatch) {
397396
// For typescript
398397
throw new Error('If you hit this error, the regex above is buggy.');

0 commit comments

Comments
 (0)