Skip to content

Commit c88a852

Browse files
AndrewFinlaycoreyfarrell
authored andcommitted
docs: nyc instrument and --exclude-node-modules (#1039)
1 parent c213469 commit c88a852

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

README.md

+47-5
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,31 @@ Install custom reporters as a development dependency and you can use the `--repo
195195
nyc report --reporter=<custom-reporter-name>
196196
```
197197

198+
## Producing instrumented source
199+
200+
The `nyc instrument` command can produce a set of instrumented source files.
201+
These files are suitable for client side deployment in end to end testing.
202+
You can create an instrumented version of your source code by running:
203+
204+
```bash
205+
nyc instrument <input> [output]
206+
```
207+
208+
`<input>` can be any file or directory within the project root directory.
209+
The `[output]` directory is optional and can be located anywhere, if it is not set the instrumented code will be sent to `stdout`.
210+
For example, `nyc instrument . ./output` will produce instrumented versions of any source files it finds in `.` and store them in `./output`.
211+
212+
Any existing output can be removed by specifying the `--delete` option.
213+
Run `nyc instrument --help` to display a full list of available command options.
214+
215+
**Note:** `nyc instrument` will not copy the contents of a `.git` folder to the output directory.
216+
198217
## Setting the project root directory
199218

200219
nyc runs a lot of file system operations relative to the project root directory.
201220
During startup nyc will look for the *default* project root directory.
202221
The *default* project root directory is the first directory found that contains a `package.json` file when searching from the current working directory up.
203-
If nyc fails to find a directory containing a `package.json` file, it will use current working directory as the *default* project root directory.
222+
If nyc fails to find a directory containing a `package.json` file, it will use the current working directory as the *default* project root directory.
204223
You can change the project root directory with the `--cwd` option.
205224

206225
nyc uses the project root directory when:
@@ -220,7 +239,7 @@ It does this by watching for files that are `require()`'d during the test.
220239
When a file is `require()`'d, nyc creates and returns an instrumented version of the source, rather than the original.
221240
Only source files that are visited during a test will appear in the coverage report and contribute to coverage statistics.
222241

223-
nyc will instrument all files if the `--all` flag is set.
242+
nyc will instrument all files if the `--all` flag is set or if running `nyc instrument`.
224243
In this case all files will appear in the coverage report and contribute to coverage statistics.
225244

226245
nyc will only collect coverage for files that are located under `cwd`, and then only `*.js` files or files with extensions listed in the `extension` array.
@@ -263,9 +282,6 @@ The `exclude` option has the following defaults settings:
263282
These settings exclude `test` and `__tests__` directories as well as `test.js`, `*.test.js`, and `test-*.js` files.
264283
Specifying your own exclude property completely replaces these defaults.
265284

266-
**Note:** The exclude list always implicitly contains `**/node_modules/**`, even if not explicitly specified in an overriding `exclude` array.
267-
To reverse this you must add the negated exclude rule `!**/node_modules/`.
268-
269285
For example, the following config will collect coverage for every file in the `src` directory regardless of whether it is `require()`'d in a test.
270286
It will also exclude any files with the extension `.spec.js`.
271287

@@ -286,6 +302,32 @@ It will also exclude any files with the extension `.spec.js`.
286302
**Note:** Be wary of automatic OS glob expansion when specifying include/exclude globs with the CLI.
287303
To prevent this, wrap each glob in single quotes.
288304

305+
### Including files within `node_modules`
306+
307+
We always add `**/node_modules/**` to the exclude list, even if not specified in the config.
308+
You can override this by setting `--exclude-node-modules=false`.
309+
310+
For example, in the following config, `"excludeNodeModules: false"` will prevent `node_modules` from being added to the exclude rules.
311+
The set of include rules then restrict nyc to only consider instrumenting files found under the `lib/` and `node_modules/@my-org/` directories.
312+
The exclude rules then prevent nyc instrumenting anything in a `test` folder and the file `node_modules/@my-org/something/unwanted.js`.
313+
314+
```json
315+
{
316+
"nyc": {
317+
"all": true,
318+
"include": [
319+
"lib/**",
320+
"node_modules/@my-org/**"
321+
],
322+
"exclude": [
323+
"node_modules/@my-org/something/unwanted.js",
324+
"**/test/**"
325+
],
326+
"excludeNodeModules": false
327+
}
328+
}
329+
```
330+
289331
## Require additional modules
290332

291333
The `--require` flag can be provided to `nyc` to indicate that additional

0 commit comments

Comments
 (0)