jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.12k stars 6.44k forks source link

[Bug]: Incorrect Error Stacktrace when using Object.freeze(Object.prototype) #15141

Closed kamilzki closed 1 month ago

kamilzki commented 3 months ago

Version

^29.7.0

Steps to reproduce

Hello! I encountered an issue when running Jest tests with Object.freeze(Object.prototype) in my code. The error stacktrace produced by a failing test is not correctly pointing to the location of the error in the test file. Here is a simplified example to reproduce the issue:

// example.js
Object.freeze(Object.prototype);

function add(a, b) {
  return a + b;
}

module.exports = { add };

and one failing test:

// example.test.js
const { add } = require('./example');

describe('add', () => {
    it('adds two numbers', () => {
        expect(add(1, 2)).toEqual(3);
    });

    it('adds two numbers (to fail test)', () => {
        expect(add(1, 2)).toEqual(5);
    });
});

When Object.freeze(Object.prototype) is commented out, the error stacktrace correctly points to the line in the test file where the assertion failed ("Expected behavior").

Expected behavior

Error: expect(received).toEqual(expected) // deep equality

Expected: 5 Received: 3

at Object.toEqual ((...)/test-jest/example.test.js:9:27) ... ### Actual behavior Error: expect(received).toEqual(expected) // deep equality Expected: 5 Received: 3 at [object Object] at [object Object] at new Promise () at [object Object] ... ### Additional context It's difficult to use some solutions like https://github.com/snyk-labs/nopp or just `Object.freeze(Object.prototype)` which are protecting against the Prototype Pollution. ### Environment ```shell System: OS: Linux 5.15 Ubuntu 20.04 LTS (Focal Fossa) CPU: (12) x64 Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz Binaries: Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node Yarn: 1.22.19 - /mnt/c/Program Files/nodejs/yarn npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm npmPackages: jest: ^29.7.0 => 29.7.0 ```
github-actions[bot] commented 2 months ago

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] commented 1 month ago

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] commented 1 month ago

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] commented 1 week ago

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.