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.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:20 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
yharaskrikcommented, Jul 7, 2021

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

1reaction
yharaskrikcommented, May 18, 2022

closing as found in the PR that this doesn’t provide as much benefit as expected, unfortunately.

Agreed! (I am having deja vu with this conversation though, thought we already closed it haha)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring the build settings of a target - Apple Developer
Specify the options you use to compile, link, and produce a product from a target, and identify settings inherited from your project or...
Read more >
Set up project for AndroidX Test - Android Developers
Step 1: Open the build.gradle file for your Gradle module. Step 2: In the repositories section, make sure Google's Maven repository appears:.
Read more >
Swift package with resources in iOS project won't compile test ...
I've been trying to convert a set of app and library projects to use swift packages for dependencies (previously used Carthage), and I've...
Read more >
dotnet test command - .NET CLI | Microsoft Learn
For multi-targeted projects, tests are run for each targeted framework. The test host and the unit test framework are packaged as NuGet packages ......
Read more >
Xcode Build Settings
A convenient reference of available build settings for Xcode ... for testing purposes when it isn't practical to use an asset pack server....
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