|
| 1 | +import { expect } from 'vitest' |
| 2 | +import { isNativeRunner, readCoverageMap, runVitest, test } from '../utils' |
| 3 | + |
| 4 | +/* See {@link file://./../fixtures/src/ignore-node.ts} */ |
| 5 | +const filename = '<process-cwd>/fixtures/src/ignore-node.ts' |
| 6 | + |
| 7 | +test('{ ignoreNode } excludes the matching nodes', async () => { |
| 8 | + await runVitest({ |
| 9 | + include: ['fixtures/test/ignore-node-fixture.test.ts'], |
| 10 | + coverage: { |
| 11 | + reporter: 'json', |
| 12 | + ignoreNode: (node, type) => { |
| 13 | + if ( |
| 14 | + type === 'statement' && |
| 15 | + node.type === 'ExpressionStatement' && |
| 16 | + node.expression.type === 'CallExpression' && |
| 17 | + node.expression.callee.type === 'Identifier' && |
| 18 | + node.expression.callee.name === '__generatedHelper' |
| 19 | + ) { |
| 20 | + return true |
| 21 | + } |
| 22 | + |
| 23 | + return type === 'branch' && node.type === 'IfStatement' |
| 24 | + }, |
| 25 | + }, |
| 26 | + }) |
| 27 | + |
| 28 | + const coverageMap = await readCoverageMap() |
| 29 | + const fileCoverage = coverageMap.fileCoverageFor(filename) |
| 30 | + const lines = fileCoverage.getLineCoverage() |
| 31 | + |
| 32 | + expect(lines[4]).toBeUndefined() |
| 33 | + |
| 34 | + expect(fileCoverage.getBranchCoverageByLine()[6]).toBeUndefined() |
| 35 | + |
| 36 | + expect(lines[7]).toBeGreaterThanOrEqual(1) |
| 37 | + expect(lines[8]).toBeGreaterThanOrEqual(1) |
| 38 | + |
| 39 | + expect(lines[11]).toBe(0) |
| 40 | +}) |
| 41 | + |
| 42 | +test('{ ignoreNode } is not called for nodes that Vitest already ignores', async ({ skip }) => { |
| 43 | + skip(isNativeRunner(), 'Native runner does not transform imports to __vite_ssr_import_') |
| 44 | + |
| 45 | + const declarators: string[] = [] |
| 46 | + |
| 47 | + await runVitest({ |
| 48 | + include: ['fixtures/test/ignore-node-fixture.test.ts'], |
| 49 | + coverage: { |
| 50 | + reporter: 'json', |
| 51 | + ignoreNode: (node) => { |
| 52 | + if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier') { |
| 53 | + declarators.push(node.id.name) |
| 54 | + } |
| 55 | + }, |
| 56 | + }, |
| 57 | + }) |
| 58 | + |
| 59 | + expect(declarators).toEqual(['total']) |
| 60 | +}) |
0 commit comments