Node Server Tutorial - Part 3: Task-Running

Common tasks include:

  • Building an application
  • Serving an application locally for development purposes
  • Running your unit tests
  • Linting your code

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 the auth project:

/auth/project.json
1{ 2 "name": "auth", 3 "$schema": "../node_modules/nx/schemas/project-schema.json", 4 "sourceRoot": "auth/src", 5 "projectType": "library", 6 "targets": { 7 "build": { 8 "executor": "@nx/js:tsc", 9 "outputs": ["{options.outputPath}"], 10 "options": { 11 "outputPath": "dist/./auth", 12 "tsConfig": "auth/tsconfig.lib.json", 13 "packageJson": "auth/package.json", 14 "main": "auth/src/index.ts", 15 "assets": ["auth/*.md"] 16 } 17 }, 18 "lint": { 19 "executor": "@nx/eslint:lint", 20 "outputs": ["{options.outputFile}"], 21 "options": { 22 "lintFilePatterns": ["auth/**/*.ts"] 23 } 24 }, 25 "test": { 26 "executor": "@nx/jest:jest", 27 "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], 28 "options": { 29 "jestConfig": "auth/jest.config.ts", 30 "passWithNoTests": true 31 }, 32 "configurations": { 33 "ci": { 34 "ci": true, 35 "codeCoverage": true 36 } 37 } 38 } 39 }, 40 "tags": [] 41} 42
Nx 15 and lower use @nrwl/ instead of @nx/

You can see that three targets are defined here: build, test and lint.

The properties of these targets are 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 - Task Pipelines).
  • 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 build target for your auth project:

~/products-api

npx nx build auth

1 2> nx run auth:build 3 4Compiling TypeScript files for project "auth"... 5Done compiling TypeScript files for project "auth". 6 7 ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 8 9 > NX Successfully ran target build for project auth (1s) 10

You can now find your built auth distributable in your dist/auth/ directory, as specified in the outputPath property of the build target options in your project.json file.

Next, run a lint check on auth:

~/products-api

npx nx lint auth

1 2> nx run auth:lint 3 4 5Linting "auth"... 6 7All files pass linting. 8 9 10 ——————————————————————————————————————————————————————————————————————————————————————————————— 11 12 > NX Successfully ran target lint for project products-api (777ms) 13

Run e2e Tests

To run the e2e tests, you first need to serve the root products-api project:

~/products-api

npx nx serve products-api

1> nx run products-api:serve 2 3Debugger listening on ws://localhost:9229/5ee3e454-1e38-4d9b-a5de-64a4cb1e21b9 4Debugger listening on ws://localhost:9229/5ee3e454-1e38-4d9b-a5de-64a4cb1e21b9 5For help, see: https://nodejs.org/en/docs/inspector 6[ ready ] http://localhost:3000 7[ watch ] build succeeded, watching for changes... 8

Then you can run the e2e tests from the e2e project in a separate terminal:

~/products-api

npx nx e2e e2e

1> nx run e2e:e2e 2 3Determining test suites to run... 4Setting up... 5 6 PASS e2e e2e/src/server/server.spec.ts 7 GET / 8 ✓ should return a message (39 ms) 9 POST /auth 10 ✓ should return a status and a name (19 ms) 11 12Test Suites: 1 passed, 1 total 13Tests: 2 passed, 2 total 14Snapshots: 0 total 15Time: 0.263 s, estimated 1 s 16Ran all test suites. 17 18Tearing down... 19 20 21 ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 22 23 > NX Successfully ran target e2e for project e2e (2s) 24

What's Next