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

Jest ESM #1247

Closed
songz opened this issue Dec 31, 2021 · 5 comments
Closed

Jest ESM #1247

songz opened this issue Dec 31, 2021 · 5 comments
Assignees
Labels
blocked for issues for which we could not do HARD really difficult problems

Comments

@songz
Copy link
Contributor

songz commented Dec 31, 2021

Fix Jest + Typescript + ESM. Currently our jest does not support esm, which results in hacks like these, which mocks out the library instead of generating snapshots with them.

@hwong0305
Copy link
Collaborator

Needs to be done after #1264 since it enables ESM on nextjs

@flacial flacial self-assigned this Jan 27, 2022
@flacial flacial added the HARD really difficult problems label Jan 28, 2022
@flacial
Copy link
Member

flacial commented Jan 31, 2022

Things for the engineers to be aware of:

@flacial
Copy link
Member

flacial commented Feb 3, 2022

Here's what I did:

  1. Enable ---experimental-modules flag to add support for ESM

package.json:

  "scripts": {
    "..."
    "test": "TZ=UTC node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --runInBand",
    "..."
  },
  1. Specify jsx and tsx files to be treated as ESM

jest.config.ts:

extensionsToTreatAsEsm: ['.ts', '.jsx', '.tsx']
  1. Imported jest in 70+ files
import { jest } from '@jest/globals'
  1. Use ts-node-dev instead of ts-node to be able to keep jest.config as a TypeScript file
  "scripts": {
    "..."
    "ts-node": "ts-node-dev --compiler-options {\\\"module\\\":\\\"CommonJS\\\"}",
    "..."
  },

Main errors to be fixed:

  1. require is not defined: It occurs when jest.mock is used.
  2. The module factory of jest.mock() is not allowed to reference any out-of-scope variables.
      18 | import Title from '../../../../components/Title'
      19 | jest.mock('../../../../components/Title', () => {
    > 20 |   return jest.fn(() => null)
         |          ^^^^
      21 | })
      22 | import {
      23 |   getSubLessonSlugs,
  1. SyntaxError: The requested module '@prisma/client' does not provide an export named 'PrismaClient'
  2. Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. - components/AlertsDisplay.test.js
  3. TypeError: Cannot assign to read only property 'withScope' of object '[object Module]' - helpers/apolloLogPlugin.test.js

@flacial
Copy link
Member

flacial commented Feb 4, 2022

We'll wait for jestjs/jest#10976 and jestjs/jest#9430 to be resolved.

Then we can continue from where we stopped.

@songz songz added the blocked for issues for which we could not do label Mar 16, 2022
@songz
Copy link
Contributor Author

songz commented Mar 16, 2022

labeled as blocked and closing for now. We'll tackle this when this becomes an issue again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked for issues for which we could not do HARD really difficult problems
Projects
None yet
Development

No branches or pull requests

3 participants