LearningTypeScript / projects

Hands-on real world projects that will help you exercise your knowledge of TypeScript.
https://learningtypescript.com/projects
244 stars 166 forks source link

🛠 Tooling: 01-syntactic-sugar tests fail on Node 19 #260

Closed rogue0137 closed 1 year ago

rogue0137 commented 1 year ago

Tooling Report Checklist

Expected

I expected the tests to run and fail when I ran npm run test -- 1.

Actual

I received this error:

 FAIL  01-syntactic-sugar/index.test.js
  ● Test suite failed to run

    TypeError: Cannot assign to read only property 'performance' of object '[object global]'

      at hijackMethod (../../../node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:946:32)
      at Object.install (../../../node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1733:17)
      at FakeTimers.useFakeTimers (../../../node_modules/@jest/fake-timers/build/modernFakeTimers.js:110:36)

So I went into jest.config.js.

The original file is:

module.exports = {
    fakeTimers: { enableGlobally: true },
    transform: {
        "^.+\\.(t|j)sx?$": [
            "@swc/jest",
            {
                jsc: {
                    target: "es2021",
                },
            },
        ],
    },
};

I changed it to:

module.exports = {
    fakeTimers: { enableGlobally: false },
    transform: {
        "^.+\\.(t|j)sx?$": [
            "@swc/jest",
            {
                jsc: {
                    target: "es2021",
                },
            },
        ],
    },
};

After changing enableGlobally from true to false, my tests failed the way I expected them to. I then fixed the obvious issues in my code and the tests passed.

Additional Info

I'm not really sure how to document this finding. Again, it should only affect users with node v19. I didn't try this out, but it was recommended in a post.

My node version is v19.9.0.

JoshuaKGoldberg commented 1 year ago

👋 thanks for filing this @rogue0137! This is new - and looks like perhaps a result of https://github.com/sinonjs/fake-timers/issues/441? Updating jest and ts-jest to their latest versions seems to fix it.