Setup `build` as a target dependency for `test` and use compiled assets in tests
See original GitHub issueDescription
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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:20 (15 by maintainers)
This PR has the functionality for the jest executor to determine if all of the deps have been built and if so create all of the
moduleNameMapper
values, creates a tmp jest.config.js and then uses that config file as the config for running the tests themselves, this will allow jest to use the compiled artifacts rather than compiling on the fly.https://github.com/yharaskrik/nx/pull/2/files
Agreed! (I am having deja vu with this conversation though, thought we already closed it haha)