jest-dom icon indicating copy to clipboard operation
jest-dom copied to clipboard

Upgrade to v28 complains "Cannot find module '@jest/expect'"

Open tettoffensive opened this issue 3 years ago • 5 comments

  • @testing-library/react version: 13.2.0
  • Testing Framework and version: jest 28.1.0
  • DOM Environment: "jest-environment-jsdom": "^28.1.0", "react": "^18.1.0",

Relevant code or config:

import { render } from '@testing-library/react';
import '@testing-library/jest-dom';
import Stack from '../Stack';

describe('Stack', () => {
  it('renders', () => {
    const stack = render(<Stack />);
    const button = stack.getByRole('button');
    expect(button).toBeInTheDocument();
  });
});

jest.setup.js:

import '@testing-library/jest-dom/extend-expect';

jest.config.js:

module.exports = {
  setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
  rootDir: '.',
  moduleNameMapper: {
    // Handle CSS imports (with CSS modules)
    // https://jestjs.io/docs/webpack#mocking-css-modules
    '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',

    // Handle CSS imports (without CSS modules)
    '^.+\\.(css|sass|scss)$': '<rootDir>/__mocks__/styleMock.js',

    // Handle image imports
    // https://jestjs.io/docs/webpack#handling-static-assets
    '^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i': `<rootDir>/__mocks__/fileMock.js`,

    // Handle module aliases
    '^~/(.*)$': '<rootDir>/$1',
  },
  collectCoverageFrom: ['**/*.{js,jsx,ts,tsx}', '!**/*.d.ts', '!**/node_modules/**'],
  testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/', '<rootDir>/functions'],
  testEnvironment: 'jsdom',
  verbose: false,
  resolver: 'jest-node-exports-resolver',
  transform: { '\\.m?[jt]sx?$': ['babel-jest', { presets: ['next/babel'] }] },
  transformIgnorePatterns: ['/node_modules/(?!(dequal)/)', '^.+\\.module\\.(css|sass|scss)$'],
};

What you did:

yarn add jest@latest ts-jest@latest -D yarn add @testing-library/dom -D yarn add jest-environment-jsdom -D

yarn test Stack

What happened:

Before I upgraded my tests were passing. Now they all fail

 FAIL  components/__tests__/Stack.unit.test.tsx
  ● Test suite failed to run

    Cannot find module '@jest/expect' from 'node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js'

      at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:491:11)

tettoffensive avatar May 17 '22 19:05 tettoffensive

Having same issue in non-react app

heinrich321 avatar Jul 25 '22 18:07 heinrich321

Same here in a react app

raunakdoesdev avatar Aug 03 '22 17:08 raunakdoesdev

I had the same issue.

Do you have a jest.config.js?

This is was fixed it for me.

I removed resolver: 'jest-node-exports-resolver' and changed it from:

module.exports = {
    preset: 'ts-jest', // use js-jest if you re not using typescript
    testEnvironment: 'node',
    resolver: 'jest-node-exports-resolver',
}

to:


module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'node',
    transformIgnorePatterns: ['^.+\\.js$'],
}

Can you try?

areindl avatar Aug 05 '22 14:08 areindl

@areindl I did the same, fixed the issue +1

heinrich321 avatar Aug 05 '22 14:08 heinrich321

You might not need this jest-node-exports-resolver as https://github.com/facebook/jest/releases/tag/v28.0.0 already support export in package.json

hunterdes avatar Feb 13 '23 04:02 hunterdes