Skip to content

Commit fa4abfb

Browse files
committed
add tests
1 parent ee0e5a0 commit fa4abfb

File tree

2 files changed

+114
-36
lines changed

2 files changed

+114
-36
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
serverWebpackConfig,
1313
userNextConfig,
1414
} from '../fixtures';
15-
import { materializeFinalWebpackConfig } from '../testUtils';
15+
import { materializeFinalNextConfig, materializeFinalWebpackConfig } from '../testUtils';
1616

1717
describe('constructWebpackConfigFunction()', () => {
1818
it('includes expected properties', async () => {
@@ -47,6 +47,17 @@ describe('constructWebpackConfigFunction()', () => {
4747
expect(finalWebpackConfig).toEqual(expect.objectContaining(materializedUserWebpackConfig));
4848
});
4949

50+
it("doesn't set devtool if webpack plugin is disabled", () => {
51+
const finalNextConfig = materializeFinalNextConfig({
52+
...exportedNextConfig,
53+
webpack: () => ({ devtool: 'something-besides-source-map' } as any),
54+
sentry: { disableServerWebpackPlugin: true },
55+
});
56+
const finalWebpackConfig = finalNextConfig.webpack?.(serverWebpackConfig, serverBuildContext);
57+
58+
expect(finalWebpackConfig?.devtool).not.toEqual('source-map');
59+
});
60+
5061
it('allows for the use of `hidden-source-map` as `devtool` value for client-side builds', async () => {
5162
const exportedNextConfigHiddenSourceMaps = {
5263
...exportedNextConfig,

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

Lines changed: 102 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'fs';
22
import * as os from 'os';
33
import * as path from 'path';
44

5-
import { BuildContext } from '../../../src/config/types';
5+
import { BuildContext, ExportedNextConfig } from '../../../src/config/types';
66
import { getUserConfigFile, getWebpackPluginOptions, SentryWebpackPlugin } from '../../../src/config/webpack';
77
import {
88
clientBuildContext,
@@ -14,7 +14,7 @@ import {
1414
userSentryWebpackPluginConfig,
1515
} from '../fixtures';
1616
import { exitsSync, mkdtempSyncSpy, mockExistsSync, realExistsSync } from '../mocks';
17-
import { findWebpackPlugin, materializeFinalNextConfig, materializeFinalWebpackConfig } from '../testUtils';
17+
import { findWebpackPlugin, materializeFinalWebpackConfig } from '../testUtils';
1818

1919
describe('Sentry webpack plugin config', () => {
2020
it('includes expected properties', async () => {
@@ -283,44 +283,111 @@ describe('Sentry webpack plugin config', () => {
283283
});
284284
});
285285

286-
describe('disabling SentryWebpackPlugin', () => {
287-
it('allows SentryWebpackPlugin to be turned off for client code (independent of server code)', () => {
288-
const clientFinalNextConfig = materializeFinalNextConfig({
289-
...exportedNextConfig,
290-
sentry: { disableClientWebpackPlugin: true },
291-
});
292-
const clientFinalWebpackConfig = clientFinalNextConfig.webpack?.(clientWebpackConfig, clientBuildContext);
286+
describe('SentryWebpackPlugin enablement', () => {
287+
let processEnvBackup: typeof process.env;
293288

294-
const serverFinalNextConfig = materializeFinalNextConfig(exportedNextConfig, userSentryWebpackPluginConfig);
295-
const serverFinalWebpackConfig = serverFinalNextConfig.webpack?.(serverWebpackConfig, serverBuildContext);
296-
297-
expect(clientFinalWebpackConfig?.plugins).not.toEqual(expect.arrayContaining([expect.any(SentryWebpackPlugin)]));
298-
expect(serverFinalWebpackConfig?.plugins).toEqual(expect.arrayContaining([expect.any(SentryWebpackPlugin)]));
289+
beforeEach(() => {
290+
processEnvBackup = { ...process.env };
299291
});
300-
it('allows SentryWebpackPlugin to be turned off for server code (independent of client code)', () => {
301-
const serverFinalNextConfig = materializeFinalNextConfig({
302-
...exportedNextConfig,
303-
sentry: { disableServerWebpackPlugin: true },
304-
});
305-
const serverFinalWebpackConfig = serverFinalNextConfig.webpack?.(serverWebpackConfig, serverBuildContext);
306292

307-
const clientFinalNextConfig = materializeFinalNextConfig(exportedNextConfig, userSentryWebpackPluginConfig);
308-
const clientFinalWebpackConfig = clientFinalNextConfig.webpack?.(clientWebpackConfig, clientBuildContext);
309-
310-
expect(serverFinalWebpackConfig?.plugins).not.toEqual(expect.arrayContaining([expect.any(SentryWebpackPlugin)]));
311-
expect(clientFinalWebpackConfig?.plugins).toEqual(expect.arrayContaining([expect.any(SentryWebpackPlugin)]));
293+
afterEach(() => {
294+
process.env = processEnvBackup;
312295
});
313296

314-
it("doesn't set devtool if webpack plugin is disabled", () => {
315-
const finalNextConfig = materializeFinalNextConfig({
316-
...exportedNextConfig,
317-
webpack: () => ({ devtool: 'something-besides-source-map' } as any),
318-
sentry: { disableServerWebpackPlugin: true },
319-
});
320-
const finalWebpackConfig = finalNextConfig.webpack?.(serverWebpackConfig, serverBuildContext);
321-
322-
expect(finalWebpackConfig?.devtool).not.toEqual('source-map');
323-
});
297+
it.each([
298+
// [testName, exportedNextConfig, extraEnvValues, shouldFindServerPlugin, shouldFindClientPlugin]
299+
[
300+
'obeys `disableClientWebpackPlugin = true`',
301+
{
302+
...exportedNextConfig,
303+
sentry: { disableClientWebpackPlugin: true },
304+
},
305+
{},
306+
true,
307+
false,
308+
],
309+
310+
[
311+
'obeys `disableServerWebpackPlugin = true`',
312+
{
313+
...exportedNextConfig,
314+
sentry: { disableServerWebpackPlugin: true },
315+
},
316+
{},
317+
false,
318+
true,
319+
],
320+
[
321+
'disables the plugin in Vercel `preview` environment',
322+
exportedNextConfig,
323+
{ VERCEL_ENV: 'preview' },
324+
false,
325+
false,
326+
],
327+
[
328+
'disables the plugin in Vercel `development` environment',
329+
exportedNextConfig,
330+
{ VERCEL_ENV: 'development' },
331+
false,
332+
false,
333+
],
334+
[
335+
'allows `disableClientWebpackPlugin = false` to override env vars`',
336+
{
337+
...exportedNextConfig,
338+
sentry: { disableClientWebpackPlugin: false },
339+
},
340+
{ VERCEL_ENV: 'preview' },
341+
false,
342+
true,
343+
],
344+
[
345+
'allows `disableServerWebpackPlugin = false` to override env vars`',
346+
{
347+
...exportedNextConfig,
348+
sentry: { disableServerWebpackPlugin: false },
349+
},
350+
{ VERCEL_ENV: 'preview' },
351+
true,
352+
false,
353+
],
354+
])(
355+
'%s',
356+
async (
357+
_testName: string,
358+
exportedNextConfig: ExportedNextConfig,
359+
extraEnvValues: Record<string, string>,
360+
shouldFindServerPlugin: boolean,
361+
shouldFindClientPlugin: boolean,
362+
) => {
363+
process.env = { ...process.env, ...extraEnvValues };
364+
365+
// We create a copy of the next config for each `materializeFinalWebpackConfig` call because the `sentry`
366+
// property gets deleted along the way, and its value matters for some of our test cases
367+
const serverFinalWebpackConfig = await materializeFinalWebpackConfig({
368+
exportedNextConfig: { ...exportedNextConfig },
369+
userSentryWebpackPluginConfig,
370+
incomingWebpackConfig: serverWebpackConfig,
371+
incomingWebpackBuildContext: serverBuildContext,
372+
});
373+
374+
const clientFinalWebpackConfig = await materializeFinalWebpackConfig({
375+
exportedNextConfig: { ...exportedNextConfig },
376+
userSentryWebpackPluginConfig,
377+
incomingWebpackConfig: clientWebpackConfig,
378+
incomingWebpackBuildContext: clientBuildContext,
379+
});
380+
381+
const genericSentryWebpackPluginInstance = expect.any(SentryWebpackPlugin);
382+
383+
expect(findWebpackPlugin(serverFinalWebpackConfig, 'SentryCliPlugin')).toEqual(
384+
shouldFindServerPlugin ? genericSentryWebpackPluginInstance : undefined,
385+
);
386+
expect(findWebpackPlugin(clientFinalWebpackConfig, 'SentryCliPlugin')).toEqual(
387+
shouldFindClientPlugin ? genericSentryWebpackPluginInstance : undefined,
388+
);
389+
},
390+
);
324391
});
325392

326393
describe('getUserConfigFile', () => {

0 commit comments

Comments
 (0)