UNPKG

4.04 kBMarkdownView Raw
1# test-exclude
2
3The file include/exclude logic used by [nyc] and [babel-plugin-istanbul].
4
5[![Build Status](https://travis-ci.org/istanbuljs/test-exclude.svg)](https://travis-ci.org/istanbuljs/test-exclude)
6[![Coverage Status](https://coveralls.io/repos/github/istanbuljs/test-exclude/badge.svg?branch=master)](https://coveralls.io/github/istanbuljs/test-exclude?branch=master)
7[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
8[![Greenkeeper badge](https://badges.greenkeeper.io/istanbuljs/test-exclude.svg)](https://greenkeeper.io/)
9
10## Usage
11
12```js
13const TestExclude = require('test-exclude');
14const exclude = new TestExclude();
15if (exclude().shouldInstrument('./foo.js')) {
16 // let's instrument this file for test coverage!
17}
18```
19
20### TestExclude(options)
21
22The test-exclude constructor accepts an options object. The defaults are taken from
23[@istanbuljs/schema].
24
25#### options.cwd
26
27This is the base directory by which all comparisons are performed. Files outside `cwd`
28are not included.
29
30Default: `process.cwd()`
31
32#### options.exclude
33
34Array of path globs to be ignored. Note this list does not include `node_modules` which
35is added separately. See [@istanbuljs/schema/default-excludes.js] for default list.
36
37#### options.excludeNodeModules
38
39By default `node_modules` is excluded. Setting this option `true` allows `node_modules`
40to be included.
41
42#### options.include
43
44Array of path globs that can be included. By default this is unrestricted giving a result
45similar to `['**']` but more optimized.
46
47#### options.extension
48
49Array of extensions that can be included. This ensures that nyc only attempts to process
50files which it might understand. Note use of some formats may require adding parser
51plugins to your nyc or babel configuration.
52
53Default: `['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx']`
54
55### TestExclude#shouldInstrument(filename): boolean
56
57Test if `filename` matches the rules of this test-exclude instance.
58
59```js
60const exclude = new TestExclude();
61exclude.shouldInstrument('index.js'); // true
62exclude.shouldInstrument('test.js'); // false
63exclude.shouldInstrument('README.md'); // false
64exclude.shouldInstrument('node_modules/test-exclude/index.js'); // false
65```
66
67In this example code:
68* `index.js` is true because it matches the default `options.extension` list
69 and is not part of the default `options.exclude` list.
70* `test.js` is excluded because it matches the default `options.exclude` list.
71* `README.md` is not matched by the default `options.extension`
72* `node_modules/test-exclude/index.js` is excluded because `options.excludeNodeModules`
73 is true by default.
74
75### TestExculde#globSync(cwd = options.cwd): Array[string]
76
77This synchronously retrieves a list of files within `cwd` which should be instrumented.
78Note that setting `cwd` to a parent of `options.cwd` is ineffective, this argument can
79only be used to further restrict the result.
80
81### TestExclude#glob(cwd = options.cwd): Promise<Array[string]>
82
83This function does the same as `TestExclude#globSync` but does so asynchronously. The
84Promise resolves to an Array of strings.
85
86
87## `test-exclude` for enterprise
88
89Available as part of the Tidelift Subscription.
90
91The maintainers of `test-exclude` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-test-exclude?utm_source=npm-test-exclude&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
92
93[nyc]: https://github.com/istanbuljs/nyc
94[babel-plugin-istanbul]: https://github.com/istanbuljs/babel-plugin-istanbul
95[@istanbuljs/schema]: https://github.com/istanbuljs/schema
96[@istanbuljs/schema/default-excludes.js]: https://github.com/istanbuljs/schema/blob/master/default-exclude.js