1
1
import Chance from 'chance'
2
- import { checkNextSiteHasBuilt , checkZipSize , getProblematicUserRewrites } from '../../packages/runtime/src/helpers/verification'
2
+ import {
3
+ checkNextSiteHasBuilt ,
4
+ checkZipSize ,
5
+ getProblematicUserRewrites ,
6
+ } from '../../packages/runtime/src/helpers/verification'
3
7
import { outdent } from 'outdent'
4
8
import type { NetlifyPluginOptions } from '@netlify/build'
5
- import { describeCwdTmpDir , moveNextDist } from " ../test-utils"
9
+ import { describeCwdTmpDir , moveNextDist } from ' ../test-utils'
6
10
7
- const netlifyConfig = { build : { command : 'npm run build' } , functions : { } , redirects : [ ] , headers : [ ] } as NetlifyPluginOptions [ "netlifyConfig" ]
11
+ const netlifyConfig = {
12
+ build : { command : 'npm run build' } ,
13
+ functions : { } ,
14
+ redirects : [ ] ,
15
+ headers : [ ] ,
16
+ } as NetlifyPluginOptions [ 'netlifyConfig' ]
8
17
9
18
import type { NetlifyPluginUtils } from '@netlify/build'
10
19
type FailBuild = NetlifyPluginUtils [ 'build' ] [ 'failBuild' ]
@@ -18,6 +27,12 @@ jest.mock('fs', () => {
18
27
}
19
28
} )
20
29
30
+ // disable chalk colors to easier validate console text output
31
+ jest . mock ( `chalk` , ( ) => {
32
+ process . env . FORCE_COLOR = '0'
33
+ return jest . requireActual ( 'chalk' )
34
+ } )
35
+
21
36
describe ( 'checkNextSiteHasBuilt' , ( ) => {
22
37
let failBuildMock
23
38
const { existsSync } = require ( 'fs' )
@@ -88,48 +103,65 @@ describe('checkNextSiteHasBuilt', () => {
88
103
} )
89
104
90
105
describe ( 'checkZipSize' , ( ) => {
91
- let consoleSpy
106
+ let consoleWarnSpy , consoleLogSpy
92
107
const { existsSync, promises } = require ( 'fs' )
93
108
94
109
beforeEach ( ( ) => {
95
- consoleSpy = jest . spyOn ( global . console , 'warn' )
110
+ consoleWarnSpy = jest . spyOn ( global . console , 'warn' )
111
+ consoleWarnSpy . mockClear ( )
112
+ consoleLogSpy = jest . spyOn ( global . console , 'log' )
113
+ consoleLogSpy . mockClear ( )
96
114
process . env . DISABLE_BUNDLE_ZIP_SIZE_CHECK = 'false'
97
115
} )
98
116
99
117
afterEach ( ( ) => {
100
118
delete process . env . DISABLE_BUNDLE_ZIP_SIZE_CHECK
101
119
} )
102
120
121
+ afterAll ( ( ) => {
122
+ consoleWarnSpy . mockReset ( )
123
+ consoleLogSpy . mockReset ( )
124
+ existsSync . mockReset ( )
125
+ } )
126
+
103
127
it ( 'emits a warning that DISABLE_BUNDLE_ZIP_SIZE_CHECK was enabled' , async ( ) => {
104
128
process . env . DISABLE_BUNDLE_ZIP_SIZE_CHECK = 'true'
105
129
await checkZipSize ( chance . string ( ) )
106
- expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Function bundle size check was DISABLED with the DISABLE_BUNDLE_ZIP_SIZE_CHECK environment variable. Your deployment will break if it exceeds the maximum supported size of function zip files in your account.' )
130
+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
131
+ 'Function bundle size check was DISABLED with the DISABLE_BUNDLE_ZIP_SIZE_CHECK environment variable. Your deployment will break if it exceeds the maximum supported size of function zip files in your account.' ,
132
+ )
107
133
} )
108
134
109
135
it ( 'does not emit a warning if the file size is below the warning size' , async ( ) => {
110
136
existsSync . mockReturnValue ( true )
111
- jest . spyOn ( promises , 'stat' ) . mockResolvedValue ( { size : ( 1024 * 1024 * 20 ) } )
137
+ jest . spyOn ( promises , 'stat' ) . mockResolvedValue ( { size : 1024 * 1024 * 20 } )
112
138
113
139
await checkZipSize ( 'some-file.zip' )
114
140
115
- expect ( consoleSpy ) . not . toHaveBeenCalled ( )
141
+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
116
142
} )
117
143
118
144
it ( 'emits a warning if the file size is above the warning size' , async ( ) => {
119
145
existsSync . mockReturnValue ( true )
120
- jest . spyOn ( promises , 'stat' ) . mockResolvedValue ( { size : ( 1024 * 1024 * 200 ) } )
146
+ jest . spyOn ( promises , 'stat' ) . mockResolvedValue ( { size : 1024 * 1024 * 200 } )
121
147
122
- await checkZipSize ( 'some-file.zip' )
148
+ try {
149
+ process . env . FORCE_COLOR = '0'
150
+ await checkZipSize ( 'some-file.zip' )
151
+ } catch ( e ) {
152
+ // StreamZip is not mocked, so ultimately the call will throw an error,
153
+ // but we are logging message before that so we can assert it
154
+ }
123
155
124
- expect ( consoleSpy ) . toHaveBeenCalledWith (
156
+ expect ( consoleLogSpy ) . toHaveBeenCalledWith (
125
157
expect . stringContaining (
126
- 'The function zip some-file.zip size is 200 MB, which is larger than the recommended maximum size of 250 MB.'
127
- )
158
+ 'The function zip some-file.zip size is 210 MB, which is larger than the recommended maximum size of 52.4 MB.' ,
159
+ ) ,
128
160
)
129
161
} )
130
162
} )
131
163
132
- describeCwdTmpDir ( " getProblematicUserRewrites" , ( ) => {
164
+ describeCwdTmpDir ( ' getProblematicUserRewrites' , ( ) => {
133
165
it ( 'finds problematic user rewrites' , async ( ) => {
134
166
await moveNextDist ( )
135
167
const rewrites = getProblematicUserRewrites ( {
0 commit comments