jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: Jest used with Babel (babel-jest) and TypeScript does not properly handle Object.prototype

Open SmashingQuasar opened this issue 1 year ago • 8 comments

Version

28.1.3

Steps to reproduce

  1. Clone this sample repository: https://github.com/SmashingQuasar/jest-object-prototype
  2. (optional) If using Docker, launch ./docker/up.sh. It will start a container properly setup using the correct Node.JS version (16.17.0).
  3. (optional) If using Docker, enter the container using docker exec -it jest-object-prototype bash.
  4. Launch yarn to install all dependencies.
  5. 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 

SmashingQuasar avatar Aug 22 '22 16:08 SmashingQuasar

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.

github-actions[bot] avatar Sep 21 '22 17:09 github-actions[bot]

I am commenting to indicate this is still an issue. We have found no workaround aside from migrating to another testing framework...

SmashingQuasar avatar Sep 29 '22 12:09 SmashingQuasar

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.

github-actions[bot] avatar Oct 29 '22 12:10 github-actions[bot]

This is still an issue.

SmashingQuasar avatar Nov 13 '22 23:11 SmashingQuasar

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.

github-actions[bot] avatar Dec 13 '22 23:12 github-actions[bot]

This is still an issue.

SmashingQuasar avatar Dec 13 '22 23:12 SmashingQuasar

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.

github-actions[bot] avatar Jan 13 '23 00:01 github-actions[bot]

This is still an issue.

SmashingQuasar avatar Jan 13 '23 09:01 SmashingQuasar

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.

github-actions[bot] avatar Feb 12 '23 09:02 github-actions[bot]

This is still an issue.

SmashingQuasar avatar Feb 16 '23 13:02 SmashingQuasar

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.

github-actions[bot] avatar Mar 18 '23 14:03 github-actions[bot]

This is still an issue.

SmashingQuasar avatar Mar 24 '23 09:03 SmashingQuasar

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.

github-actions[bot] avatar Apr 23 '23 09:04 github-actions[bot]

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?

SmashingQuasar avatar Apr 23 '23 17:04 SmashingQuasar

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 (;

mrazauskas avatar Apr 23 '23 18:04 mrazauskas

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.

SmashingQuasar avatar Apr 24 '23 19:04 SmashingQuasar

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.

github-actions[bot] avatar May 24 '23 20:05 github-actions[bot]

This is still an issue.

SmashingQuasar avatar Jun 20 '23 11:06 SmashingQuasar

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.

github-actions[bot] avatar Jul 20 '23 12:07 github-actions[bot]

This is still an issue.

SmashingQuasar avatar Jul 28 '23 08:07 SmashingQuasar

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

mrazauskas avatar Jul 28 '23 08:07 mrazauskas

As mentioned, the problem is tracked in #2549. Closing this one to reduce duplication.

mrazauskas avatar Jul 28 '23 10:07 mrazauskas

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.

github-actions[bot] avatar Aug 28 '23 00:08 github-actions[bot]