1- import { describe , it , expect , vi , beforeEach } from 'vitest' ;
1+ import { describe , it , expect , vi , beforeEach , afterEach } from 'vitest' ;
22import { gatherExternalFiles } from '../external-files' ;
33import { BuildOutput , OutputChunk } from '../../../types' ;
44import fs from 'fs-extra' ;
55import path from 'node:path' ;
6+ import os from 'node:os' ;
67import { setFakeWxt } from '../testing/fake-objects' ;
78
8- // Mock fs-extra
9- vi . mock ( 'fs-extra' ) ;
10- const mockFs = vi . mocked ( fs ) ;
11-
129describe ( 'gatherExternalFiles' , ( ) => {
13- beforeEach ( ( ) => {
10+ let tempDir : string ;
11+ let projectDir : string ;
12+ let externalDir : string ;
13+
14+ beforeEach ( async ( ) => {
15+ tempDir = await fs . mkdtemp (
16+ path . join ( os . tmpdir ( ) , 'wxt-external-files-test-' ) ,
17+ ) ;
18+ projectDir = path . join ( tempDir , 'project' ) ;
19+ externalDir = path . join ( tempDir , 'external' ) ;
20+
21+ await fs . ensureDir ( path . join ( projectDir , 'src' ) ) ;
22+ await fs . ensureDir ( externalDir ) ;
1423 vi . clearAllMocks ( ) ;
1524
16- // Setup fake wxt instance with default config
1725 setFakeWxt ( {
1826 config : {
1927 zip : {
20- sourcesRoot : '/project/ src',
28+ sourcesRoot : path . join ( projectDir , ' src') ,
2129 } ,
2230 logger : {
2331 info : vi . fn ( ) ,
@@ -27,6 +35,10 @@ describe('gatherExternalFiles', () => {
2735 } ) ;
2836 } ) ;
2937
38+ afterEach ( async ( ) => {
39+ await fs . remove ( tempDir ) ;
40+ } ) ;
41+
3042 it ( 'should return empty array when no external files are found' , async ( ) => {
3143 const buildOutput : BuildOutput = {
3244 manifest : { manifest_version : 3 , name : 'test' , version : '1.0.0' } ,
@@ -53,15 +65,8 @@ describe('gatherExternalFiles', () => {
5365 } ) ;
5466
5567 it ( 'should include external files that exist outside the project directory' , async ( ) => {
56- const externalFile = '/parent/shared/utils.ts' ;
57-
58- // Mock fs.access to succeed for external file
59- mockFs . access . mockImplementation ( ( filePath ) => {
60- if ( filePath === externalFile ) {
61- return Promise . resolve ( ) ;
62- }
63- return Promise . reject ( new Error ( 'File not found' ) ) ;
64- } ) ;
68+ const externalFile = path . join ( externalDir , 'shared-utils.ts' ) ;
69+ await fs . writeFile ( externalFile , 'export const shared = true;' ) ;
6570
6671 const buildOutput : BuildOutput = {
6772 manifest : { manifest_version : 3 , name : 'test' , version : '1.0.0' } ,
@@ -72,7 +77,10 @@ describe('gatherExternalFiles', () => {
7277 {
7378 type : 'chunk' ,
7479 fileName : 'background.js' ,
75- moduleIds : [ '/project/src/background.ts' , externalFile ] ,
80+ moduleIds : [
81+ path . join ( projectDir , 'src' , 'background.ts' ) ,
82+ externalFile ,
83+ ] ,
7684 } as OutputChunk ,
7785 ] ,
7886 entrypoints : [ ] ,
@@ -82,11 +90,15 @@ describe('gatherExternalFiles', () => {
8290
8391 const result = await gatherExternalFiles ( buildOutput ) ;
8492 expect ( result ) . toEqual ( [ externalFile ] ) ;
85- expect ( mockFs . access ) . toHaveBeenCalledWith ( externalFile ) ;
8693 } ) ;
8794
8895 it ( 'should exclude files in node_modules' , async ( ) => {
89- const nodeModuleFile = '/project/node_modules/some-package/index.js' ;
96+ const nodeModuleFile = path . join (
97+ projectDir ,
98+ 'node_modules' ,
99+ 'some-package' ,
100+ 'index.js' ,
101+ ) ;
90102
91103 const buildOutput : BuildOutput = {
92104 manifest : { manifest_version : 3 , name : 'test' , version : '1.0.0' } ,
@@ -97,7 +109,10 @@ describe('gatherExternalFiles', () => {
97109 {
98110 type : 'chunk' ,
99111 fileName : 'background.js' ,
100- moduleIds : [ '/project/src/background.ts' , nodeModuleFile ] ,
112+ moduleIds : [
113+ path . join ( projectDir , 'src' , 'background.ts' ) ,
114+ nodeModuleFile ,
115+ ] ,
101116 } as OutputChunk ,
102117 ] ,
103118 entrypoints : [ ] ,
@@ -107,7 +122,6 @@ describe('gatherExternalFiles', () => {
107122
108123 const result = await gatherExternalFiles ( buildOutput ) ;
109124 expect ( result ) . toEqual ( [ ] ) ;
110- expect ( mockFs . access ) . not . toHaveBeenCalledWith ( nodeModuleFile ) ;
111125 } ) ;
112126
113127 it ( 'should exclude virtual modules' , async ( ) => {
@@ -122,7 +136,10 @@ describe('gatherExternalFiles', () => {
122136 {
123137 type : 'chunk' ,
124138 fileName : 'background.js' ,
125- moduleIds : [ '/project/src/background.ts' , virtualModule ] ,
139+ moduleIds : [
140+ path . join ( projectDir , 'src' , 'background.ts' ) ,
141+ virtualModule ,
142+ ] ,
126143 } as OutputChunk ,
127144 ] ,
128145 entrypoints : [ ] ,
@@ -132,7 +149,6 @@ describe('gatherExternalFiles', () => {
132149
133150 const result = await gatherExternalFiles ( buildOutput ) ;
134151 expect ( result ) . toEqual ( [ ] ) ;
135- expect ( mockFs . access ) . not . toHaveBeenCalledWith ( virtualModule ) ;
136152 } ) ;
137153
138154 it ( 'should exclude HTTP URLs' , async ( ) => {
@@ -147,7 +163,10 @@ describe('gatherExternalFiles', () => {
147163 {
148164 type : 'chunk' ,
149165 fileName : 'background.js' ,
150- moduleIds : [ '/project/src/background.ts' , httpUrl ] ,
166+ moduleIds : [
167+ path . join ( projectDir , 'src' , 'background.ts' ) ,
168+ httpUrl ,
169+ ] ,
151170 } as OutputChunk ,
152171 ] ,
153172 entrypoints : [ ] ,
@@ -157,14 +176,11 @@ describe('gatherExternalFiles', () => {
157176
158177 const result = await gatherExternalFiles ( buildOutput ) ;
159178 expect ( result ) . toEqual ( [ ] ) ;
160- expect ( mockFs . access ) . not . toHaveBeenCalledWith ( httpUrl ) ;
161179 } ) ;
162180
163181 it ( 'should skip non-existent external files' , async ( ) => {
164- const nonExistentFile = '/parent/missing/file.ts' ;
165-
166- // Mock fs.access to reject for non-existent file
167- mockFs . access . mockRejectedValue ( new Error ( 'File not found' ) ) ;
182+ // Use a path in external dir that we don't create (so it won't exist)
183+ const nonExistentFile = path . join ( externalDir , 'missing-file.ts' ) ;
168184
169185 const buildOutput : BuildOutput = {
170186 manifest : { manifest_version : 3 , name : 'test' , version : '1.0.0' } ,
@@ -175,7 +191,10 @@ describe('gatherExternalFiles', () => {
175191 {
176192 type : 'chunk' ,
177193 fileName : 'background.js' ,
178- moduleIds : [ '/project/src/background.ts' , nonExistentFile ] ,
194+ moduleIds : [
195+ path . join ( projectDir , 'src' , 'background.ts' ) ,
196+ nonExistentFile ,
197+ ] ,
179198 } as OutputChunk ,
180199 ] ,
181200 entrypoints : [ ] ,
@@ -185,20 +204,13 @@ describe('gatherExternalFiles', () => {
185204
186205 const result = await gatherExternalFiles ( buildOutput ) ;
187206 expect ( result ) . toEqual ( [ ] ) ;
188- expect ( mockFs . access ) . toHaveBeenCalledWith ( nonExistentFile ) ;
189207 } ) ;
190208
191209 it ( 'should handle multiple external files and deduplicate them' , async ( ) => {
192- const externalFile1 = '/parent/shared/utils.ts' ;
193- const externalFile2 = '/parent/shared/types.ts' ;
194-
195- // Mock fs.access to succeed for both external files
196- mockFs . access . mockImplementation ( ( filePath ) => {
197- if ( filePath === externalFile1 || filePath === externalFile2 ) {
198- return Promise . resolve ( ) ;
199- }
200- return Promise . reject ( new Error ( 'File not found' ) ) ;
201- } ) ;
210+ const externalFile1 = path . join ( externalDir , 'utils.ts' ) ;
211+ const externalFile2 = path . join ( externalDir , 'types.ts' ) ;
212+ await fs . writeFile ( externalFile1 , 'export const util = true;' ) ;
213+ await fs . writeFile ( externalFile2 , 'export type MyType = string;' ) ;
202214
203215 const buildOutput : BuildOutput = {
204216 manifest : { manifest_version : 3 , name : 'test' , version : '1.0.0' } ,
@@ -210,7 +222,7 @@ describe('gatherExternalFiles', () => {
210222 type : 'chunk' ,
211223 fileName : 'background.js' ,
212224 moduleIds : [
213- '/project/ src/ background.ts',
225+ path . join ( projectDir , ' src' , ' background.ts') ,
214226 externalFile1 ,
215227 externalFile2 ,
216228 externalFile1 , // Duplicate should be ignored
@@ -229,7 +241,8 @@ describe('gatherExternalFiles', () => {
229241 } ) ;
230242
231243 it ( 'should only process chunk-type outputs' , async ( ) => {
232- const externalFile = '/parent/shared/utils.ts' ;
244+ const externalFile = path . join ( externalDir , 'shared-utils.ts' ) ;
245+ await fs . writeFile ( externalFile , 'export const shared = true;' ) ;
233246
234247 const buildOutput : BuildOutput = {
235248 manifest : { manifest_version : 3 , name : 'test' , version : '1.0.0' } ,
@@ -252,11 +265,7 @@ describe('gatherExternalFiles', () => {
252265 ] ,
253266 } ;
254267
255- // Mock fs.access to succeed
256- mockFs . access . mockResolvedValue ( undefined ) ;
257-
258268 const result = await gatherExternalFiles ( buildOutput ) ;
259269 expect ( result ) . toEqual ( [ externalFile ] ) ;
260- expect ( mockFs . access ) . toHaveBeenCalledOnce ( ) ;
261270 } ) ;
262271} ) ;
0 commit comments