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.

How to use with Jest?

See original GitHub issue

I’m trying to use your package with Jest to test React project but I can’t get it to work, below is my Jest configuration.

// jest.config.js
module.exports = {
  // rest of config...
  setupFilesAfterEnv: ['./tests/setup.ts']
};
// tests/setup.ts
import appRootPath from 'app-root-path';
import tsConfig from '../tsconfig.json';
import tsConfigPaths from 'tsconfig-paths';

let cleanup: () => void;

beforeAll(() => {
  cleanup = tsConfigPaths.register({
    baseUrl: appRootPath.path,
    paths: tsConfig.compilerOptions.paths
  });
});

afterAll(() => {
  cleanup();
});

it keeps throwing errors like “Cannot find module ‘@views/components/error-boundary’ from ‘App.tsx’”

any ideas on how to use it with Jest, keep in mind that Jest doesn’t support the --require flag.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

42reactions
geerschcommented, Oct 16, 2019

@AhmedMKamal, you don’t need to use that package.

If you use the baseUrland paths options in your tsconfig file, you must make sure to also configure the moduleNameMapper option in your Jest config.

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@app/*": ["src/*"]
    }
  }
}

jest.config.json

{ 
  "moduleNameMapper": {
    "^@app/(.*)$": "<rootDir>/$1"
  }
}

You’ll need to map the configuration to the Jest config format, which uses regular expressions. ts-jest provides a helper to transform the mapping from tsconfig to Jest config format, but it you use it you need the .js version of the config file.

jest.config.js

const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig');

module.exports = {
  //...
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths , { prefix: '<rootDir>/' } )
};

The module that you use basically does the same thing as the pathsToModuleNameMapper helper offered by ts-jest. But if you can use ts-jest’s helper, then I’d remove the extra dependency and use it instead.

More information can be found here:

https://kulshekhar.github.io/ts-jest/user/config/

1reaction
aelborecommented, Mar 6, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started - Jest
Install Jest using your favorite package manager: npm; Yarn. npm install --save-dev jest ... To use Babel, install required dependencies:.
Read more >
Jest Tutorial for Beginners: Getting Started With JavaScript ...
Jest is a JavaScript test runner, that is, a JavaScript library for creating, running, and structuring tests. Jest ships as an NPM package,...
Read more >
Jest Tutorial - JavaScript Unit Testing Using Jest Framework
In this Jest tutorial, we will learn about various Jest features, Jest matchers and how to use Jest framework for JavaScript Unit Testing....
Read more >
Testing TypeScript apps using Jest - LogRocket Blog
Jest is a simple, lightweight testing framework that provides a variety of testing capabilities to JavaScript and TypeScript projects. It ...
Read more >
A Practical Guide To Testing React Applications With Jest
One thing we can do is to test components props — specifically, testing whether props from one component are being passed to another....
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