Skip to content

Commit 4b8db28

Browse files
committed
Add tests for nextjs 13 checks
1 parent 5250cba commit 4b8db28

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ export function constructWebpackConfigFunction(
6060
const pageExtensions = userNextConfig.pageExtensions || ['tsx', 'ts', 'jsx', 'js'];
6161
const dotPrefixedPageExtensions = pageExtensions.map(ext => `.${ext}`);
6262
const pageExtensionRegex = pageExtensions.map(escapeStringForRegex).join('|');
63-
const nextJsVersion = getNextjsVersion();
64-
const { major } = parseSemver(nextJsVersion || '');
63+
const nextVersion = nextJsVersion || getNextjsVersion();
64+
const { major } = parseSemver(nextVersion || '');
6565

6666
// We add `.ts` and `.js` back in because `pageExtensions` might not be relevant to the instrumentation file
6767
// e.g. user's setting `.mdx`. In that case we still want to default look up

packages/nextjs/test/config/webpack/constructWebpackConfig.test.ts

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import '../mocks';
33
import * as core from '@sentry/core';
44
import { describe, expect, it, vi } from 'vitest';
5+
import * as util from '../../../src/config/util';
56
import * as getWebpackPluginOptionsModule from '../../../src/config/webpackPluginOptions';
67
import {
78
CLIENT_SDK_CONFIG_FILE,
@@ -188,8 +189,11 @@ describe('constructWebpackConfigFunction()', () => {
188189
});
189190

190191
describe('edge runtime polyfills', () => {
191-
it('adds polyfills only for edge runtime in dev mode', async () => {
192-
// Test edge runtime in dev mode - should add 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
193197
const edgeDevBuildContext = { ...edgeBuildContext, dev: true };
194198
const edgeDevConfig = await materializeFinalWebpackConfig({
195199
exportedNextConfig,
@@ -201,6 +205,13 @@ describe('constructWebpackConfigFunction()', () => {
201205
expect(edgeProvidePlugin).toBeDefined();
202206
expect(edgeDevConfig.resolve?.alias?.perf_hooks).toMatch(/perf_hooks\.js$/);
203207

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+
204215
// Test edge runtime in prod mode - should NOT add polyfills
205216
const edgeProdBuildContext = { ...edgeBuildContext, dev: false };
206217
const edgeProdConfig = await materializeFinalWebpackConfig({
@@ -212,6 +223,13 @@ describe('constructWebpackConfigFunction()', () => {
212223
const edgeProdProvidePlugin = edgeProdConfig.plugins?.find(plugin => plugin.constructor.name === 'ProvidePlugin');
213224
expect(edgeProdProvidePlugin).toBeUndefined();
214225

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+
215233
// Test server runtime in dev mode - should NOT add polyfills
216234
const serverDevBuildContext = { ...serverBuildContext, dev: true };
217235
const serverDevConfig = await materializeFinalWebpackConfig({
@@ -223,6 +241,13 @@ describe('constructWebpackConfigFunction()', () => {
223241
const serverProvidePlugin = serverDevConfig.plugins?.find(plugin => plugin.constructor.name === 'ProvidePlugin');
224242
expect(serverProvidePlugin).toBeUndefined();
225243

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+
226251
// Test client runtime in dev mode - should NOT add polyfills
227252
const clientDevBuildContext = { ...clientBuildContext, dev: true };
228253
const clientDevConfig = await materializeFinalWebpackConfig({
@@ -233,6 +258,52 @@ describe('constructWebpackConfigFunction()', () => {
233258

234259
const clientProvidePlugin = clientDevConfig.plugins?.find(plugin => plugin.constructor.name === 'ProvidePlugin');
235260
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();
236307
});
237308
});
238309
});

0 commit comments

Comments
 (0)