Skip to content

Setup build as a target dependency for test and use compiled assets in tests #6253

@yharaskrik

Description

@yharaskrik

Description

With the new version of Nx we can now define build for dependencies as a target that needs to be run prior to the original build command can be run using the targetDependencies in nx.json. It would be awesome to be able to use the compiled assets when running jests tests, rather than using jest-preset-angular with the *.ts transformer on a per spec file.

Essentially we would be able to put this in our nx.json

"targetDependencies": {
        "build": [
            {
                "target": "build",
                "projects": "dependencies"
            }
        ],
        "test": [
           {
               "target": "build",
               "projects": "dependencies"
            }
        ]
    }

and then when a spec file imports from TS such as @scope/lib1 instead of pointing that alias to /libs/lib1/src/index.ts we would point it to /dist/libs/lib1, this is similar to how the Nrwl builder will work --with-deps

Motivation

Should greatly speed up test runs if TypeScript files do not need to be compiled on the fly and takes advantage of the buildable deps Nx already provides.

Suggested Implementation

@nrwl/jest would need to look at the nx.json in the same way that the builders do and determine if there are targetDependencies and then when the tests are run, the jest.config.js that is used will need to be modified such that we can override the TS paths. Not exactly how this should work since I know jest-preset-angular is used to do the transforming with ts-jest underneath, maybe we can pass in a temporary tsconfig file to those libraries so that we can merge the root tsconfig.base.json with the paths that were build.

ts-jest will look at the moduleNameMapper (https://kulshekhar.github.io/ts-jest/docs/getting-started/paths-mapping/) for path matching, jest supports passing in moduleNameMapper (althought the typings expect a string in @types/jest for runCLI so not sure how that would work) which should allow us to override where sources are mapped too (I am assuming that those would override paths in the tsconfig but I am not 100% sure on that)

Alternate Implementations

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions