jest
jest copied to clipboard
[Bug]: Jest used with Babel (babel-jest) and TypeScript does not properly handle Object.prototype
Version
28.1.3
Steps to reproduce
- Clone this sample repository: https://github.com/SmashingQuasar/jest-object-prototype
- (optional) If using Docker, launch
./docker/up.sh
. It will start a container properly setup using the correct Node.JS version (16.17.0). - (optional) If using Docker, enter the container using
docker exec -it jest-object-prototype bash
. - Launch
yarn
to install all dependencies. - Launch
yarn test
to run tests.
The two first tests should succeed, the three last tests should fail.
Expected behavior
When you run the following code inside Node.JS (you can even use the provided container), it will log true
.
This code reproduces the test #3 that fails. It is the exact same evaluation.
console.log(Object.getPrototypeOf(await (new Response(JSON.stringify({}))).json()) === Object.prototype);
Actual behavior
When used inside Jest with the provided repository and configuration, it is equal to false
.
You can replace the unit tests by console.log
if you want to avoid using expect()
. I only used those to provide a properly designed test suite.
It seems something is hijacking Object.prototype
or Object.getPrototypeOf()
.
Additional context
We need to use Object.prototype
in correlation with Object.getPrototypeOf()
on a await response.json()
return.
I added a test using instanceof Object
to demonstrate the probable Object.prototype hijacking.
This repository is using the latest Node LTS (Gallium 16.17.0).
Environment
System:
OS: Linux 5.10 Debian GNU/Linux 10 (buster) 10 (buster)
CPU: (4) arm64 unknown
Binaries:
Node: 16.17.0 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 8.15.0 - /usr/local/bin/npm
npmPackages:
jest: ^28.1.3 => 28.1.3
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
I am commenting to indicate this is still an issue. We have found no workaround aside from migrating to another testing framework...
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This is still an issue.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This is still an issue.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This is still an issue.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This is still an issue.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This is still an issue.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This is still an issue.
We are closing in on a year for this issue that is still opened and still requires triaged. Is Jest still being maintained?
I think you are hitting #2549
Using jest-light-runner
to the run tests which need these checks in a separate project could be a solution. Reference: https://github.com/facebook/jest/issues/2549#issuecomment-1098071474
We are closing in on a year for this issue that is still opened and still requires triaged. Is Jest still being maintained?
There are no payed employees at Jest. All the job is done by volunteers here. I don’t think it is fair to complain. Remember to sponsor the project if it creates value for you (;
Thanks for your answer, I will look into your solution.
There are no payed employees at Jest. All the job is done by volunteers here. I don’t think it is fair to complain. Remember to sponsor the project if it creates value for you (;
Jest is the test runner for React which is made and maintained by Facebook. A large part of the developers who are using Jest did not choose to use it nor had a choice. They were constrained to use Jest because of how aggressively it has been enforced within React. Jest was also originally (and for most of it's existence) maintained by Facebook (Meta). It was transferred to the OpenJS Foundation only 3 months prior to the creation of this issue. Whilst I understand that it is no longer by supported by Meta, the fact is, it has been forced on many people's environment and stack without much choice. This is the legacy the current maintainers of Jest inherited. It's not their fault, that's true, but it's not like a lot of people had a choice.
I moved on from Jest for the backend
a long time ago because of this issue (there are other reasons as well) so I am following this issue mostly because I know it will be problematic the next time I have to deal with React.
All in all, it's entirely fair to complain considering the legacy of Jest. It is also entirely fair for the current maintainers to not maintain it on a regular basis and they owe us nothing. However, it would be nice to write down a message saying maintaining the solution is currently on hold and will resume eventually. Again, not attacking anyone here, just sharing my side of the coin.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This is still an issue.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This is still an issue.
It fails without TypeScript and without Babel (try removing types and adding transform: {}
).
I think you are hitting #2549.
One of the solution could be using jest-light-runner
for the tests which need instanceof
. It makes the tests in your reproduction to pass. Reference: https://github.com/jestjs/jest/issues/2549#issuecomment-1098071474
EDIT Hm.. Seems like we talk about that already ;D
As mentioned, the problem is tracked in #2549. Closing this one to reduce duplication.
This issue 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.