jest icon indicating copy to clipboard operation
jest copied to clipboard

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

Open kamilzki opened this issue 8 months ago • 0 comments

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 <Click to see difference>

at Object.toEqual ((...)/test-jest/example.test.js:9:27)
...

Actual behavior

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

Expected: 5 Received: 3 <Click to see difference>

at [object Object]
at [object Object]
at new Promise (<anonymous>)
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

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

kamilzki avatar Jun 20 '24 13:06 kamilzki