Skip to content

Commit ffc8aa0

Browse files
author
Luca Forstner
committed
Fix tests
1 parent f36d418 commit ffc8aa0

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable complexity */
22
/* eslint-disable max-lines */
33
import { getSentryRelease } from '@sentry/node';
4-
import { arrayify, dropUndefinedKeys, escapeStringForRegex, logger } from '@sentry/utils';
4+
import { arrayify, dropUndefinedKeys, escapeStringForRegex, logger, stringMatchesSomePattern } from '@sentry/utils';
55
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
66
import * as chalk from 'chalk';
77
import * as fs from 'fs';
@@ -316,7 +316,7 @@ async function addSentryToEntryProperty(
316316

317317
// inject into all entry points which might contain user's code
318318
for (const entryPointName in newEntryProperty) {
319-
if (shouldAddSentryToEntryPoint(entryPointName, runtime)) {
319+
if (shouldAddSentryToEntryPoint(entryPointName, runtime, userSentryOptions.excludeServerRoutes ?? [])) {
320320
addFilesToExistingEntryPoint(newEntryProperty, entryPointName, filesToInject);
321321
} else {
322322
if (
@@ -448,9 +448,20 @@ function checkWebpackPluginOverrides(
448448
* @param excludeServerRoutes A list of excluded serverside entrypoints provided by the user
449449
* @returns `true` if sentry code should be injected, and `false` otherwise
450450
*/
451-
function shouldAddSentryToEntryPoint(entryPointName: string, runtime: 'node' | 'browser' | 'edge'): boolean {
451+
function shouldAddSentryToEntryPoint(
452+
entryPointName: string,
453+
runtime: 'node' | 'browser' | 'edge',
454+
excludeServerRoutes: Array<string | RegExp>,
455+
): boolean {
452456
// On the server side, by default we inject the `Sentry.init()` code into every page (with a few exceptions).
453457
if (runtime === 'node') {
458+
// User-specified pages to skip. (Note: For ease of use, `excludeServerRoutes` is specified in terms of routes,
459+
// which don't have the `pages` prefix.)
460+
const entryPointRoute = entryPointName.replace(/^pages/, '');
461+
if (stringMatchesSomePattern(entryPointRoute, excludeServerRoutes, true)) {
462+
return false;
463+
}
464+
454465
// This expression will implicitly include `pages/_app` which is called for all serverside routes and pages
455466
// regardless whether or not the user has a`_app` file.
456467
return entryPointName.startsWith('pages/');
@@ -460,6 +471,13 @@ function shouldAddSentryToEntryPoint(entryPointName: string, runtime: 'node' | '
460471
entryPointName === 'main-app' // entrypoint for `/app` pages
461472
);
462473
} else {
474+
// User-specified pages to skip. (Note: For ease of use, `excludeServerRoutes` is specified in terms of routes,
475+
// which don't have the `pages` prefix.)
476+
const entryPointRoute = entryPointName.replace(/^pages/, '');
477+
if (stringMatchesSomePattern(entryPointRoute, excludeServerRoutes, true)) {
478+
return false;
479+
}
480+
463481
return true;
464482
}
465483
}

packages/nextjs/test/config/testUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
12
import type { WebpackPluginInstance } from 'webpack';
23

34
import type {
@@ -9,7 +10,6 @@ import type {
910
WebpackConfigObject,
1011
WebpackConfigObjectWithModuleRules,
1112
} from '../../src/config/types';
12-
import type { SentryWebpackPlugin } from '../../src/config/webpack';
1313
import { constructWebpackConfigFunction } from '../../src/config/webpack';
1414
import { withSentryConfig } from '../../src/config/withSentryConfig';
1515
import { defaultRuntimePhase, defaultsObject } from './fixtures';

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// mock helper functions not tested directly in this file
22
import '../mocks';
33

4-
import { SentryWebpackPlugin } from '../../../src/config/webpack';
4+
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
5+
56
import {
67
CLIENT_SDK_CONFIG_FILE,
78
clientBuildContext,
@@ -138,7 +139,7 @@ describe('constructWebpackConfigFunction()', () => {
138139
);
139140
});
140141

141-
it('injects user config file into `_app` in client bundle but not in server bundle', async () => {
142+
it('injects user config file into `_app` in server bundle but not in client bundle', async () => {
142143
const finalServerWebpackConfig = await materializeFinalWebpackConfig({
143144
exportedNextConfig,
144145
incomingWebpackConfig: serverWebpackConfig,
@@ -152,12 +153,12 @@ describe('constructWebpackConfigFunction()', () => {
152153

153154
expect(finalServerWebpackConfig.entry).toEqual(
154155
expect.objectContaining({
155-
'pages/_app': expect.not.arrayContaining([serverConfigFilePath]),
156+
'pages/_app': expect.arrayContaining([serverConfigFilePath]),
156157
}),
157158
);
158159
expect(finalClientWebpackConfig.entry).toEqual(
159160
expect.objectContaining({
160-
'pages/_app': expect.arrayContaining([clientConfigFilePath]),
161+
'pages/_app': expect.not.arrayContaining([clientConfigFilePath]),
161162
}),
162163
);
163164
});
@@ -232,9 +233,9 @@ describe('constructWebpackConfigFunction()', () => {
232233
});
233234

234235
expect(finalWebpackConfig.entry).toEqual({
235-
main: './src/index.ts',
236+
main: ['./sentry.client.config.js', './src/index.ts'],
236237
// only _app has config file injected
237-
'pages/_app': [clientConfigFilePath, 'next-client-pages-loader?page=%2F_app'],
238+
'pages/_app': 'next-client-pages-loader?page=%2F_app',
238239
'pages/_error': 'next-client-pages-loader?page=%2F_error',
239240
'pages/sniffTour': ['./node_modules/smellOVision/index.js', 'private-next-pages/sniffTour.js'],
240241
'pages/simulator/leaderboard': {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
12
import * as fs from 'fs';
23
import * as os from 'os';
34
import * as path from 'path';
45

56
import type { BuildContext, ExportedNextConfig } from '../../../src/config/types';
6-
import { getUserConfigFile, getWebpackPluginOptions, SentryWebpackPlugin } from '../../../src/config/webpack';
7+
import { getUserConfigFile, getWebpackPluginOptions } from '../../../src/config/webpack';
78
import {
89
clientBuildContext,
910
clientWebpackConfig,
@@ -36,7 +37,7 @@ describe('Sentry webpack plugin config', () => {
3637
authToken: 'dogsarebadatkeepingsecrets', // picked up from env
3738
stripPrefix: ['webpack://_N_E/'], // default
3839
urlPrefix: '~/_next', // default
39-
entries: expect.any(Function), // default, tested separately elsewhere
40+
entries: [],
4041
release: 'doGsaREgReaT', // picked up from env
4142
dryRun: false, // based on buildContext.dev being false
4243
}),
@@ -78,6 +79,7 @@ describe('Sentry webpack plugin config', () => {
7879

7980
expect(sentryWebpackPluginInstance.options.include).toEqual([
8081
{ paths: [`${clientBuildContext.dir}/.next/static/chunks/pages`], urlPrefix: '~/_next/static/chunks/pages' },
82+
{ paths: [`${clientBuildContext.dir}/.next/static/chunks/app`], urlPrefix: '~/_next/static/chunks/app' },
8183
]);
8284
});
8385

@@ -141,6 +143,7 @@ describe('Sentry webpack plugin config', () => {
141143

142144
expect(sentryWebpackPluginInstance.options.include).toEqual([
143145
{ paths: [`${serverBuildContextWebpack4.dir}/.next/server/pages/`], urlPrefix: '~/_next/server/pages' },
146+
{ paths: [`${serverBuildContextWebpack4.dir}/.next/server/app/`], urlPrefix: '~/_next/server/app' },
144147
]);
145148
});
146149

@@ -158,6 +161,7 @@ describe('Sentry webpack plugin config', () => {
158161

159162
expect(sentryWebpackPluginInstance.options.include).toEqual([
160163
{ paths: [`${serverBuildContext.dir}/.next/server/pages/`], urlPrefix: '~/_next/server/pages' },
164+
{ paths: [`${serverBuildContext.dir}/.next/server/app/`], urlPrefix: '~/_next/server/app' },
161165
{ paths: [`${serverBuildContext.dir}/.next/server/chunks/`], urlPrefix: '~/_next/server/chunks' },
162166
]);
163167
});

0 commit comments

Comments
 (0)