-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jest-message-util): Add support for Error cause in
test
and `i…
…t` (#13935)
- Loading branch information
Showing
10 changed files
with
487 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
'use strict'; | ||
|
||
function buildErrorWithCause(message: string, opts: {cause: unknown}): Error { | ||
const error = new Error(message, opts); | ||
if (opts.cause !== error.cause) { | ||
// Error with cause not supported in legacy versions of node, we just polyfill it | ||
Object.assign(error, opts); | ||
} | ||
return error; | ||
} | ||
|
||
function g() { | ||
throw new Error('error during g'); | ||
} | ||
function f() { | ||
try { | ||
g(); | ||
} catch (err) { | ||
throw buildErrorWithCause('error during f', {cause: err}); | ||
} | ||
} | ||
|
||
test('error with cause in test', () => { | ||
f(); | ||
}); | ||
|
||
describe('describe block', () => { | ||
it('error with cause in describe/it', () => { | ||
f(); | ||
}); | ||
|
||
it('error with string cause in describe/it', () => { | ||
throw buildErrorWithCause('with string cause', { | ||
cause: 'here is the cause', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
'use strict'; | ||
|
||
function buildErrorWithCause(message: string, opts: {cause: unknown}): Error { | ||
const error = new Error(message, opts); | ||
if (opts.cause !== error.cause) { | ||
// Error with cause not supported in legacy versions of node, we just polyfill it | ||
Object.assign(error, opts); | ||
} | ||
return error; | ||
} | ||
|
||
function g() { | ||
throw new Error('error during g'); | ||
} | ||
function f() { | ||
try { | ||
g(); | ||
} catch (err) { | ||
throw buildErrorWithCause('error during f', {cause: err}); | ||
} | ||
} | ||
|
||
describe('error with cause in describe', () => { | ||
f(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.