@@ -2,6 +2,7 @@ import type { BuildContext, BuildResult } from "esbuild";
22import type { Context } from "../..//types/index.js" ;
33import { context } from "esbuild" ;
44import { copy , outputFile , outputJSON , pathExists } from "fs-extra/esm" ;
5+ import { resolve } from "pathe" ;
56import { glob } from "tinyglobby" ;
67import { CACHE_DIR , TESTER_PLUGIN_DIR } from "../../constant.js" ;
78import { saveResource } from "../../utils/file.js" ;
@@ -38,7 +39,7 @@ export class TestRunnerPlugin {
3839 const esbuildResult = await this . esbuildContext ?. rebuild ( ) ;
3940
4041 // get affected tests
41- const tests = this . getAffectedTests ( changedFile , esbuildResult ?. metafile ) ;
42+ const tests = findImpactedTests ( changedFile , esbuildResult ?. metafile ) ;
4243
4344 // this.generateTestPage
4445 // mocha setup
@@ -144,24 +145,33 @@ export class TestRunnerPlugin {
144145 const html = generateHtml ( setupCode , testFiles ) ;
145146 await outputFile ( `${ TESTER_PLUGIN_DIR } /content/index.xhtml` , html ) ;
146147 }
148+ }
147149
148- /**
149- * 给定源码路径,查找导入了该文件的所有 output file
150- */
151- private getAffectedTests ( changedFile : string , metafile : BuildResult [ "metafile" ] ) : string [ ] {
152- if ( ! metafile )
153- return [ ] ;
154-
155- const affectedEntries = new Set < string > ( ) ;
156-
157- for ( const [ path , info ] of Object . entries ( metafile . outputs ) ) {
158- for ( const imp of info . imports ) {
159- if ( imp . path === changedFile ) {
160- affectedEntries . add ( path ) ;
161- }
162- }
150+ /**
151+ * Determines which test files are impacted by a given changed file based on the esbuild metafile.
152+ *
153+ * This function analyzes the build metadata to find test files that depend on the changed file
154+ * either directly as an entry point or indirectly as an input. It is useful in a watch mode setup
155+ * to selectively rerun only the affected tests.
156+ *
157+ * @param {string } changedFilePath - The file path of the changed source file.
158+ * @param {BuildResult["metafile"] } buildMetadata - The esbuild metafile containing dependency information.
159+ * @returns {string[] } An array of impacted test file paths that need to be re-executed.
160+ */
161+ export function findImpactedTests ( changedFilePath : string , buildMetadata : BuildResult [ "metafile" ] ) : string [ ] {
162+ if ( ! buildMetadata )
163+ return [ ] ;
164+
165+ const resolvedChangedFile = resolve ( changedFilePath ) ;
166+ const impactedTestFiles = new Set < string > ( ) ;
167+
168+ for ( const [ outputFilePath , outputInfo ] of Object . entries ( buildMetadata . outputs ) ) {
169+ const testFilePath = outputFilePath . replace ( `${ TESTER_PLUGIN_DIR } /content/` , "" ) ;
170+ // const resolvedEntryPoint = outputInfo.entryPoint ? resolve(outputInfo.entryPoint) : null;
171+
172+ if ( Object . keys ( outputInfo . inputs ) . some ( inputPath => resolve ( inputPath ) === resolvedChangedFile ) ) {
173+ impactedTestFiles . add ( testFilePath ) ;
163174 }
164-
165- return [ ...affectedEntries ] ;
166175 }
176+ return Array . from ( impactedTestFiles ) ;
167177}
0 commit comments