Angular Monorepo Tutorial - 3: Task-Running

Common tasks include:

  • Building an application
  • Serving a local web server with the built project
  • Running your unit tests
  • Linting your code
  • Running e2e tests

When you ran your generators in Part 1, you already set up these common tasks for each project.

Defining Targets

Here's the project.json file for your common-ui project:

libs/common-ui/project.json
1{ 2 "name": "common-ui", 3 "$schema": "../../node_modules/nx/schemas/project-schema.json", 4 "projectType": "library", 5 "sourceRoot": "libs/common-ui/src", 6 "prefix": "myorg", 7 "targets": { 8 "test": { 9 "executor": "@nx/jest:jest", 10 "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], 11 "options": { 12 "jestConfig": "libs/common-ui/jest.config.ts", 13 "passWithNoTests": true 14 } 15 }, 16 "lint": { 17 "executor": "@nx/linter:eslint", 18 "options": { 19 "lintFilePatterns": [ 20 "libs/common-ui/**/*.ts", 21 "libs/common-ui/**/*.html" 22 ] 23 } 24 } 25 }, 26 "tags": [] 27} 28
Nx 15 and lower use @nrwl/ instead of @nx/

You can see that two targets are defined here: test and lint.

The properties inside each of these targets is defined as follows:

  • executor - which Nx executor to run. The syntax here is: <plugin name>:<executor name>
  • outputs - this is an array of files that would be created by running this target. (This informs Nx on what to save for it's caching mechanisms you'll learn about in 4 - Workspace Optimizations).
  • options - this is an object defining which executor options to use for the given target. Every Nx executor allows for options as a way to parameterize it's functionality.

Running Tasks

Syntax for Running Tasks in Nx

Run the test target for your common-ui project:

~/myorg

npx nx test common-ui

1 2> nx run common-ui:test 3 4 PASS common-ui libs/common-ui/src/lib/banner/banner.component.spec.ts 5 BannerComponent 6 ✓ should create (22 ms) 7 8Test Suites: 1 passed, 1 total 9Tests: 1 passed, 1 total 10Snapshots: 0 total 11Time: 2.192 s 12Ran all test suites. 13 14 ———————————————————————————————————————————————————————————————————————————————————————————————— 15 16 > NX Successfully ran target test for project common-ui (4s) 17 18

Next, run a lint check on your common-ui project:

~/myorg

npx nx lint common-ui

1 2> nx run common-ui:lint 3 4 5Linting "common-ui"... 6 7All files pass linting. 8 9 10 ———————————————————————————————————————————————————————————————————————————————————————————————— 11 12 > NX Successfully ran target lint for project common-ui (1s) 13 14

What's Next