Skip to content

Commit fae7557

Browse files
committed
test: use enums in tests and not string values
1 parent a68962a commit fae7557

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/build/skew-protection.test.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import type { Span } from '@opentelemetry/api'
55
import { afterEach, beforeEach, describe, expect, it, MockInstance, vi } from 'vitest'
66

77
import type { PluginContext } from './plugin-context.js'
8-
import { setSkewProtection, shouldEnableSkewProtection } from './skew-protection.js'
8+
import {
9+
EnabledOrDisabledReason,
10+
setSkewProtection,
11+
shouldEnableSkewProtection,
12+
} from './skew-protection.js'
913

1014
// Mock fs promises
1115
vi.mock('node:fs/promises', () => ({
@@ -52,7 +56,7 @@ describe('shouldEnableSkewProtection', () => {
5256

5357
expect(result).toEqual({
5458
enabled: false,
55-
enabledOrDisabledReason: 'off-default',
59+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_OUT_DEFAULT,
5660
})
5761
})
5862
})
@@ -65,7 +69,7 @@ describe('shouldEnableSkewProtection', () => {
6569

6670
expect(result).toEqual({
6771
enabled: true,
68-
enabledOrDisabledReason: 'on-env-var',
72+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_IN_ENV_VAR,
6973
})
7074
})
7175

@@ -76,7 +80,7 @@ describe('shouldEnableSkewProtection', () => {
7680

7781
expect(result).toEqual({
7882
enabled: true,
79-
enabledOrDisabledReason: 'on-env-var',
83+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_IN_ENV_VAR,
8084
})
8185
})
8286
})
@@ -89,7 +93,7 @@ describe('shouldEnableSkewProtection', () => {
8993

9094
expect(result).toEqual({
9195
enabled: false,
92-
enabledOrDisabledReason: 'off-env-var',
96+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_OUT_ENV_VAR,
9397
})
9498
})
9599

@@ -100,7 +104,7 @@ describe('shouldEnableSkewProtection', () => {
100104

101105
expect(result).toEqual({
102106
enabled: false,
103-
enabledOrDisabledReason: 'off-env-var',
107+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_OUT_ENV_VAR,
104108
})
105109
})
106110
})
@@ -113,7 +117,7 @@ describe('shouldEnableSkewProtection', () => {
113117

114118
expect(result).toEqual({
115119
enabled: true,
116-
enabledOrDisabledReason: 'on-ff',
120+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_IN_FF,
117121
})
118122
})
119123

@@ -124,7 +128,7 @@ describe('shouldEnableSkewProtection', () => {
124128

125129
expect(result).toEqual({
126130
enabled: false,
127-
enabledOrDisabledReason: 'off-default',
131+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_OUT_DEFAULT,
128132
})
129133
})
130134
})
@@ -138,7 +142,7 @@ describe('shouldEnableSkewProtection', () => {
138142

139143
expect(result).toEqual({
140144
enabled: false,
141-
enabledOrDisabledReason: 'off-no-valid-deploy-id',
145+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_OUT_NO_VALID_DEPLOY_ID,
142146
})
143147
})
144148

@@ -150,7 +154,7 @@ describe('shouldEnableSkewProtection', () => {
150154

151155
expect(result).toEqual({
152156
enabled: false,
153-
enabledOrDisabledReason: 'off-no-valid-deploy-id',
157+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_OUT_NO_VALID_DEPLOY_ID,
154158
})
155159
})
156160

@@ -163,7 +167,7 @@ describe('shouldEnableSkewProtection', () => {
163167

164168
expect(result).toEqual({
165169
enabled: false,
166-
enabledOrDisabledReason: 'off-no-valid-deploy-id-env-var',
170+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_OUT_NO_VALID_DEPLOY_ID_ENV_VAR,
167171
})
168172
})
169173
})
@@ -177,7 +181,7 @@ describe('shouldEnableSkewProtection', () => {
177181

178182
expect(result).toEqual({
179183
enabled: false,
180-
enabledOrDisabledReason: 'off-env-var',
184+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_OUT_ENV_VAR,
181185
})
182186
})
183187

@@ -189,7 +193,7 @@ describe('shouldEnableSkewProtection', () => {
189193

190194
expect(result).toEqual({
191195
enabled: true,
192-
enabledOrDisabledReason: 'on-env-var',
196+
enabledOrDisabledReason: EnabledOrDisabledReason.OPT_IN_ENV_VAR,
193197
})
194198
})
195199
})
@@ -248,7 +252,10 @@ describe('setSkewProtection', () => {
248252
it('should set span attribute and return early when disabled', async () => {
249253
await setSkewProtection(mockCtx, mockSpan)
250254

251-
expect(mockSpan.setAttribute).toHaveBeenCalledWith('skewProtection', 'off-default')
255+
expect(mockSpan.setAttribute).toHaveBeenCalledWith(
256+
'skewProtection',
257+
EnabledOrDisabledReason.OPT_OUT_DEFAULT,
258+
)
252259
expect(mkdir).not.toHaveBeenCalled()
253260
expect(writeFile).not.toHaveBeenCalled()
254261
expect(consoleSpy.log).not.toHaveBeenCalled()
@@ -264,7 +271,7 @@ describe('setSkewProtection', () => {
264271

265272
expect(mockSpan.setAttribute).toHaveBeenCalledWith(
266273
'skewProtection',
267-
'off-no-valid-deploy-id-env-var',
274+
EnabledOrDisabledReason.OPT_OUT_NO_VALID_DEPLOY_ID_ENV_VAR,
268275
)
269276
expect(consoleSpy.warn).toHaveBeenCalledWith(
270277
'NETLIFY_NEXT_SKEW_PROTECTION environment variable is set to true, but skew protection is currently unavailable for CLI deploys. Skew protection will not be enabled.',
@@ -280,7 +287,10 @@ describe('setSkewProtection', () => {
280287

281288
await setSkewProtection(mockCtx, mockSpan)
282289

283-
expect(mockSpan.setAttribute).toHaveBeenCalledWith('skewProtection', 'on-env-var')
290+
expect(mockSpan.setAttribute).toHaveBeenCalledWith(
291+
'skewProtection',
292+
EnabledOrDisabledReason.OPT_IN_ENV_VAR,
293+
)
284294
expect(consoleSpy.log).toHaveBeenCalledWith(
285295
'Setting up Next.js Skew Protection due to NETLIFY_NEXT_SKEW_PROTECTION=true environment variable.',
286296
)
@@ -319,7 +329,10 @@ describe('setSkewProtection', () => {
319329

320330
await setSkewProtection(mockCtx, mockSpan)
321331

322-
expect(mockSpan.setAttribute).toHaveBeenCalledWith('skewProtection', 'on-ff')
332+
expect(mockSpan.setAttribute).toHaveBeenCalledWith(
333+
'skewProtection',
334+
EnabledOrDisabledReason.OPT_IN_FF,
335+
)
323336
expect(consoleSpy.log).toHaveBeenCalledWith('Setting up Next.js Skew Protection.')
324337
expect(process.env.NEXT_DEPLOYMENT_ID).toBe('test-deploy-id')
325338
expect(mkdir).toHaveBeenCalledWith('/test/path', { recursive: true })

src/build/skew-protection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { Span } from '@opentelemetry/api'
66
import type { PluginContext } from './plugin-context.js'
77

88
// eslint-disable-next-line no-shadow
9-
const enum EnabledOrDisabledReason {
9+
export const enum EnabledOrDisabledReason {
1010
OPT_OUT_DEFAULT = 'off-default',
1111
OPT_OUT_NO_VALID_DEPLOY_ID = 'off-no-valid-deploy-id',
1212
OPT_OUT_NO_VALID_DEPLOY_ID_ENV_VAR = 'off-no-valid-deploy-id-env-var',

0 commit comments

Comments
 (0)