Skip to content

Commit 5891e4d

Browse files
author
Luca Forstner
committed
tests n stuff
1 parent fbea9d5 commit 5891e4d

File tree

8 files changed

+52
-57
lines changed

8 files changed

+52
-57
lines changed

packages/nextjs/src/config/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export type NextConfigObject = {
4848
>;
4949
};
5050

51-
export type UserSentryOptions = {
51+
export type SentryBuildtimeOptions = {
5252
/**
5353
* Override the SDK's default decision about whether or not to enable to the Sentry webpack plugin for server files.
5454
* Note that `false` forces the plugin to be enabled, even in situations where it's not recommended.

packages/nextjs/src/config/webpack.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
EntryPropertyObject,
1919
NextConfigObject,
2020
SentryWebpackPluginOptions,
21-
UserSentryOptions,
21+
SentryBuildtimeOptions,
2222
WebpackConfigFunction,
2323
WebpackConfigObject,
2424
WebpackConfigObjectWithModuleRules,
@@ -61,7 +61,7 @@ let showedMissingGlobalErrorWarningMsg = false;
6161
export function constructWebpackConfigFunction(
6262
userNextConfig: NextConfigObject = {},
6363
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
64-
userSentryOptions: UserSentryOptions = {},
64+
userSentryOptions: SentryBuildtimeOptions = {},
6565
): WebpackConfigFunction {
6666
// Will be called by nextjs and passed its default webpack configuration and context data about the build (whether
6767
// we're building server or client, whether we're in dev, what version of webpack we're using, etc). Note that
@@ -511,7 +511,7 @@ function findTranspilationRules(rules: WebpackModuleRule[] | undefined, projectD
511511
async function addSentryToEntryProperty(
512512
currentEntryProperty: WebpackEntryProperty,
513513
buildContext: BuildContext,
514-
userSentryOptions: UserSentryOptions,
514+
userSentryOptions: SentryBuildtimeOptions,
515515
): Promise<EntryPropertyObject> {
516516
// The `entry` entry in a webpack config can be a string, array of strings, object, or function. By default, nextjs
517517
// sets it to an async function which returns the promise of an object of string arrays. Because we don't know whether
@@ -725,7 +725,7 @@ function shouldAddSentryToEntryPoint(entryPointName: string, runtime: 'node' | '
725725
export function getWebpackPluginOptions(
726726
buildContext: BuildContext,
727727
userPluginOptions: Partial<SentryWebpackPluginOptions>,
728-
userSentryOptions: UserSentryOptions,
728+
userSentryOptions: SentryBuildtimeOptions,
729729
): SentryWebpackPluginOptions {
730730
const { buildId, isServer, config, dir: projectDir } = buildContext;
731731
const userNextConfig = config as NextConfigObject;
@@ -884,7 +884,7 @@ export function getWebpackPluginOptions(
884884
}
885885

886886
/** Check various conditions to decide if we should run the plugin */
887-
function shouldEnableWebpackPlugin(buildContext: BuildContext, userSentryOptions: UserSentryOptions): boolean {
887+
function shouldEnableWebpackPlugin(buildContext: BuildContext, userSentryOptions: SentryBuildtimeOptions): boolean {
888888
const { isServer } = buildContext;
889889
const { disableServerWebpackPlugin, disableClientWebpackPlugin } = userSentryOptions;
890890

@@ -900,7 +900,7 @@ function shouldEnableWebpackPlugin(buildContext: BuildContext, userSentryOptions
900900
/** Handle warning messages about `hideSourceMaps` option. Can be removed in v9 or v10 (or whenever we consider that
901901
* enough people will have upgraded the SDK that the warning about the default in v8 - currently commented out - is
902902
* overkill). */
903-
function handleSourcemapHidingOptionWarning(userSentryOptions: UserSentryOptions, isServer: boolean): void {
903+
function handleSourcemapHidingOptionWarning(userSentryOptions: SentryBuildtimeOptions, isServer: boolean): void {
904904
// This is nextjs's own logging formatting, vendored since it's not exported. See
905905
// https://github.com/vercel/next.js/blob/c3ceeb03abb1b262032bd96457e224497d3bbcef/packages/next/build/output/log.ts#L3-L11
906906
// and
@@ -969,7 +969,7 @@ function setUpModuleRules(newConfig: WebpackConfigObject): WebpackConfigObjectWi
969969
function addValueInjectionLoader(
970970
newConfig: WebpackConfigObjectWithModuleRules,
971971
userNextConfig: NextConfigObject,
972-
userSentryOptions: UserSentryOptions,
972+
userSentryOptions: SentryBuildtimeOptions,
973973
buildContext: BuildContext,
974974
sentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions>,
975975
): void {

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,47 @@ import type {
55
NextConfigFunction,
66
NextConfigObject,
77
SentryWebpackPluginOptions,
8-
UserSentryOptions,
8+
SentryBuildtimeOptions,
99
} from './types';
1010
import { constructWebpackConfigFunction } from './webpack';
1111

1212
let showedExportModeTunnelWarning = false;
1313

1414
/**
15-
* Modifies the passed in Next.js configuration
15+
* Modifies the passed in Next.js configuration with automatic build-time instrumentation and source map upload.
1616
*
17-
* @param nextConfig The existing config to be exported prior to adding Sentry
18-
* @param sentryWebpackPluginOptions Configuration for SentryWebpackPlugin
19-
* @param sentrySDKOptions Optional additional options to add as alternative to `sentry` property of config
17+
* @param nextConfig A Next.js configuration object, as usually exported in `next.config.js` or `next.config.mjs`.
18+
* @param sentryWebpackPluginOptions Options to configure the automatically included Sentry Webpack Plugin for source maps and release management in Sentry.
19+
* @param sentryBuildtimeOptions Additional options to configure instrumentation and
2020
* @returns The modified config to be exported
2121
*/
2222
export function withSentryConfig(
2323
nextConfig: NextConfig = {},
2424
sentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
25-
sentrySDKOptions: UserSentryOptions = {},
25+
sentryBuildtimeOptions: SentryBuildtimeOptions = {},
2626
): NextConfigFunction | NextConfigObject {
2727
if (typeof nextConfig === 'function') {
2828
return function (this: unknown, ...webpackConfigFunctionArgs: unknown[]): ReturnType<NextConfigFunction> {
2929
const maybePromiseNextConfig: ReturnType<typeof nextConfig> = nextConfig.apply(this, webpackConfigFunctionArgs);
3030

3131
if (isThenable(maybePromiseNextConfig)) {
3232
return maybePromiseNextConfig.then(promiseResultNextConfig => {
33-
return getFinalConfigObject(promiseResultNextConfig, sentrySDKOptions, sentryWebpackPluginOptions);
33+
return getFinalConfigObject(promiseResultNextConfig, sentryBuildtimeOptions, sentryWebpackPluginOptions);
3434
});
3535
}
3636

37-
return getFinalConfigObject(maybePromiseNextConfig, sentrySDKOptions, sentryWebpackPluginOptions);
37+
return getFinalConfigObject(maybePromiseNextConfig, sentryBuildtimeOptions, sentryWebpackPluginOptions);
3838
};
3939
} else {
40-
return getFinalConfigObject(nextConfig, sentrySDKOptions, sentryWebpackPluginOptions);
40+
return getFinalConfigObject(nextConfig, sentryBuildtimeOptions, sentryWebpackPluginOptions);
4141
}
4242
}
4343

4444
// Modify the materialized object form of the user's next config by deleting the `sentry` property and wrapping the
4545
// `webpack` property
4646
function getFinalConfigObject(
4747
incomingUserNextConfigObject: NextConfigObject,
48-
userSentryOptions: UserSentryOptions,
48+
userSentryOptions: SentryBuildtimeOptions,
4949
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions>,
5050
): NextConfigObject {
5151
if ('sentry' in incomingUserNextConfigObject) {

packages/nextjs/test/config/fixtures.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
EntryPropertyFunction,
44
ExportedNextConfig,
55
NextConfigObject,
6-
NextConfigObjectWithSentry,
76
WebpackConfigObject,
87
} from '../../src/config/types';
98

@@ -26,7 +25,7 @@ export const userNextConfig: NextConfigObject = {
2625
};
2726

2827
/** Mocks of the arguments passed to `withSentryConfig` */
29-
export const exportedNextConfig = userNextConfig as NextConfigObjectWithSentry;
28+
export const exportedNextConfig = userNextConfig;
3029
export const userSentryWebpackPluginConfig = { org: 'squirrelChasers', project: 'simulator' };
3130
process.env.SENTRY_AUTH_TOKEN = 'dogsarebadatkeepingsecrets';
3231
process.env.SENTRY_RELEASE = 'doGsaREgReaT';

packages/nextjs/test/config/testUtils.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
EntryPropertyFunction,
77
ExportedNextConfig,
88
NextConfigObject,
9+
SentryBuildtimeOptions,
910
SentryWebpackPluginOptions,
1011
WebpackConfigObject,
1112
WebpackConfigObjectWithModuleRules,
@@ -28,8 +29,9 @@ export function materializeFinalNextConfig(
2829
exportedNextConfig: ExportedNextConfig,
2930
userSentryWebpackPluginConfig?: Partial<SentryWebpackPluginOptions>,
3031
runtimePhase?: string,
32+
sentryBuildTimeOptions?: SentryBuildtimeOptions,
3133
): NextConfigObject {
32-
const sentrifiedConfig = withSentryConfig(exportedNextConfig, userSentryWebpackPluginConfig);
34+
const sentrifiedConfig = withSentryConfig(exportedNextConfig, userSentryWebpackPluginConfig, sentryBuildTimeOptions);
3335
let finalConfigValues = sentrifiedConfig;
3436

3537
if (typeof sentrifiedConfig === 'function') {
@@ -59,6 +61,7 @@ export async function materializeFinalWebpackConfig(options: {
5961
userSentryWebpackPluginConfig?: Partial<SentryWebpackPluginOptions>;
6062
incomingWebpackConfig: WebpackConfigObject;
6163
incomingWebpackBuildContext: BuildContext;
64+
sentryBuildTimeOptions?: SentryBuildtimeOptions;
6265
}): Promise<WebpackConfigObjectWithModuleRules> {
6366
const { exportedNextConfig, userSentryWebpackPluginConfig, incomingWebpackConfig, incomingWebpackBuildContext } =
6467
options;
@@ -69,15 +72,11 @@ export async function materializeFinalWebpackConfig(options: {
6972
? await exportedNextConfig('phase-production-build', defaultsObject)
7073
: exportedNextConfig;
7174

72-
// extract the `sentry` property as we do in `withSentryConfig`
73-
const { sentry: sentryConfig } = materializedUserNextConfig;
74-
delete materializedUserNextConfig.sentry;
75-
7675
// get the webpack config function we'd normally pass back to next
7776
const webpackConfigFunction = constructWebpackConfigFunction(
7877
materializedUserNextConfig,
7978
userSentryWebpackPluginConfig,
80-
sentryConfig,
79+
options.sentryBuildTimeOptions ?? {},
8180
);
8281

8382
// call it to get concrete values for comparison

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,38 @@ describe('constructWebpackConfigFunction()', () => {
4848
});
4949

5050
it("doesn't set devtool if webpack plugin is disabled", () => {
51-
const finalNextConfig = materializeFinalNextConfig({
52-
...exportedNextConfig,
53-
webpack: () =>
54-
({
55-
...serverWebpackConfig,
56-
devtool: 'something-besides-source-map',
57-
}) as any,
58-
sentry: { disableServerWebpackPlugin: true },
59-
});
51+
const finalNextConfig = materializeFinalNextConfig(
52+
{
53+
...exportedNextConfig,
54+
webpack: () =>
55+
({
56+
...serverWebpackConfig,
57+
devtool: 'something-besides-source-map',
58+
}) as any,
59+
},
60+
undefined,
61+
undefined,
62+
{ disableServerWebpackPlugin: true },
63+
);
64+
6065
const finalWebpackConfig = finalNextConfig.webpack?.(serverWebpackConfig, serverBuildContext);
6166

6267
expect(finalWebpackConfig?.devtool).not.toEqual('source-map');
6368
});
6469

6570
it('allows for the use of `hidden-source-map` as `devtool` value for client-side builds', async () => {
66-
const exportedNextConfigHiddenSourceMaps = {
67-
...exportedNextConfig,
68-
sentry: { ...exportedNextConfig.sentry, hideSourceMaps: true },
69-
};
70-
7171
const finalClientWebpackConfig = await materializeFinalWebpackConfig({
72-
exportedNextConfig: exportedNextConfigHiddenSourceMaps,
72+
exportedNextConfig: exportedNextConfig,
7373
incomingWebpackConfig: clientWebpackConfig,
7474
incomingWebpackBuildContext: clientBuildContext,
75+
sentryBuildTimeOptions: { hideSourceMaps: true },
7576
});
7677

7778
const finalServerWebpackConfig = await materializeFinalWebpackConfig({
78-
exportedNextConfig: exportedNextConfigHiddenSourceMaps,
79+
exportedNextConfig: exportedNextConfig,
7980
incomingWebpackConfig: serverWebpackConfig,
8081
incomingWebpackBuildContext: serverBuildContext,
82+
sentryBuildTimeOptions: { hideSourceMaps: true },
8183
});
8284

8385
expect(finalClientWebpackConfig.devtool).toEqual('hidden-source-map');

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as os from 'os';
33
import * as path from 'path';
44
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
55

6-
import type { BuildContext, ExportedNextConfig } from '../../../src/config/types';
6+
import type { BuildContext, ExportedNextConfig, SentryBuildtimeOptions } from '../../../src/config/types';
77
import { getUserConfigFile, getWebpackPluginOptions } from '../../../src/config/webpack';
88
import {
99
clientBuildContext,
@@ -83,13 +83,14 @@ describe('Sentry webpack plugin config', () => {
8383
});
8484

8585
it('has the correct value when building client bundles using `widenClientFileUpload` option', async () => {
86-
const exportedNextConfigWithWidening = { ...exportedNextConfig, sentry: { widenClientFileUpload: true } };
86+
const exportedNextConfigWithWidening = { ...exportedNextConfig };
8787
const buildContext = getBuildContext('client', exportedNextConfigWithWidening);
8888

8989
const finalWebpackConfig = await materializeFinalWebpackConfig({
9090
exportedNextConfig: exportedNextConfigWithWidening,
9191
incomingWebpackConfig: clientWebpackConfig,
9292
incomingWebpackBuildContext: buildContext,
93+
sentryBuildTimeOptions: { widenClientFileUpload: true },
9394
});
9495

9596
const sentryWebpackPluginInstance = findWebpackPlugin(
@@ -180,11 +181,12 @@ describe('Sentry webpack plugin config', () => {
180181
});
181182

182183
it('has the correct value when building client bundles using `widenClientFileUpload` option', async () => {
183-
const exportedNextConfigWithWidening = { ...exportedNextConfig, sentry: { widenClientFileUpload: true } };
184+
const exportedNextConfigWithWidening = { ...exportedNextConfig };
184185
const finalWebpackConfig = await materializeFinalWebpackConfig({
185186
exportedNextConfig: exportedNextConfigWithWidening,
186187
incomingWebpackConfig: clientWebpackConfig,
187188
incomingWebpackBuildContext: getBuildContext('client', exportedNextConfigWithWidening),
189+
sentryBuildTimeOptions: { widenClientFileUpload: true },
188190
});
189191

190192
const sentryWebpackPluginInstance = findWebpackPlugin(
@@ -219,8 +221,8 @@ describe('Sentry webpack plugin config', () => {
219221
'obeys `disableClientWebpackPlugin = true`',
220222
{
221223
...exportedNextConfig,
222-
sentry: { disableClientWebpackPlugin: true },
223224
},
225+
{ disableClientWebpackPlugin: true },
224226
{},
225227
true,
226228
false,
@@ -230,8 +232,8 @@ describe('Sentry webpack plugin config', () => {
230232
'obeys `disableServerWebpackPlugin = true`',
231233
{
232234
...exportedNextConfig,
233-
sentry: { disableServerWebpackPlugin: true },
234235
},
236+
{ disableServerWebpackPlugin: true },
235237
{},
236238
false,
237239
true,
@@ -241,6 +243,7 @@ describe('Sentry webpack plugin config', () => {
241243
async (
242244
_testName: string,
243245
exportedNextConfig: ExportedNextConfig,
246+
buildTimeOptions: SentryBuildtimeOptions,
244247
extraEnvValues: Record<string, string>,
245248
shouldFindServerPlugin: boolean,
246249
shouldFindClientPlugin: boolean,
@@ -254,13 +257,15 @@ describe('Sentry webpack plugin config', () => {
254257
userSentryWebpackPluginConfig,
255258
incomingWebpackConfig: serverWebpackConfig,
256259
incomingWebpackBuildContext: serverBuildContext,
260+
sentryBuildTimeOptions: buildTimeOptions,
257261
});
258262

259263
const clientFinalWebpackConfig = await materializeFinalWebpackConfig({
260264
exportedNextConfig: { ...exportedNextConfig },
261265
userSentryWebpackPluginConfig,
262266
incomingWebpackConfig: clientWebpackConfig,
263267
incomingWebpackBuildContext: clientBuildContext,
268+
sentryBuildTimeOptions: buildTimeOptions,
264269
});
265270

266271
const genericSentryWebpackPluginInstance = expect.any(SentryWebpackPlugin);

packages/nextjs/test/config/withSentryConfig.test.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,4 @@ describe('withSentryConfig', () => {
4949

5050
expect(exportedNextConfigFunction).toHaveBeenCalledWith(defaultRuntimePhase, defaultsObject);
5151
});
52-
53-
it('removes `sentry` property', () => {
54-
// It's unclear why we need this cast -
55-
const finalConfig = materializeFinalNextConfig({ ...exportedNextConfig, sentry: {} });
56-
// const finalConfig = materializeFinalNextConfig({ ...exportedNextConfig, sentry: {} } as ExportedNextConfig);
57-
58-
// We have to check using `in` because TS knows it shouldn't be there and throws a type error if we try to access it
59-
// directly
60-
expect('sentry' in finalConfig).toBe(false);
61-
});
6252
});

0 commit comments

Comments
 (0)