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.

Jest Paths to Modules failing when running tests

See original GitHub issue

I have an NX workspace up and running, haven’t had any trouble with it in some time. However recently I created a new app, and have some paths configured in it in ts.config.app.json (I have no choice but to use these paths):

paths: { 
       "core/*": ["apps/myapp/myapp-ui/src/app/core/*"],
      "shared/*": ["apps/,myapp/myapp-ui/src/app/shared/*"],
      "templates/*": ["apps/myapp/myapp-ui/src/app/templates/*"],
}

or 

paths: { 
       "^core/(.*)": ["apps/myapp/myapp-ui/src/app/core/1$"],
      ...
}

etc. (and many other options tried).

It works fines, runs as expected, etc, until I try to test it (using Jest). Then it’s all “modules not found”.

I have tried configuring modulePaths, moduleNameMapper, etc. etc. in the jest.config that lives in the root of the app, which looks like this:

workspace – apps ----someapps ------myapp ------jest.config.js

Nothing works, the modules are never found. I originally suspected that the rootDir is not doing what I’d expect it to be doing, but by “breaking” the jest resolver, it traces out the rootDir in the console (it actually tells you, rootDir = etc/etc), so I know it’s correct.

Any wisdom on this one would be greatly appreciated. I’m at a total brick wall here and I need to get these tests running. If I put the app in a vanilla angular 8 project, everything works fine.

Thanks in advance. I have looked elsewhere in the issues here, but there doesn’t seem, to be any basic guidance insofar as, “this is how it resolves the rootDir, which means this is how you need to configure it.”

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
Cammisulicommented, Jul 30, 2020

Hey folks, we have a special resolver that we use for Jest to find apps and libs in your workspace. Can you please make sure that the root jest.config.js has something like the following?

module.exports = {
  testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
  transform: {
    '^.+\\.(ts|js|html)$': 'ts-jest'
  },
  resolver: '@nrwl/jest/plugins/resolver',
  moduleFileExtensions: ['ts', 'js', 'html'],
  coverageReporters: ['html']
};

The most important piece being the resolver property.

1reaction
anthonyma94commented, Mar 23, 2022

Awesome, thank you for sharing.

I did some research after my comment yesterday, turns out Jest doesn’t use ts-node or tsc when using globalSetup, which is why the paths aren’t loading. I was able to get it working by progamatically starting the services in my globalSetup file:

/** These steps are needed as globalSetup doesn't start tsc. */
// Manually load ts-node and tsconfig
require("ts-node").register({
    transpileOnly: true,
});
// Manually load envs
require("dotenv").config({ path: "./apps/api/.env" });
process.env.DB_NAME = "test_db";
process.env.MONGO_URL = "";

// Manually map paths
const { compilerOptions } = require("../../../../tsconfig.base.json");
const tsConfigPaths = require("tsconfig-paths");

tsConfigPaths.register({
    baseUrl: "./",
    paths: compilerOptions.paths,
});

Hopefully this helps!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript paths not resolving when running jest?
I wanted to resolve modules paths starting with ~/ to my <baseUrl>/<moduleName> . Thank to OJ Kwon link I solved it with (give...
Read more >
Jest CLI Options
If Jest doesn't exit at the end of a test run, it means external resources are still being held on to or timers...
Read more >
Configuring package.json · Jest
The bail config option can be used here to have Jest stop running tests after the first failure. cacheDirectory [string] #. Default: "/tmp/<path>"....
Read more >
Configuring Module Resolution On Typescript and Jest
If you tried running the tests any time until now, it probably failed to run ... To be able to use aliases for...
Read more >
jest(1) — jest — Debian unstable - Debian Manpages
Path to a module exporting a filtering function. This method receives a list of tests which can be manipulated to exclude tests from...
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