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

Don't format node assert errors when there's no 'assert' module #4376

Merged

Conversation

suchipi
Copy link
Contributor

@suchipi suchipi commented Aug 27, 2017

Summary

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

Test plan

I have created a test that mocks assert as a module that throws when loaded, but I am not confident that the mock is installed at the right time, or that I am testing the right part of the code. I could use some direction here.

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 jestjs#4365
@codecov-io
Copy link

codecov-io commented Aug 27, 2017

Codecov Report

Merging #4376 into master will decrease coverage by 0.38%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4376      +/-   ##
==========================================
- Coverage    56.2%   55.82%   -0.39%     
==========================================
  Files         191      189       -2     
  Lines        6421     6386      -35     
  Branches        6        6              
==========================================
- Hits         3609     3565      -44     
- Misses       2809     2818       +9     
  Partials        3        3
Impacted Files Coverage Δ
...kages/jest-circus/src/format_node_assert_errors.js 0% <0%> (ø) ⬆️
...ckages/jest-cli/src/reporters/get_result_header.js 78.57% <0%> (-3.79%) ⬇️
...ackages/jest-cli/src/reporters/default_reporter.js 91.04% <0%> (-0.39%) ⬇️
packages/jest-cli/src/reporters/Status.js 93.75% <0%> (-0.19%) ⬇️
packages/jest-config/src/index.js 0% <0%> (ø) ⬆️
packages/jest-config/src/normalize.js 78.26% <0%> (ø) ⬆️
...ges/jest-cli/src/reporters/get_snapshot_summary.js
...ages/jest-cli/src/reporters/get_snapshot_status.js
packages/jest-cli/src/reporters/utils.js 62.5% <0%> (+1.11%) ⬆️
...ackages/jest-cli/src/reporters/summary_reporter.js 19.69% <0%> (+7.69%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e535665...1873352. Read the comment docs.

@ide
Copy link
Contributor

ide commented Aug 27, 2017

React Native's Metro bundler looks for all require() calls statically. The issue is that if it statically finds "require('assert')" anywhere in the code, Metro bundler will fail to build the bundle.

@suchipi
Copy link
Contributor Author

suchipi commented Aug 27, 2017

Hmm. Does Metro have anything like webpack's require.resolveWeak?

@suchipi
Copy link
Contributor Author

suchipi commented Aug 27, 2017

Maybe a better solution would be to pull format_node_assert_errors out so that jest-circus doesn't try to use it out of the box. We could still use addEventListener to put it back in in the places we need it (node).

@cpojer
Copy link
Member

cpojer commented Aug 27, 2017

What about require.call(null, 'assert')?

@suchipi
Copy link
Contributor Author

suchipi commented Aug 27, 2017

Looks like that works (I tested via create-react-native-app); I'll change this to use that.

suchipi and others added 2 commits August 27, 2017 13:45
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.
@cpojer
Copy link
Member

cpojer commented Aug 27, 2017

Do you mind just getting rid of this test? It isn't really useful in its current form and I'd like to merge this fix regardless.

@suchipi
Copy link
Contributor Author

suchipi commented Aug 27, 2017

Done

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Figuring out how to handle the "assert" dependency in RN
5 participants