1
- import type { NetlifyPluginOptions } from '@netlify/build'
1
+ import type {
2
+ NetlifyPluginConstants ,
3
+ NetlifyPluginOptions ,
4
+ NetlifyPluginUtils ,
5
+ } from '@netlify/build'
2
6
import glob from 'fast-glob'
3
7
import { Mock , afterEach , beforeEach , expect , test , vi } from 'vitest'
4
8
import { mockFileSystem } from '../../../tests/index.js'
5
9
import { FixtureTestContext , createFsFixture } from '../../../tests/utils/fixture.js'
6
- import { getBlobStore } from '../blob.js'
7
- import { STATIC_DIR } from '../constants.js'
10
+ import { BLOB_DIR , STATIC_DIR } from '../constants.js'
8
11
import { copyStaticAssets , uploadStaticContent } from './static.js'
12
+ import * as fs from 'fs'
13
+ import { join } from 'path'
9
14
10
- afterEach ( ( ) => {
11
- vi . restoreAllMocks ( )
12
- } )
13
-
14
- vi . mock ( '../blob.js' , ( ) => ( {
15
- getBlobStore : vi . fn ( ) ,
16
- } ) )
17
-
18
- let mockBlobSet = vi . fn ( )
19
- beforeEach ( ( ) => {
20
- ; ( getBlobStore as Mock ) . mockReturnValue ( {
21
- set : mockBlobSet ,
22
- } )
23
- } )
15
+ const utils = {
16
+ build : { failBuild : vi . fn ( ) } ,
17
+ } as unknown as NetlifyPluginUtils
24
18
25
19
test ( 'should clear the static directory contents' , async ( ) => {
26
20
const PUBLISH_DIR = '.next'
@@ -30,8 +24,8 @@ test('should clear the static directory contents', async () => {
30
24
} )
31
25
32
26
await copyStaticAssets ( {
33
- constants : { PUBLISH_DIR } ,
34
- } as Pick < NetlifyPluginOptions , 'constants' > )
27
+ constants : { PUBLISH_DIR } as NetlifyPluginConstants ,
28
+ } )
35
29
36
30
expect ( Object . keys ( vol . toJSON ( ) ) ) . toEqual (
37
31
expect . not . arrayContaining ( [ `${ STATIC_DIR } /remove-me.js` ] ) ,
@@ -50,8 +44,8 @@ test<FixtureTestContext>('should link static content from the publish directory
50
44
)
51
45
52
46
await copyStaticAssets ( {
53
- constants : { PUBLISH_DIR } ,
54
- } as Pick < NetlifyPluginOptions , 'constants' > )
47
+ constants : { PUBLISH_DIR } as NetlifyPluginConstants ,
48
+ } )
55
49
56
50
const files = await glob ( '**/*' , { cwd, dot : true } )
57
51
@@ -77,11 +71,10 @@ test<FixtureTestContext>('should link static content from the public directory t
77
71
)
78
72
79
73
await copyStaticAssets ( {
80
- constants : { PUBLISH_DIR } ,
81
- } as Pick < NetlifyPluginOptions , 'constants' > )
74
+ constants : { PUBLISH_DIR } as NetlifyPluginConstants ,
75
+ } )
82
76
83
77
const files = await glob ( '**/*' , { cwd, dot : true } )
84
-
85
78
expect ( files ) . toEqual (
86
79
expect . arrayContaining ( [
87
80
'public/another-asset.json' ,
@@ -94,7 +87,6 @@ test<FixtureTestContext>('should link static content from the public directory t
94
87
95
88
test < FixtureTestContext > ( 'should copy the static pages to the publish directory if the routes do not exist in the prerender-manifest' , async ( ctx ) => {
96
89
const PUBLISH_DIR = '.next'
97
-
98
90
const { cwd } = await createFsFixture (
99
91
{
100
92
[ `${ PUBLISH_DIR } /prerender-manifest.json` ] : JSON . stringify ( {
@@ -108,12 +100,14 @@ test<FixtureTestContext>('should copy the static pages to the publish directory
108
100
)
109
101
110
102
await uploadStaticContent ( {
111
- constants : { PUBLISH_DIR } ,
112
- } as Pick < NetlifyPluginOptions , 'constants' > )
103
+ constants : { PUBLISH_DIR } as NetlifyPluginConstants ,
104
+ utils,
105
+ } )
113
106
114
- expect ( mockBlobSet ) . toHaveBeenCalledTimes ( 2 )
115
- expect ( mockBlobSet ) . toHaveBeenCalledWith ( 'server/pages/test.html' , 'test-1' )
116
- expect ( mockBlobSet ) . toHaveBeenCalledWith ( 'server/pages/test2.html' , 'test-2' )
107
+ expect ( ( await glob ( '**/*' , { cwd : join ( cwd , BLOB_DIR ) , dot : true } ) ) . sort ( ) ) . toEqual ( [
108
+ 'server/pages/test.html' ,
109
+ 'server/pages/test2.html' ,
110
+ ] )
117
111
} )
118
112
119
113
test < FixtureTestContext > ( 'should not copy the static pages to the publish directory if the routes exist in the prerender-manifest' , async ( ctx ) => {
@@ -135,8 +129,9 @@ test<FixtureTestContext>('should not copy the static pages to the publish direct
135
129
)
136
130
137
131
await uploadStaticContent ( {
138
- constants : { PUBLISH_DIR } ,
139
- } as Pick < NetlifyPluginOptions , 'constants' > )
132
+ constants : { PUBLISH_DIR } as NetlifyPluginConstants ,
133
+ utils,
134
+ } )
140
135
141
- expect ( mockBlobSet ) . not . toHaveBeenCalled ( )
136
+ expect ( await glob ( '**/*' , { cwd : join ( cwd , BLOB_DIR ) , dot : true } ) ) . toHaveLength ( 0 )
142
137
} )
0 commit comments