test: exclude mock from coverage · nodejs/node@fb0a6fb

7 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -28,7 +28,7 @@ const {

2828

const { setupCoverageHooks } = require('internal/util');

2929

const { tmpdir } = require('os');

3030

const { join, resolve, relative } = require('path');

31-

const { fileURLToPath } = require('internal/url');

31+

const { fileURLToPath, URL } = require('internal/url');

3232

const { kMappings, SourceMap } = require('internal/source_map/source_map');

3333

const {

3434

codes: {

@@ -37,6 +37,7 @@ const {

3737

},

3838

} = require('internal/errors');

3939

const { matchGlobPattern } = require('internal/fs/glob');

40+

const { kMockSearchParam } = require('internal/test_runner/mock/mock');

4041
4142

const kCoverageFileRegex = /^coverage-(\d+)-(\d{13})-(\d+)\.json$/;

4243

const kIgnoreRegex = /\/\* node:coverage ignore next (?<count>\d+ )?\*\//;

@@ -497,6 +498,11 @@ class TestCoverage {

497498

return true;

498499

}

499500
501+

const searchParams = new URL(url).searchParams;

502+

if (searchParams.get(kMockSearchParam)) {

503+

return true;

504+

}

505+
500506

// This check filters out the node_modules/ directory, unless it is explicitly included.

501507

return StringPrototypeIncludes(url, '/node_modules/');

502508

}

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,5 @@

1+

import {getData, sum} from './sum.js'

2+
3+

export const theModuleSum = (a, b) => sum(a,b)

4+
5+

export const theModuleGetData = () => getData()

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,9 @@

1+

// console.log('imported')

2+
3+

const data = { type: 'object' }

4+
5+

// console.log(data)

6+
7+

export const sum = (a, b) => a + b

8+
9+

export const getData = () => data

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,17 @@

1+

import { describe, it, mock } from 'node:test';

2+
3+

describe('module test with mock', async () => {

4+

mock.module('../coverage-with-mock/sum.js', {

5+

namedExports: {

6+

sum: (a, b) => 1,

7+

getData: () => ({}),

8+

},

9+

});

10+
11+

const { theModuleSum, theModuleGetData } = await import('../coverage-with-mock/module.js');

12+
13+

it('tests correct thing', (t) => {

14+

t.assert.strictEqual(theModuleSum(1, 2), 1);

15+

t.assert.deepStrictEqual(theModuleGetData(), {});

16+

});

17+

});

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,38 @@

1+

TAP version 13

2+

# Subtest: module test with mock

3+

# Subtest: tests correct thing

4+

ok 1 - tests correct thing

5+

---

6+

duration_ms: *

7+

type: 'test'

8+

...

9+

1..1

10+

ok 1 - module test with mock

11+

---

12+

duration_ms: *

13+

type: 'suite'

14+

...

15+

1..1

16+

# tests 1

17+

# suites 1

18+

# pass 1

19+

# fail 0

20+

# cancelled 0

21+

# skipped 0

22+

# todo 0

23+

# duration_ms *

24+

# start of coverage report

25+

# ---------------------------------------------------------------------------

26+

# file | line % | branch % | funcs % | uncovered lines

27+

# ---------------------------------------------------------------------------

28+

# test | | | |

29+

# fixtures | | | |

30+

# test-runner | | | |

31+

# coverage-with-mock | | | |

32+

# module.js | 100.00 | 100.00 | 100.00 |

33+

# output | | | |

34+

# coverage-with-mock.mjs | 100.00 | 100.00 | 100.00 |

35+

# ---------------------------------------------------------------------------

36+

# all files | 100.00 | 100.00 | 100.00 |

37+

# ---------------------------------------------------------------------------

38+

# end of coverage report

Original file line numberDiff line numberDiff line change

@@ -29,11 +29,11 @@ ok 1 - foo

2929

# fixtures | | | |

3030

# test-runner | | | |

3131

# coverage | | | |

32-

# bar.mts | 0.00 | 100.00 | 100.00 | 1-3

32+

# bar.mts | 33.33 | 100.00 | 0.00 | 2-3

3333

# foo.mts | 100.00 | 100.00 | 100.00 |

3434

# output | | | |

3535

# typescript-coverage.mts | 100.00 | 100.00 | 100.00 |

3636

# ----------------------------------------------------------------------------

37-

# all files | 85.29 | 100.00 | 85.71 |

37+

# all files | 93.55 | 100.00 | 85.71 |

3838

# ----------------------------------------------------------------------------

3939

# end of coverage report

Original file line numberDiff line numberDiff line change

@@ -329,6 +329,15 @@ const tests = [

329329

'--experimental-test-coverage',

330330

'--test-coverage-exclude=!test/**']

331331

} : false,

332+

process.features.inspector ? {

333+

name: 'test-runner/output/coverage-with-mock.mjs',

334+

flags: ['--disable-warning=ExperimentalWarning',

335+

'--test-reporter=tap',

336+

'--experimental-transform-types',

337+

'--experimental-test-module-mocks',

338+

'--experimental-test-coverage',

339+

'--test-coverage-exclude=!test/**']

340+

} : false,

332341

]

333342

.filter(Boolean)

334343

.map(({ flags, name, tty, transform, cwd }) => ({