question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't find libraries when running Jest tests

See original GitHub issue

Current Behavior

After updating to the latest via migrate I am unable to run Jest unit tests as it doesn’t seem to be able to resolve my nx libraries. Similar to this issue: https://stackoverflow.com/questions/72606965/testing-with-nx-jest-and-angular-cant-find-library-when-running-tests

Expected Behavior

Should be able to run Jest tests when using nx libraries.

Failure Logs

Cannot find module ‘@org/shared:ui’ from ‘src/header/header.tsx’

Environment

    "next": "^12.1.5",
    "@nrwl/cypress": "14.3.1",
    "@nrwl/eslint-plugin-nx": "14.3.1",
    "@nrwl/jest": "14.3.1",
    "@nrwl/js": "14.3.1",
    "@nrwl/linter": "14.3.1",
    "@nrwl/next": "14.3.1",
    "@nrwl/node": "14.3.1",
    "@nrwl/react": "14.3.1",
    "@nrwl/storybook": "14.3.1",
    "@nrwl/tao": "14.3.1",
    "@nrwl/web": "14.3.1",
    "@nrwl/workspace": "14.3.1",
    "jest": "^28.1.1",
    "jest-environment-jsdom": "^28.1.1",

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:4
  • Comments:24 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
KamalAmancommented, Aug 4, 2022

Experiencing the same issue after updating.

I resolved it by adding a moduleNameMapper fixed at the cwd

jest.preset.js

const nxPreset = require('@nrwl/jest/preset');
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig.base.json');

module.exports = {
  ...nxPreset,
  testEnvironment: 'jsdom',
  transform: {
    '\\.(gql|graphql)$': 'jest-transform-graphql',
    '.*': 'babel-jest',
  },
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
    prefix: process.cwd()
  })
};

It seems like moduleNameMapper should be a part of the @nrwl/jest/preset in the first place. When troubleshooting it seems like imports are being done relative to each libs folder path

5reactions
ivan114commented, Jul 1, 2022

I modified the preset from @KamalAman a little bit as the config crashing when there is JSON file in the project, and this config works for my nodejs test

const nxPreset = require('@nrwl/jest/preset');
const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig.base.json');

module.exports = {
  ...nxPreset,
  testEnvironment: 'node',
  transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest',
  },
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
    prefix: process.cwd(),
  }),
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing with Nx, Jest and Angular - Can't find library when ...
The import { EnvironmentService } from '@myorg/client-core'; is inside the paitent.guard.ts file. It can find the libraries when I run ng serve ...
Read more >
Troubleshooting - Jest
Troubleshooting. Uh oh, something went wrong? Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why​.
Read more >
Configuring Jest
By default, Jest runs all tests and produces all errors into the console upon ... See the coverageProvider option for more details.
Read more >
Testing React Apps - Jest
Run. npm; Yarn. npm install --save-dev react-test-renderer ... This way you won't see the props passed to the mock component in the snapshot ......
Read more >
Snapshot Testing - Jest
Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found