Skip to content

Latest deployment(1.0.0) broken ESM import in Jest/Enzyme #4014

Open
@CodeMedic42

Description

I have a library which I just converted over to ESM(forced to because of another dependency). It took a LONG time to get it all working(stupid ecosystem change). But since the latest update to cheerio my Jest tests are failing. I am not sure what changed in cheerio as I do not use it directly. I use enzyme which has a dependency on cheerio. All this will make giving a working example VERY difficult. I can give you the error I am getting and I can try and give you the particulars of my set up. If there is anything else I can give you please let me know.

Here is the error

Must use import to load ES Module: /Users/me/source/.../my-project/node_modules/.pnpm/[email protected]/node_modules/cheerio/dist/browser/index.js

Here is roughly my setup

packge.json

{
    ...
    "scripts": {
        "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
    },
    "devDependencies": {
        "@jest/globals": "^29.7.0",
        "enzyme": "3.11.0",
        "enzyme-adapter-react-16": "1.15.7",
        "jest": "^29.7.0",
        "jest-environment-jsdom": "29.7.0",
        "ts-jest": "^29.1.5",
        "typescript": "^5.4.5",
    }
}

jest.config.mjs

import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default {
  passWithNoTests: true,
  preset: 'ts-jest',
  testEnvironment: 'jsdom',
  extensionsToTreatAsEsm: ['.ts', '.tsx'],
  transform: {
    '^.+\\.m?[tj]sx?$': [
      'ts-jest',
      {
        useESM: true,
      },
    ],
  },
  setupFilesAfterEnv: [
    path.join(__dirname, 'setup.mjs')
  ],
  moduleNameMapper: {
    '^uuid$': '<rootDir>/node_modules/uuid/wrapper.mjs'
  }
};

setup.mjs

import { jest } from '@jest/globals';

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

const enzymeMountPoint = window.document.createElement('div');
enzymeMountPoint.setAttribute('id', 'enzyme-mount-point');
document.body.appendChild(enzymeMountPoint);

let wrapper = null;

// eslint-disable-next-line no-undef
global.beforeEach(() => {
    console.log = jest.fn();
    console.warn = jest.fn();
});

// eslint-disable-next-line no-undef
global.afterEach(() => {
    if (wrapper === null || wrapper === undefined) {
        return;
    }

    wrapper.detach();

    wrapper = null;
});

function mountTo(component) {
    wrapper = Enzyme.mount(component, { attachTo: enzymeMountPoint })

    return wrapper;
}

global.mountTo = mountTo;

tsconfig.json

{
    "compilerOptions": {
        "target": "es2020",
        "lib": ["ES2020", "DOM", "DOM.Iterable"],
        "module": "ES2020",
        "moduleResolution": "Bundler",
        "jsx": "react-jsx",
        "baseUrl": "./src",
        "noEmit": true,
        "isolatedModules": true,
        "esModuleInterop": true,
        "strict": true,
        "skipLibCheck": true,
        "checkJs": false,
        "allowJs": true
    },
    "include": ["**/*", "*"],
}

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions