Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade jest #1892

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config-jest/setup/shims.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
require('airbnb-js-shims/target/es2019');

const { TextEncoder, TextDecoder } = require('util');

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
Comment on lines +3 to +6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had to add this because jest-environment-jsdom does not contain TextEncoder and TextDecorder, but react requires them.

10 changes: 8 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('jest').Config} */
module.exports = {
bail: false,
collectCoverageFrom: [
Expand Down Expand Up @@ -39,8 +40,13 @@ module.exports = {
setupFiles: ['<rootDir>/config-jest/setup/shims.js', '<rootDir>/config-jest/setup/console.js'],
setupFilesAfterEnv: ['<rootDir>/config-jest/setup/enzyme.js'],
testEnvironment: 'jsdom',
testURL: 'http://localhost',
timers: 'fake',
testEnvironmentOptions: {
url: 'http://localhost',
},
fakeTimers: {
enableGlobally: true,
legacyFakeTimers: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is only a single test that fails when using the modern mock timers, but it wasn't immediately obvious how to fix this (probably just jest.useRealTimers()?), so I've just kept using the legacy fake timers for now which were the default with jest 25.

expect(data[0].date.constructor).toEqual(Date);

failed with

 FAIL  packages/visx-mock-data/test/genDateValue.test.ts
  ● generators/genDateValue › should return [{ date, value }]

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

    Expected: [Function ClockDate]
    Received: [Function Date]

      19 |     const n = 1;
      20 |     const data = genDateValue(n);
    > 21 |     expect(data[0].date.constructor).toEqual(Date);
         |                                      ^
      22 |     expect(typeof data[0].value).toBe('number');
      23 |   });
      24 |

      at Object.toEqual (packages/visx-mock-data/test/genDateValue.test.ts:21:38)

There is another test referencing fake timers, so that one should be revisited as well when switching from legacy to modern fake timers:

// fake timers in Jest 25 are completely unusable so I'm using real timers here
// when it's upgraded should be updated to use advanceTimersByTime

},
verbose: false,
testPathIgnorePatterns: ['<rootDir>/packages/visx-demo'],
transformIgnorePatterns: [
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
"@testing-library/react": "^16.1.0",
"@testing-library/user-event": "^13.5.0",
"@types/enzyme": "^3.10.3",
"@types/jest": "^24.0.18",
"@types/jsdom": "^12.2.4",
"@types/jest": "^29.5.14",
"@types/jsdom": "^21.1.7",
"@types/node-fetch": "1.6.9",
"@typescript-eslint/eslint-plugin": "^5.46.1",
"@typescript-eslint/parser": "^5.46.1",
Expand All @@ -88,7 +88,7 @@
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.1.7",
"eslint-plugin-jest": "^28.9.0",
"eslint-plugin-jsx-a11y": "^6.6.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.1.1",
Expand All @@ -99,7 +99,8 @@
"filesize": "6.1.0",
"fs-jetpack": "^1.3.0",
"husky": "^3.0.0",
"jest": "^25.5.4",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest-mock-console": "^1.0.1",
"lerna": "^7.0.2",
"marked": "^0.7.0",
Expand Down
Loading
Loading