1
- import { fs , vol } from 'memfs '
1
+ import { type getDeployStore } from '@netlify/blobs '
2
2
import { join } from 'node:path'
3
- import { afterEach , expect , test , vi } from 'vitest'
4
- import { fsCpHelper , mockFileSystem } from '../../../tests/index.js'
3
+ import { beforeEach , expect , test , vi } from 'vitest'
4
+ import { mockFileSystem } from '../../../tests/index.js'
5
5
import { BUILD_DIR } from '../constants.js'
6
6
import { copyStaticContent } from './static.js'
7
7
8
- vi . mock ( 'node:fs' , ( ) => fs )
9
- vi . mock ( 'node:fs/promises' , ( ) => {
8
+ vi . mock ( 'node:fs' , async ( ) => {
9
+ const unionFs : any = ( await import ( 'unionfs' ) ) . default
10
+ const fs = await vi . importActual < typeof import ( 'fs' ) > ( 'node:fs' )
11
+ unionFs . reset = ( ) => {
12
+ unionFs . fss = [ fs ]
13
+ }
14
+ const united = unionFs . use ( fs )
15
+ return { default : united , ...united }
16
+ } )
17
+
18
+ vi . mock ( 'node:fs/promises' , async ( ) => {
19
+ const fs = await import ( 'node:fs' )
20
+ const { fsCpHelper, rmHelper } = await import ( '../../../tests/utils/fs-helper.js' )
10
21
return {
11
22
...fs . promises ,
12
- cp : ( src , dest , options ) => fsCpHelper ( src , dest , options ) ,
23
+ rm : rmHelper ,
24
+ cp : fsCpHelper ,
13
25
}
14
26
} )
15
27
16
- afterEach ( ( ) => {
17
- vol . reset ( )
18
- vi . restoreAllMocks ( )
28
+ let fakeBlob : ReturnType < typeof getDeployStore >
29
+
30
+ beforeEach ( ( ) => {
31
+ fakeBlob = {
32
+ set : vi . fn ( ) ,
33
+ } as unknown as ReturnType < typeof getDeployStore >
19
34
} )
20
35
21
36
test ( 'should copy the static assets from the build to the publish directory' , async ( ) => {
22
- const cwd = mockFileSystem ( {
37
+ const { cwd, vol } = mockFileSystem ( {
23
38
[ `${ BUILD_DIR } /.next/static/test.js` ] : '' ,
24
39
[ `${ BUILD_DIR } /.next/static/sub-dir/test2.js` ] : '' ,
25
40
} )
26
41
27
42
const PUBLISH_DIR = join ( cwd , 'publish' )
28
- await copyStaticContent ( { PUBLISH_DIR } )
43
+ await copyStaticContent ( { PUBLISH_DIR } , fakeBlob )
29
44
30
- const filenamesInVolume = Object . keys ( vol . toJSON ( ) )
31
- expect ( filenamesInVolume ) . toEqual (
45
+ expect ( fakeBlob . set ) . toHaveBeenCalledTimes ( 0 )
46
+ expect ( Object . keys ( vol . toJSON ( ) ) ) . toEqual (
32
47
expect . arrayContaining ( [
33
48
`${ PUBLISH_DIR } /_next/static/test.js` ,
34
49
`${ PUBLISH_DIR } /_next/static/sub-dir/test2.js` ,
@@ -37,39 +52,38 @@ test('should copy the static assets from the build to the publish directory', as
37
52
} )
38
53
39
54
test ( 'should throw expected error if no static assets directory exists' , async ( ) => {
40
- const cwd = mockFileSystem ( { } )
55
+ const { cwd } = mockFileSystem ( { } )
41
56
42
57
const PUBLISH_DIR = join ( cwd , 'publish' )
43
58
const staticDirectory = join ( cwd , '.netlify/.next/static' )
44
59
45
- await expect ( copyStaticContent ( { PUBLISH_DIR } ) ) . rejects . toThrowError (
60
+ await expect ( copyStaticContent ( { PUBLISH_DIR } , fakeBlob ) ) . rejects . toThrowError (
46
61
`Failed to copy static assets: Error: ENOENT: no such file or directory, readdir '${ staticDirectory } '` ,
47
62
)
48
63
} )
49
64
50
65
test ( 'should copy files from the public directory to the publish directory' , async ( ) => {
51
- const cwd = mockFileSystem ( {
66
+ const { cwd, vol } = mockFileSystem ( {
52
67
[ `${ BUILD_DIR } /.next/static/test.js` ] : '' ,
53
68
'public/fake-image.svg' : '' ,
54
69
'public/another-asset.json' : '' ,
55
70
} )
56
71
57
72
const PUBLISH_DIR = join ( cwd , 'publish' )
58
- await copyStaticContent ( { PUBLISH_DIR } )
73
+ await copyStaticContent ( { PUBLISH_DIR } , fakeBlob )
59
74
60
- const filenamesInVolume = Object . keys ( vol . toJSON ( ) )
61
- expect ( filenamesInVolume ) . toEqual (
75
+ expect ( Object . keys ( vol . toJSON ( ) ) ) . toEqual (
62
76
expect . arrayContaining ( [ `${ PUBLISH_DIR } /fake-image.svg` , `${ PUBLISH_DIR } /another-asset.json` ] ) ,
63
77
)
64
78
} )
65
79
66
80
test ( 'should not copy files if the public directory does not exist' , async ( ) => {
67
- const cwd = mockFileSystem ( {
81
+ const { cwd, vol } = mockFileSystem ( {
68
82
[ `${ BUILD_DIR } /.next/static/test.js` ] : '' ,
69
83
} )
70
84
71
85
const PUBLISH_DIR = join ( cwd , 'publish' )
72
- await expect ( copyStaticContent ( { PUBLISH_DIR } ) ) . resolves . toBeUndefined ( )
86
+ await expect ( copyStaticContent ( { PUBLISH_DIR } , fakeBlob ) ) . resolves . toBeUndefined ( )
73
87
74
88
expect ( vol . toJSON ( ) ) . toEqual ( {
75
89
[ join ( cwd , `${ BUILD_DIR } /.next/static/test.js` ) ] : '' ,
0 commit comments