Skip to content

Commit 49cabc0

Browse files
committed
Test: Baseline existing jsdom coverage
1 parent 00531d2 commit 49cabc0

2 files changed

Lines changed: 426 additions & 28 deletions

File tree

‎test/unit/scripts/validate-vitest-conventions.mjs‎

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import typescriptEslintParser from '@typescript-eslint/parser';
1414
import globPackage from 'glob';
1515
import {
1616
discoverTestFiles,
17+
getVitestProjectName,
1718
getVitestTestsByProject,
1819
VITEST_PROJECT_NAMES,
1920
} from './discover-test-files.mjs';
@@ -38,13 +39,24 @@ const policyExceptions = JSON.parse(
3839
)
3940
);
4041
const renderedUiBaseline = new Set( policyExceptions.renderedUi );
42+
const discoveredTests = discoverTestFiles( ROOT_DIR );
4143
const vitestTestsByProject = getVitestTestsByProject(
42-
discoverTestFiles( ROOT_DIR ),
44+
discoveredTests,
4345
migration
4446
);
4547
const vitestTests = Object.values( vitestTestsByProject ).flat().sort();
4648
const jsdomTests = new Set( vitestTestsByProject.jsdom );
4749
const browserTests = new Set( vitestTestsByProject.browser );
50+
const allJsdomTests = new Set(
51+
discoveredTests.filter(
52+
( file ) => getVitestProjectName( file ) === 'jsdom'
53+
)
54+
);
55+
const allBrowserTests = new Set(
56+
discoveredTests.filter(
57+
( file ) => getVitestProjectName( file ) === 'browser'
58+
)
59+
);
4860
const vitestInfrastructure = [
4961
'test/unit/vitest.config.mjs',
5062
...glob( 'test/unit/config/**/*.vitest*.{js,jsx,mjs,ts,tsx}', {
@@ -84,15 +96,15 @@ if ( renderedUiBaseline.size !== policyExceptions.renderedUi.length ) {
8496
violations.push( 'Rendered UI jsdom baseline entries must be unique' );
8597
}
8698
for ( const file of renderedUiBaseline ) {
87-
if ( ! jsdomTests.has( file ) ) {
99+
if ( ! allJsdomTests.has( file ) ) {
88100
violations.push(
89101
`${ file }: rendered UI baseline entries must be jsdom tests`
90102
);
91103
}
92104
}
93105
for ( const [ exceptionName, projectTests ] of [
94-
[ 'browserFireEvent', browserTests ],
95-
[ 'jsdomBrowserApis', jsdomTests ],
106+
[ 'browserFireEvent', allBrowserTests ],
107+
[ 'jsdomBrowserApis', allJsdomTests ],
96108
] ) {
97109
for ( const [ file, reason ] of Object.entries(
98110
policyExceptions[ exceptionName ]
@@ -262,6 +274,26 @@ for ( const file of vitestTests ) {
262274
}
263275
}
264276

277+
for ( const file of [ ...allJsdomTests, ...allBrowserTests ].sort() ) {
278+
const source = readFileSync( path.join( ROOT_DIR, file ), 'utf8' );
279+
const project = getVitestProjectName( file );
280+
281+
violations.push(
282+
...validateVitestPolicy( {
283+
file,
284+
source,
285+
project,
286+
allowBrowserFireEvent: Boolean(
287+
policyExceptions.browserFireEvent[ file ]
288+
),
289+
allowJsdomBrowserApis: Boolean(
290+
policyExceptions.jsdomBrowserApis[ file ]
291+
),
292+
allowRenderedUi: renderedUiBaseline.has( file ),
293+
} )
294+
);
295+
}
296+
265297
for ( const file of files ) {
266298
const filename = path.join( ROOT_DIR, file );
267299
const source = readFileSync( filename, 'utf8' );
@@ -279,28 +311,6 @@ for ( const file of files ) {
279311
( reference ) => reference.identifier
280312
) ?? []
281313
);
282-
let project = 'node';
283-
if ( browserTests.has( file ) ) {
284-
project = 'browser';
285-
} else if ( jsdomTests.has( file ) ) {
286-
project = 'jsdom';
287-
}
288-
289-
violations.push(
290-
...validateVitestPolicy( {
291-
file,
292-
source,
293-
project,
294-
allowBrowserFireEvent: Boolean(
295-
policyExceptions.browserFireEvent[ file ]
296-
),
297-
allowJsdomBrowserApis: Boolean(
298-
policyExceptions.jsdomBrowserApis[ file ]
299-
),
300-
allowRenderedUi: renderedUiBaseline.has( file ),
301-
} )
302-
);
303-
304314
traverseAst( ast, visitorKeys, {
305315
AssignmentExpression( node ) {
306316
if ( isCommonJsExport( node.left ) ) {
@@ -542,7 +552,9 @@ for ( const projectName of VITEST_PROJECT_NAMES ) {
542552
console.log(
543553
`Validated ${ vitestTests.length } Vitest tests, ${
544554
files.length
545-
} ESM graph files, ${
555+
} ESM graph files, ${ allJsdomTests.size } jsdom policy files, ${
556+
allBrowserTests.size
557+
} Browser policy files, ${
546558
vitestVersions.size
547559
} workspace dependencies, and ${ typescriptTestCount } TypeScript ${
548560
typescriptTestCount === 1 ? 'test' : 'tests'

0 commit comments

Comments
 (0)