|
2 | 2 | import '../mocks';
|
3 | 3 | import * as core from '@sentry/core';
|
4 | 4 | import { describe, expect, it, vi } from 'vitest';
|
| 5 | +import * as util from '../../../src/config/util'; |
5 | 6 | import * as getWebpackPluginOptionsModule from '../../../src/config/webpackPluginOptions';
|
6 | 7 | import {
|
7 | 8 | CLIENT_SDK_CONFIG_FILE,
|
8 | 9 | clientBuildContext,
|
9 | 10 | clientWebpackConfig,
|
| 11 | + edgeBuildContext, |
10 | 12 | exportedNextConfig,
|
11 | 13 | serverBuildContext,
|
12 | 14 | serverWebpackConfig,
|
@@ -185,4 +187,123 @@ describe('constructWebpackConfigFunction()', () => {
|
185 | 187 | });
|
186 | 188 | });
|
187 | 189 | });
|
| 190 | + |
| 191 | + describe('edge runtime polyfills', () => { |
| 192 | + it('adds polyfills only for edge runtime in dev mode on Next.js 13', async () => { |
| 193 | + // Mock Next.js version 13 - polyfills should be added |
| 194 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('13.0.0'); |
| 195 | + |
| 196 | + // Test edge runtime in dev mode with Next.js 13 - should add polyfills |
| 197 | + const edgeDevBuildContext = { ...edgeBuildContext, dev: true }; |
| 198 | + const edgeDevConfig = await materializeFinalWebpackConfig({ |
| 199 | + exportedNextConfig, |
| 200 | + incomingWebpackConfig: serverWebpackConfig, |
| 201 | + incomingWebpackBuildContext: edgeDevBuildContext, |
| 202 | + }); |
| 203 | + |
| 204 | + const edgeProvidePlugin = edgeDevConfig.plugins?.find(plugin => plugin.constructor.name === 'ProvidePlugin'); |
| 205 | + expect(edgeProvidePlugin).toBeDefined(); |
| 206 | + expect(edgeDevConfig.resolve?.alias?.perf_hooks).toMatch(/perf_hooks\.js$/); |
| 207 | + |
| 208 | + vi.restoreAllMocks(); |
| 209 | + }); |
| 210 | + |
| 211 | + it('does NOT add polyfills for edge runtime in prod mode even on Next.js 13', async () => { |
| 212 | + // Mock Next.js version 13 - but prod mode should still not add polyfills |
| 213 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('13.0.0'); |
| 214 | + |
| 215 | + // Test edge runtime in prod mode - should NOT add polyfills |
| 216 | + const edgeProdBuildContext = { ...edgeBuildContext, dev: false }; |
| 217 | + const edgeProdConfig = await materializeFinalWebpackConfig({ |
| 218 | + exportedNextConfig, |
| 219 | + incomingWebpackConfig: serverWebpackConfig, |
| 220 | + incomingWebpackBuildContext: edgeProdBuildContext, |
| 221 | + }); |
| 222 | + |
| 223 | + const edgeProdProvidePlugin = edgeProdConfig.plugins?.find(plugin => plugin.constructor.name === 'ProvidePlugin'); |
| 224 | + expect(edgeProdProvidePlugin).toBeUndefined(); |
| 225 | + |
| 226 | + vi.restoreAllMocks(); |
| 227 | + }); |
| 228 | + |
| 229 | + it('does NOT add polyfills for server runtime even on Next.js 13', async () => { |
| 230 | + // Mock Next.js version 13 |
| 231 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('13.0.0'); |
| 232 | + |
| 233 | + // Test server runtime in dev mode - should NOT add polyfills |
| 234 | + const serverDevBuildContext = { ...serverBuildContext, dev: true }; |
| 235 | + const serverDevConfig = await materializeFinalWebpackConfig({ |
| 236 | + exportedNextConfig, |
| 237 | + incomingWebpackConfig: serverWebpackConfig, |
| 238 | + incomingWebpackBuildContext: serverDevBuildContext, |
| 239 | + }); |
| 240 | + |
| 241 | + const serverProvidePlugin = serverDevConfig.plugins?.find(plugin => plugin.constructor.name === 'ProvidePlugin'); |
| 242 | + expect(serverProvidePlugin).toBeUndefined(); |
| 243 | + |
| 244 | + vi.restoreAllMocks(); |
| 245 | + }); |
| 246 | + |
| 247 | + it('does NOT add polyfills for client runtime even on Next.js 13', async () => { |
| 248 | + // Mock Next.js version 13 |
| 249 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('13.0.0'); |
| 250 | + |
| 251 | + // Test client runtime in dev mode - should NOT add polyfills |
| 252 | + const clientDevBuildContext = { ...clientBuildContext, dev: true }; |
| 253 | + const clientDevConfig = await materializeFinalWebpackConfig({ |
| 254 | + exportedNextConfig, |
| 255 | + incomingWebpackConfig: clientWebpackConfig, |
| 256 | + incomingWebpackBuildContext: clientDevBuildContext, |
| 257 | + }); |
| 258 | + |
| 259 | + const clientProvidePlugin = clientDevConfig.plugins?.find(plugin => plugin.constructor.name === 'ProvidePlugin'); |
| 260 | + expect(clientProvidePlugin).toBeUndefined(); |
| 261 | + |
| 262 | + vi.restoreAllMocks(); |
| 263 | + }); |
| 264 | + |
| 265 | + it('does NOT add polyfills for edge runtime in dev mode on Next.js versions other than 13', async () => { |
| 266 | + const edgeDevBuildContext = { ...edgeBuildContext, dev: true }; |
| 267 | + |
| 268 | + // Test with Next.js 12 - should NOT add polyfills |
| 269 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('12.3.0'); |
| 270 | + const edgeConfigV12 = await materializeFinalWebpackConfig({ |
| 271 | + exportedNextConfig, |
| 272 | + incomingWebpackConfig: serverWebpackConfig, |
| 273 | + incomingWebpackBuildContext: edgeDevBuildContext, |
| 274 | + }); |
| 275 | + expect(edgeConfigV12.plugins?.find(plugin => plugin.constructor.name === 'ProvidePlugin')).toBeUndefined(); |
| 276 | + vi.restoreAllMocks(); |
| 277 | + |
| 278 | + // Test with Next.js 14 - should NOT add polyfills |
| 279 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('14.0.0'); |
| 280 | + const edgeConfigV14 = await materializeFinalWebpackConfig({ |
| 281 | + exportedNextConfig, |
| 282 | + incomingWebpackConfig: serverWebpackConfig, |
| 283 | + incomingWebpackBuildContext: edgeDevBuildContext, |
| 284 | + }); |
| 285 | + expect(edgeConfigV14.plugins?.find(plugin => plugin.constructor.name === 'ProvidePlugin')).toBeUndefined(); |
| 286 | + vi.restoreAllMocks(); |
| 287 | + |
| 288 | + // Test with Next.js 15 - should NOT add polyfills |
| 289 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.0.0'); |
| 290 | + const edgeConfigV15 = await materializeFinalWebpackConfig({ |
| 291 | + exportedNextConfig, |
| 292 | + incomingWebpackConfig: serverWebpackConfig, |
| 293 | + incomingWebpackBuildContext: edgeDevBuildContext, |
| 294 | + }); |
| 295 | + expect(edgeConfigV15.plugins?.find(plugin => plugin.constructor.name === 'ProvidePlugin')).toBeUndefined(); |
| 296 | + vi.restoreAllMocks(); |
| 297 | + |
| 298 | + // Test with undefined Next.js version - should NOT add polyfills |
| 299 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue(undefined); |
| 300 | + const edgeConfigUndefined = await materializeFinalWebpackConfig({ |
| 301 | + exportedNextConfig, |
| 302 | + incomingWebpackConfig: serverWebpackConfig, |
| 303 | + incomingWebpackBuildContext: edgeDevBuildContext, |
| 304 | + }); |
| 305 | + expect(edgeConfigUndefined.plugins?.find(plugin => plugin.constructor.name === 'ProvidePlugin')).toBeUndefined(); |
| 306 | + vi.restoreAllMocks(); |
| 307 | + }); |
| 308 | + }); |
188 | 309 | });
|
0 commit comments