Skip to content

Commit 4d11efb

Browse files
committed
test: fix verification unit tests
1 parent cdd5592 commit 4d11efb

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

test/helpers/verification.spec.ts

+46-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
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'
37
import { outdent } from 'outdent'
48
import type { NetlifyPluginOptions } from '@netlify/build'
5-
import { describeCwdTmpDir, moveNextDist } from "../test-utils"
9+
import { describeCwdTmpDir, moveNextDist } from '../test-utils'
610

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']
817

918
import type { NetlifyPluginUtils } from '@netlify/build'
1019
type FailBuild = NetlifyPluginUtils['build']['failBuild']
@@ -18,6 +27,12 @@ jest.mock('fs', () => {
1827
}
1928
})
2029

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+
2136
describe('checkNextSiteHasBuilt', () => {
2237
let failBuildMock
2338
const { existsSync } = require('fs')
@@ -88,48 +103,65 @@ describe('checkNextSiteHasBuilt', () => {
88103
})
89104

90105
describe('checkZipSize', () => {
91-
let consoleSpy
106+
let consoleWarnSpy, consoleLogSpy
92107
const { existsSync, promises } = require('fs')
93108

94109
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()
96114
process.env.DISABLE_BUNDLE_ZIP_SIZE_CHECK = 'false'
97115
})
98116

99117
afterEach(() => {
100118
delete process.env.DISABLE_BUNDLE_ZIP_SIZE_CHECK
101119
})
102120

121+
afterAll(() => {
122+
consoleWarnSpy.mockReset()
123+
consoleLogSpy.mockReset()
124+
existsSync.mockReset()
125+
})
126+
103127
it('emits a warning that DISABLE_BUNDLE_ZIP_SIZE_CHECK was enabled', async () => {
104128
process.env.DISABLE_BUNDLE_ZIP_SIZE_CHECK = 'true'
105129
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+
)
107133
})
108134

109135
it('does not emit a warning if the file size is below the warning size', async () => {
110136
existsSync.mockReturnValue(true)
111-
jest.spyOn(promises, 'stat').mockResolvedValue({ size: (1024 * 1024 * 20) })
137+
jest.spyOn(promises, 'stat').mockResolvedValue({ size: 1024 * 1024 * 20 })
112138

113139
await checkZipSize('some-file.zip')
114140

115-
expect(consoleSpy).not.toHaveBeenCalled()
141+
expect(consoleWarnSpy).not.toHaveBeenCalled()
116142
})
117143

118144
it('emits a warning if the file size is above the warning size', async () => {
119145
existsSync.mockReturnValue(true)
120-
jest.spyOn(promises, 'stat').mockResolvedValue({ size: (1024 * 1024 * 200) })
146+
jest.spyOn(promises, 'stat').mockResolvedValue({ size: 1024 * 1024 * 200 })
121147

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+
}
123155

124-
expect(consoleSpy).toHaveBeenCalledWith(
156+
expect(consoleLogSpy).toHaveBeenCalledWith(
125157
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+
),
128160
)
129161
})
130162
})
131163

132-
describeCwdTmpDir("getProblematicUserRewrites", () => {
164+
describeCwdTmpDir('getProblematicUserRewrites', () => {
133165
it('finds problematic user rewrites', async () => {
134166
await moveNextDist()
135167
const rewrites = getProblematicUserRewrites({

0 commit comments

Comments
 (0)