|
| 1 | +# Rebuilding and Retesting What is Affected |
| 2 | + |
| 3 | +As with a regular CLI project, you can build and test apps and libs. |
| 4 | + |
| 5 | +```console |
| 6 | +ng g app myapp |
| 7 | +ng g app myapp2 --directory=mydirectory |
| 8 | +ng g lib mylib |
| 9 | +ng g lib mylib2 --directory=mydirectory |
| 10 | + |
| 11 | +ng build myapp |
| 12 | +ng build mydirectory-myapp2 |
| 13 | +ng build mylib # work if the lib is marked as publishable |
| 14 | +ng build mydirectory-mylib2 # work if the lib is marked as publishable |
| 15 | + |
| 16 | +ng test myapp # runs unit tests for myapp |
| 17 | +ng test mylib # runs unit tests for mylib |
| 18 | +ng e2e myapp-e2e # runs e2e tests for myapp |
| 19 | +``` |
| 20 | + |
| 21 | +Now imagine, `myapp` depends on `mylib`. If we make a change to `mylib`, |
| 22 | +we need to make sure nothing in the workspace is affected. |
| 23 | +Typically, you would do it like this: |
| 24 | + |
| 25 | +```console |
| 26 | +ng build mylib |
| 27 | +ng test mylib |
| 28 | +ng build myapp |
| 29 | +ng test myapp |
| 30 | +``` |
| 31 | + |
| 32 | +In many organizations, you would have dozens or hundreds of apps and libs. |
| 33 | +To be productive in a monorepo, you need to be able to check that your |
| 34 | +change is safe, and rebuilding and retesting everything on every change |
| 35 | +won't scale, tracing the dependencies manually (as shown above) wont's |
| 36 | +scale either. Nx uses code analysis to determine what needs to be rebuild |
| 37 | +and retested, and it provides the following three commands you can use: |
| 38 | +`affected:build`, `affected:test`, and `affected:e2e`. |
| 39 | + |
| 40 | +- Rerunning build for all the projects affected by a PR |
| 41 | + |
| 42 | +```console |
| 43 | +yarn affected:build --base=master --head=HEAD |
| 44 | +``` |
| 45 | + |
| 46 | +- Rerunning unit tests for all the projects affected by a PR |
| 47 | + |
| 48 | +```console |
| 49 | +yarn affected:test --base=master --head=HEAD |
| 50 | +``` |
| 51 | + |
| 52 | +- Rerunning e2e tests for all the projects affected by a PR |
| 53 | + |
| 54 | +```console |
| 55 | +yarn affected:e2e --base=master --head=HEAD |
| 56 | +``` |
| 57 | + |
| 58 | +When executing these commands, Nx will topologically sort the projects, |
| 59 | +and will run what it can in parallel. But we can also explicitly pass |
| 60 | +`--parallel` like so: |
| 61 | + |
| 62 | +```console |
| 63 | +yarn affected:build --base=master --parallel |
| 64 | +yarn affected:test --base=master --parallel |
| 65 | +yarn affected:e2e --base=master --parallel |
| 66 | +``` |
| 67 | + |
| 68 | +We can also pass `--maxParallel` to specify the number of parallel processes. |
| 69 | + |
| 70 | +## affected:build |
| 71 | + |
| 72 | +Run `npm run affected:build -- --help` or `yarn affected:build --help` to see |
| 73 | +the available options: |
| 74 | + |
| 75 | +```console |
| 76 | +Build applications affected by changes |
| 77 | + |
| 78 | +Run command using --base=[SHA1] (affected by the committed, uncommitted and |
| 79 | +untracked changes): |
| 80 | + --base Base of the current branch (usually master) [string] |
| 81 | + |
| 82 | +or using --base=[SHA1] --head=[SHA2] (affected by the committed changes): |
| 83 | + --base Base of the current branch (usually master) [string] |
| 84 | + --head Latest commit of the current branch (usually HEAD) [string] |
| 85 | + |
| 86 | +or using: |
| 87 | + --files A list of files delimited by commas [array] |
| 88 | + --uncommitted Uncommitted changes |
| 89 | + --untracked Untracked changes |
| 90 | + |
| 91 | +Options: |
| 92 | + --help Show help [boolean] |
| 93 | + --version Show version number [boolean] |
| 94 | + --parallel Parallelize the command [boolean] [default: false] |
| 95 | + --maxParallel Max number of parallel processes [number] [default: 3] |
| 96 | + --all All projects |
| 97 | + --exclude Exclude certain projects from being processed |
| 98 | + [array] [default: []] |
| 99 | + --only-failed Isolate projects which previously failed |
| 100 | + [boolean] [default: false] |
| 101 | +``` |
| 102 | + |
| 103 | +- `npm run affected:build -- --base=[SHA1] --base=[SHA2]` or `yarn affected:build --base=[SHA1] --base=[SHA2]`. Nx will calculate what changed between the two SHAs, and will build the apps affected by the change. For instance, `yarn affected:build --base=origin/master --base=HEAD` will rebuild what is affected by a PR. |
| 104 | +- `npm run affected:build -- --files=libs/mylib/src/index.ts,libs/mylib2/src/index.ts` or `yarn affected:build --files=libs/mylib/src/index.ts,libs/mylib2/src/index.ts`. Nx will build what is affected by changing the two index files. |
| 105 | +- `npm run affected:build -- --uncommitted` or `yarn affected:build --uncommitted`. Nx will build what is affected by the uncommitted files (this is useful during development). |
| 106 | +- `npm run affected:build -- --untracked` or `yarn affected:build --untracked`. Nx will build what is affected by the untracked files (this is useful during development). |
| 107 | + |
| 108 | +All other options will be passed into the underlying build command (e.g., `yarn affected:build --base=origin/master --base=HEAD --prod`). |
| 109 | + |
| 110 | +## affected:test |
| 111 | + |
| 112 | +Run `npm run affected:test -- --help` or `yarn affected:test --help` to see the available options: |
| 113 | + |
| 114 | +```console |
| 115 | +Test applications affected by the change |
| 116 | + |
| 117 | +Run command using --base=[SHA1] (affected by the committed, uncommitted and |
| 118 | +untracked changes): |
| 119 | + --base Base of the current branch (usually master) [string] |
| 120 | + |
| 121 | +or using --base=[SHA1] --head=[SHA2] (affected by the committed changes): |
| 122 | + --base Base of the current branch (usually master) [string] |
| 123 | + --head Latest commit of the current branch (usually HEAD) [string] |
| 124 | + |
| 125 | +or using: |
| 126 | + --files A list of files delimited by commas [array] |
| 127 | + --uncommitted Uncommitted changes |
| 128 | + --untracked Untracked changes |
| 129 | + |
| 130 | +Options: |
| 131 | + --help Show help [boolean] |
| 132 | + --version Show version number [boolean] |
| 133 | + --parallel Parallelize the command [boolean] [default: false] |
| 134 | + --maxParallel Max number of parallel processes [number] [default: 3] |
| 135 | + --all All projects |
| 136 | + --exclude Exclude certain projects from being processed |
| 137 | + [array] [default: []] |
| 138 | + --only-failed Isolate projects which previously failed |
| 139 | + [boolean] [default: false] |
| 140 | +``` |
| 141 | + |
| 142 | +- `npm run affected:test -- --base=[SHA1] --base=[SHA2]` or `yarn affected:test --base=[SHA1] --base=[SHA2]`. Nx will calculate what changed between the two SHAs, and will test the projects affected by the change. For instance, `yarn affected:test --base=origin/master --base=HEAD` will retest what is affected by a PR. |
| 143 | +- `npm run affected:test -- --files=libs/mylib/src/index.ts,libs/mylib2/src/index.ts` or `yarn affected:test --files=libs/mylib/src/index.ts,libs/mylib2/src/index.ts`. Nx will test what is affected by changing the two index files. |
| 144 | +- `npm run affected:test -- --uncommitted` or `yarn affected:test --uncommitted`. Nx will test what is affected by the uncommitted files (this is useful during development). |
| 145 | +- `npm run affected:test -- --untracked` or `yarn affected:test --untracked`. Nx will test what is affected by the untracked files (this is useful during development). |
| 146 | + |
| 147 | +All other options will be passed into the underlying test command (e.g., `yarn affected:test --base=origin/master --base=HEAD --sm=false`). |
| 148 | + |
| 149 | +## affected:e2e |
| 150 | + |
| 151 | +Run `npm run affected:e2e -- --help` or `yarn affected:e2e --help` to see the available options: |
| 152 | + |
| 153 | +```console |
| 154 | +Run e2e tests for the applications affected by changes |
| 155 | + |
| 156 | +Run command using --base=[SHA1] (affected by the committed, uncommitted and |
| 157 | +untracked changes): |
| 158 | + --base Base of the current branch (usually master) [string] |
| 159 | + |
| 160 | +or using --base=[SHA1] --head=[SHA2] (affected by the committed changes): |
| 161 | + --base Base of the current branch (usually master) [string] |
| 162 | + --head Latest commit of the current branch (usually HEAD) [string] |
| 163 | + |
| 164 | +or using: |
| 165 | + --files A list of files delimited by commas [array] |
| 166 | + --uncommitted Uncommitted changes |
| 167 | + --untracked Untracked changes |
| 168 | + |
| 169 | +Options: |
| 170 | + --help Show help [boolean] |
| 171 | + --version Show version number [boolean] |
| 172 | + --all All projects |
| 173 | + --exclude Exclude certain projects from being processed |
| 174 | + [array] [default: []] |
| 175 | + --only-failed Isolate projects which previously failed |
| 176 | + [boolean] [default: false] |
| 177 | +``` |
| 178 | + |
| 179 | +- `npm run affected:e2e -- --base=[SHA1] --base=[SHA2]` or `yarn affected:e2e --base=[SHA1] --base=[SHA2]`. Nx will calculate what changed between the two SHAs, and will run e2e test for the apps affected by the change. For instance, `yarn affected:test --base=origin/master --base=HEAD` will retest what is affected by a PR. |
| 180 | +- `npm run affected:e2e -- --files=libs/mylib/src/index.ts,libs/mylib2/src/index.ts` or `yarn affected:e2e --files=libs/mylib/src/index.ts,libs/mylib2/src/index.ts`. Nx will run e2e tests for what is affected by changing the two index files. |
| 181 | +- `npm run affected:e2e -- --uncommitted` or `yarn affected:e2e --uncommitted`. Nx will run e2e tests for what is affected by the uncommitted files (this is useful during development). |
| 182 | +- `npm run affected:e2e -- --untracked` or `yarn affected:e2e --untracked`. Nx will run e2e tests for what is affected by the untracked files (this is useful during development). |
| 183 | + |
| 184 | +All other options will be passed into the underlying test command (e.g., `yarn affected:test --base=origin/master --base=HEAD --sm=false`). |
0 commit comments