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 to v28 complains "Cannot find module '@jest/expect'" #457

Open
tettoffensive opened this issue May 17, 2022 · 5 comments
Open

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

tettoffensive opened this issue May 17, 2022 · 5 comments

Comments

@tettoffensive
Copy link

  • @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)
@eps1lon eps1lon transferred this issue from testing-library/react-testing-library May 17, 2022
@heinrich321
Copy link

Having same issue in non-react app

@raunakdoesdev
Copy link

Same here in a react app

@areindl
Copy link

areindl commented Aug 5, 2022

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?

@heinrich321
Copy link

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

@hunterdes
Copy link

hunterdes commented Feb 13, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants