Node Nx Tutorial - Step 7: Test Affected Projects

In addition to supporting computation caching, Nx scales your development by doing code change analysis to see what apps or libraries are affected by a particular pull request.

Commit all the changes in the repo:

git add .
git commit -am 'init'
git checkout -b testbranch

Open libs/auth/src/lib/auth.controller.ts and change the controller:

1import { Body, Controller, Get, Post } from '@nestjs/common';
2
3@Controller('auth')
4export class AuthController {
5  @Get()
6  auth() {
7    return {
8      authenticated: true,
9    };
10  }
11
12  @Post()
13  authenticate(@Body() postData: { username: string; password: string }) {
14    const { username, password } = postData;
15    // check the database
16    console.log(username, password);
17  }
18}

Run nx affected:apps, and you should see todos printed out. The affected:apps looks at what you have changed and uses the project graph to figure out which apps are affected by this change.

Run nx affected:libs, and you should see auth printed out. This command works similarly, but instead of printing the affected apps, it prints the affected libs.

Test Affected Projects

Printing the affected projects can be handy, but usually you want to do something with them. For instance, you may want to test everything that has been affected.

Run nx affected:test to retest only the projects affected by the change.

As you can see, because the code was updated without updating the tests, the unit tests failed.

>  NX  Running target test for projects:

  - auth
  - todos

...

  Failed projects:

  - todos

Note that Nx only tried to retest auth and todos. It didn't retest data because there is no way that library could be affected by the changes in this branch.

Affected:*

You can run any target against the affected projects in the graph like this:

# The following are equivalent
nx affected --target=build
nx affected:build

What's Next