sinonjs / fake-timers

Fake setTimeout and friends (collectively known as "timers"). Useful in your JavaScript tests. Extracted from Sinon.JS
BSD 3-Clause "New" or "Revised" License
804 stars 106 forks source link

TypeError: Cannot set property performance of [object Object] which has only a getter #396

Closed alexgurr closed 3 years ago

alexgurr commented 3 years ago

What did you expect to happen? Code doesn't throw an error.

What actually happens Code throws an error

  TypeError: Cannot set property performance of [object Object] which has only a getter

      at uninstall (node_modules/@jest/fake-timers/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:496:36)
      at Object.clock.uninstall (node_modules/@jest/fake-timers/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1255:20)
      at FakeTimers.useRealTimers (node_modules/@jest/fake-timers/build/modernFakeTimers.js:114:19)
      at FakeTimers.dispose (node_modules/@jest/fake-timers/build/modernFakeTimers.js:70:10)

How to reproduce

 test('an example test', async () => {
        const clock = FakeTimers.install();

        clock.runAll();

        clock.uninstall();
    });
fatso83 commented 3 years ago

Hi, this snippet is not runnable at all. Would you make it possible for me to run by creating a snippet that is runnable to get the error you present? I would assume that trimming it down the the following is sufficient?

import FakeTimers from '@sinonjs/fake-timers';
 test('breaks in Jest', async () => {
        const clock = FakeTimers.install();
        clock.runAll();
        clock.uninstall();
    });

Then just do jest my-breaking.test.js? Make it simple to look at and someone might will :smile:

alexgurr commented 3 years ago

Yep, this should be fine. Updated my issue.

fatso83 commented 3 years ago

No, that's was not enough. Doing npx jest gave me this:


    /tmp/jest-test/my.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import FakeTimers from '@sinonjs/fake-timers';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module
alexgurr commented 3 years ago

Just switch the import to a require (I'm in a Babel environment and you're directly running with node).

const FakeTimers = require('@sinonjs/fake-timers');

If this works it'll be something to do with the create-react-app jest environment, in which case you can do:

npx create-react-app temp
cd temp/src
touch tmp.test.js
# add jest test code in the file above
cd ..
npm run test
fatso83 commented 3 years ago

Not reproducible.

Standalone case

$ cat example.test.js
const FakeTimers  = require('@sinonjs/fake-timers');
 test('breaks in Jest', async () => {
        const clock = FakeTimers.install();
        clock.runAll();
        clock.uninstall();
    });

$ npx jest
 PASS  ./example.test.js
  ✓ breaks in Jest (1 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.155 s
Ran all test suites.

$ cd ../

$  npx create-react-app temp
...

$ cp ../test/example.test.js src/

$ npm test
... (Pressing a) to run all tests

 PASS  src/example.test.js
  ✓ breaks in Jest (1 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.835 s
Ran all test suites related to changed files.
fatso83 commented 3 years ago

That was with Node v16. The first case was with the latest v7 of fake-timers and the latest was first done using whatever shipped with Jest (v6.2), and I then redid it using the latest v7 by installing it afterwards and rerunning.

Can reopen if you can provide a reproducible test case.