Skip to content

Commit dfdca8b

Browse files
suchipicpojer
authored andcommitted
Don't format node assert errors when there's no 'assert' module (#4376)
* Don't format node assert errors when there's no 'assert' module In format_node_assert_errors.js, we require the 'assert' module to detect that an error thrown is an AssertionError, so that we can format assertion errors nicely. This creates an implicit dependency in the library on the assert module (a node builtin, but not available without adding a shim in browsers, React Native, etc). If we are running in an environment where assert is not available, we don't need to try to format assertion errors, so we can just bail. Detecting this case and performing the require in a try/catch removes the implicit dependency on assert. Fixes #4365 * Use require.call(null, 'assert') instead of require('assert') The React Native packager was attempting to pull in the assert module as a dependency when using `require('assert')`, but it does not do this if you use require.call(null, 'assert'), because it does not detect this pattern. * Update format_node_assert_errors.js * Remove unnecessary test
1 parent 51e0a24 commit dfdca8b

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

packages/jest-circus/src/format_node_assert_errors.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,22 @@ module.exports = (event: Event, state: State) => {
4343
switch (event.name) {
4444
case 'test_failure':
4545
case 'test_success': {
46+
let assert;
47+
try {
48+
// Use indirect require so that Metro Bundler does not attempt to
49+
// bundle `assert`, which does not exist in React Native.
50+
// eslint-disable-next-line no-useless-call
51+
assert = require.call(null, 'assert');
52+
} catch (error) {
53+
// We are running somewhere where `assert` isn't available, like a
54+
// browser or React Native. Since assert isn't available, presumably
55+
// none of the errors we get through this event listener will be
56+
// `AssertionError`s, so we don't need to do anything.
57+
break;
58+
}
59+
4660
event.test.errors = event.test.errors.map(error => {
47-
return error instanceof require('assert').AssertionError
61+
return error instanceof assert.AssertionError
4862
? assertionErrorMessage(error, {expand: state.expand})
4963
: error;
5064
});

0 commit comments

Comments
 (0)