nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.65k stars 2.36k forks source link

Jest tests show typescript errors from different libraries that not included to tests #16175

Closed pumano closed 1 year ago

pumano commented 1 year ago

Current Behavior

I have nx layout

Looks like jest trying to load some code from libraries as modules and create some typechecking, but I don't need it, I need run my test only, without somehow loading other modules that is not used directly via imports.

Expected Behavior

run my tests only, without loading other modules that is not used directly via imports.

GitHub Repo

https://github.com/pumano/nx-jest-typechecking-not-used-libraries

Steps to Reproduce

  1. clone repo
  2. install deps via npm i --force
  3. run nx test uikit

Nx Report

Node : 18.13.0
   OS   : darwin arm64
   npm  : 9.2.0

   nx                      : 15.9.2
   @nrwl/js                : 15.9.2
   @nrwl/jest              : 15.9.2
   @nrwl/linter            : 15.9.2
   @nrwl/workspace         : 15.9.2
   @nrwl/angular           : 15.9.2
   @nrwl/cli               : 15.9.2
   @nrwl/cypress           : 15.9.2
   @nrwl/devkit            : 15.9.2
   @nrwl/eslint-plugin-nx  : 15.9.2
   @nrwl/storybook         : 15.9.2
   @nrwl/tao               : 15.9.2
   @nrwl/web               : 15.9.2
   @nrwl/webpack           : 15.9.2
   typescript              : 4.9.5
   ---------------------------------------
   Community plugins:
   @storybook/angular : 6.5.16

Failure Logs

FAIL   uikit  libs/uikit/src/lib/bugton/bugton.component.spec.ts
  ● Test suite failed to run

    libs/lib-with-errors/src/lib/error/error.component.ts:11:5 - error TS2322: Type 'number' is not assignable to type 'string'.

    11     return 35;
           ~~~~~~~~~~

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.928 s
Ran all test suites.

Additional Information

No response

pumano commented 1 year ago

Looks like I found solution: By default ts-jest uses TypeScript compiler in the context of a project (yours), with full type-checking and features. But it can also be used to compile each file separately, what TypeScript calls an ‘isolated module’. That’s what the isolatedModules option (which defaults to false) does. Link to ts-jest

const nxPreset = require("@nrwl/jest/preset").default;

module.exports = { 
    ...nxPreset,
    globals: {
        'ts-jest': {
            isolatedModules: true
        }
    },
};

That really solve 2 of my problems:

But got problem:

barbados-clemens commented 1 year ago

Hi @pumano,

In your example, you say that importing helpers lib from UIKit doesn't use the lib with the ts error, but it does. because helpers imports the ts error lib then importing from helpers will therefore include the ts error lib. You can see this by viewing the nx project graph (nx graph). Nx project graph showing UIKit library depends on helpers which depends on ts error lib

As you mentioned, you can run with isolatedModules which turns off type checking, which is a great solution if you don't want/need to type check your tests.

another option is to use deep imports for tests so you don't include everything in your barrel (index.ts) file if you're only wanting to import 1 thing without transforming/checking the whole library public API. this is handy when your projects end up being very large.

I'm going to close this issue since it's working as intended.

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.