| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a77f9f7 commit 4de7e63
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,6 +47,9 @@ const kIgnoreRegex = /\/\* node:coverage ignore next (?<count>\d+ )?\*\//; | |||
| 47 | 47 | const kLineEndingRegex = /\r?\n$/u; | |
| 48 | 48 | const kLineSplitRegex = /(?<=\r?\n)/u; | |
| 49 | 49 | const kStatusRegex = /\/\* node:coverage (?<status>enable|disable) \*\//; | |
| 50 | + // Match dotfiles (e.g. `test/.foo.js`) when applying coverage globs so the | ||
| 51 | + // default exclude patterns cover them. | ||
| 52 | + const kMatchGlobPatternOptions = { __proto__: null, dot: true }; | ||
| 50 | 53 | const kTypeOnlyImportRegex = /^\s*import\s+type\b/u; | |
| 51 | 54 | const kTypeScriptSourceRegex = /\.(?:cts|mts|ts)$/u; | |
| 52 | 55 | const kSourceFileGlob = '**/*.{cjs,cts,js,mjs,mts,ts}'; | |
@@ -63,6 +66,14 @@ function getStripTypeScriptTypesForCoverage() { | |||
| 63 | 66 | return stripTypeScriptTypesForCoverage; | |
| 64 | 67 | } | |
| 65 | 68 | ||
| 69 | + function createCoverageMatcher(pattern) { | ||
| 70 | + return { | ||
| 71 | + __proto__: null, | ||
| 72 | + relative: createMatcher(pattern, kMatchGlobPatternOptions), | ||
| 73 | + absolute: createMatcher(pattern), | ||
| 74 | + }; | ||
| 75 | + } | ||
| 76 | + | ||
| 66 | 77 | class CoverageLine { | |
| 67 | 78 | constructor(line, startOffset, src, length = src?.length) { | |
| 68 | 79 | const newlineLength = src == null ? 0 : | |
@@ -605,23 +616,28 @@ class TestCoverage { | |||
| 605 | 616 | // TestCoverage instance, so compile each glob to a matcher once and reuse | |
| 606 | 617 | // it for every file. Building a fresh Minimatch per call (the previous | |
| 607 | 618 | // behavior) dominated the coverage report time, scaling with | |
| 608 | - // files * globs. | ||
| 619 | + // files * globs. Each glob compiles to a matcher pair: `relative` enables | ||
| 620 | + // dot:true so globs match dotfiles within the project, while `absolute` | ||
| 621 | + // keeps the default behavior to avoid misinterpreting dot segments in the | ||
| 622 | + // absolute filesystem path (e.g. tmp dirs like `test/.tmp.0`). | ||
| 609 | 623 | this.#excludeMatchers ??= ArrayPrototypeMap( | |
| 610 | - this.options.coverageExcludeGlobs ?? [], (pattern) => createMatcher(pattern)); | ||
| 624 | + this.options.coverageExcludeGlobs ?? [], createCoverageMatcher); | ||
| 611 | 625 | this.#includeMatchers ??= ArrayPrototypeMap( | |
| 612 | - this.options.coverageIncludeGlobs ?? [], (pattern) => createMatcher(pattern)); | ||
| 626 | + this.options.coverageIncludeGlobs ?? [], createCoverageMatcher); | ||
| 613 | 627 | ||
| 614 | 628 | // This check filters out files that match the exclude globs. | |
| 615 | 629 | for (let i = 0; i < this.#excludeMatchers.length; ++i) { | |
| 616 | 630 | const matcher = this.#excludeMatchers[i]; | |
| 617 | - if (matcher.match(relativePath) || matcher.match(absolutePath)) return true; | ||
| 631 | + if (matcher.relative.match(relativePath) || | ||
| 632 | + matcher.absolute.match(absolutePath)) return true; | ||
| 618 | 633 | } | |
| 619 | 634 | ||
| 620 | 635 | // This check filters out files that do not match the include globs. | |
| 621 | 636 | if (this.#includeMatchers.length > 0) { | |
| 622 | 637 | for (let i = 0; i < this.#includeMatchers.length; ++i) { | |
| 623 | 638 | const matcher = this.#includeMatchers[i]; | |
| 624 | - if (matcher.match(relativePath) || matcher.match(absolutePath)) return false; | ||
| 639 | + if (matcher.relative.match(relativePath) || | ||
| 640 | + matcher.absolute.match(absolutePath)) return false; | ||
| 625 | 641 | } | |
| 626 | 642 | return true; | |
| 627 | 643 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,7 @@ | |||
| 1 | + const test = require('node:test'); | ||
| 2 | + const assert = require('node:assert'); | ||
| 3 | + const { foo } = require('../logic-file.js'); | ||
| 4 | + | ||
| 5 | + test('foo returns 1 from a dotfile test', () => { | ||
| 6 | + assert.strictEqual(foo(), 1); | ||
| 7 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,16 @@ async function setupFixtures() { | |||
| 16 | 16 | await cp(fixtureDir, tmpdir.path, { recursive: true }); | |
| 17 | 17 | } | |
| 18 | 18 | ||
| 19 | + function assertDefaultExclusions(stdout) { | ||
| 20 | + assert.match(stdout, /# start of coverage report/); | ||
| 21 | + assert.doesNotMatch(stdout, /# file-test\.js\s+\|/); | ||
| 22 | + assert.doesNotMatch(stdout, /# file\.test\.mjs\s+\|/); | ||
| 23 | + assert.doesNotMatch(stdout, /# file\.test\.ts\s+\|/); | ||
| 24 | + assert.doesNotMatch(stdout, /# test\.cjs\s+\|/); | ||
| 25 | + assert.doesNotMatch(stdout, /#\s+not-matching-test-name\.js\s+\|/); | ||
| 26 | + assert.match(stdout, /# end of coverage report/); | ||
| 27 | + } | ||
| 28 | + | ||
| 19 | 29 | describe('test runner coverage default exclusion', skipIfNoInspector, () => { | |
| 20 | 30 | before(async () => { | |
| 21 | 31 | await setupFixtures(); | |
@@ -58,18 +68,6 @@ describe('test runner coverage default exclusion', skipIfNoInspector, () => { | |||
| 58 | 68 | }); | |
| 59 | 69 | ||
| 60 | 70 | it('should exclude test files from coverage by default', async () => { | |
| 61 | - const report = [ | ||
| 62 | - '# start of coverage report', | ||
| 63 | - '# --------------------------------------------------------------', | ||
| 64 | - '# file | line % | branch % | funcs % | uncovered lines', | ||
| 65 | - '# --------------------------------------------------------------', | ||
| 66 | - '# logic-file.js | 66.67 | 100.00 | 50.00 | 5-7', | ||
| 67 | - '# --------------------------------------------------------------', | ||
| 68 | - '# all files | 66.67 | 100.00 | 50.00 | ', | ||
| 69 | - '# --------------------------------------------------------------', | ||
| 70 | - '# end of coverage report', | ||
| 71 | - ].join('\n'); | ||
| 72 | - | ||
| 73 | 71 | const args = [ | |
| 74 | 72 | '--no-experimental-strip-types', | |
| 75 | 73 | '--test', | |
@@ -82,23 +80,11 @@ describe('test runner coverage default exclusion', skipIfNoInspector, () => { | |||
| 82 | 80 | }); | |
| 83 | 81 | ||
| 84 | 82 | assert.strictEqual(result.stderr.toString(), ''); | |
| 85 | - assert(result.stdout.toString().includes(report)); | ||
| 83 | + assertDefaultExclusions(result.stdout.toString()); | ||
| 86 | 84 | assert.strictEqual(result.status, 0); | |
| 87 | 85 | }); | |
| 88 | 86 | ||
| 89 | 87 | it('should exclude ts test files', async () => { | |
| 90 | - const report = [ | ||
| 91 | - '# start of coverage report', | ||
| 92 | - '# --------------------------------------------------------------', | ||
| 93 | - '# file | line % | branch % | funcs % | uncovered lines', | ||
| 94 | - '# --------------------------------------------------------------', | ||
| 95 | - '# logic-file.js | 66.67 | 100.00 | 50.00 | 5-7', | ||
| 96 | - '# --------------------------------------------------------------', | ||
| 97 | - '# all files | 66.67 | 100.00 | 50.00 | ', | ||
| 98 | - '# --------------------------------------------------------------', | ||
| 99 | - '# end of coverage report', | ||
| 100 | - ].join('\n'); | ||
| 101 | - | ||
| 102 | 88 | const args = [ | |
| 103 | 89 | '--test', | |
| 104 | 90 | '--experimental-test-coverage', | |
@@ -111,7 +97,26 @@ describe('test runner coverage default exclusion', skipIfNoInspector, () => { | |||
| 111 | 97 | }); | |
| 112 | 98 | ||
| 113 | 99 | assert.strictEqual(result.stderr.toString(), ''); | |
| 114 | - assert(result.stdout.toString().includes(report)); | ||
| 100 | + assertDefaultExclusions(result.stdout.toString()); | ||
| 101 | + assert.strictEqual(result.status, 0); | ||
| 102 | + }); | ||
| 103 | + | ||
| 104 | + it('should exclude dotfile test files from coverage by default', async () => { | ||
| 105 | + const args = [ | ||
| 106 | + '--no-experimental-strip-types', | ||
| 107 | + '--test', | ||
| 108 | + '--experimental-test-coverage', | ||
| 109 | + '--test-reporter=tap', | ||
| 110 | + 'test/.dotfile.cjs', | ||
| 111 | + ]; | ||
| 112 | + const result = spawnSync(process.execPath, args, { | ||
| 113 | + env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path }, | ||
| 114 | + cwd: tmpdir.path | ||
| 115 | + }); | ||
| 116 | + | ||
| 117 | + assert.strictEqual(result.stderr.toString(), ''); | ||
| 118 | + assertDefaultExclusions(result.stdout.toString()); | ||
| 119 | + assert.doesNotMatch(result.stdout.toString(), /#\s+\.dotfile\.cjs\s+\|/); | ||
| 115 | 120 | assert.strictEqual(result.status, 0); | |
| 116 | 121 | }); | |
| 117 | 122 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments