Skip to content

Commit 5fbe134

Browse files
committed
fix tests
1 parent 9e9f5f3 commit 5fbe134

File tree

2 files changed

+66
-40
lines changed

2 files changed

+66
-40
lines changed

packages/nextjs/src/config/webpack.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export { SentryWebpackPlugin };
1919
// TODO: merge default SentryWebpackPlugin include with their SentryWebpackPlugin include
2020
// TODO: drop merged keys from override check? `includeDefaults` option?
2121

22-
const CLIENT_SDK_CONFIG_FILE = './sentry.client.config.js';
23-
const SERVER_SDK_CONFIG_FILE = './sentry.server.config.js';
22+
export const CLIENT_SDK_CONFIG_FILE = './sentry.client.config.js';
23+
export const SERVER_SDK_CONFIG_FILE = './sentry.server.config.js';
2424

2525
const defaultSentryWebpackPluginOptions = dropUndefinedKeys({
2626
url: process.env.SENTRY_URL,

packages/nextjs/test/config.test.ts

+64-38
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,14 @@ import {
77
SentryWebpackPluginOptions,
88
WebpackConfigObject,
99
} from '../src/config/types';
10-
import { SENTRY_SERVER_CONFIG_FILE, SERVER_SDK_INIT_PATH } from '../src/config/utils';
11-
import { constructWebpackConfigFunction, SentryWebpackPlugin } from '../src/config/webpack';
12-
13-
// mock `storeServerConfigFileLocation` in order to make it a no-op when necessary
14-
jest.mock('../src/config/utils', () => {
15-
const original = jest.requireActual('../src/config/utils');
16-
return {
17-
...original,
18-
// nuke this so it won't try to look for our dummy paths
19-
storeServerConfigFileLocation: jest.fn(),
20-
};
21-
});
10+
import {
11+
constructWebpackConfigFunction,
12+
SentryWebpackPlugin,
13+
CLIENT_SDK_CONFIG_FILE,
14+
SERVER_SDK_CONFIG_FILE,
15+
} from '../src/config/webpack';
2216

23-
/** mocks of the arguments passed to `withSentryConfig` */
17+
/** Mocks of the arguments passed to `withSentryConfig` */
2418
const userNextConfig = {
2519
publicRuntimeConfig: { location: 'dogpark', activities: ['fetch', 'chasing', 'digging'] },
2620
webpack: (config: WebpackConfigObject, _options: BuildContext) => ({
@@ -35,19 +29,36 @@ const userNextConfig = {
3529
};
3630
const userSentryWebpackPluginConfig = { org: 'squirrelChasers', project: 'simulator', include: './thirdPartyMaps' };
3731

38-
/** mocks of the arguments passed to the result of `withSentryConfig` (when it's a function) */
39-
const runtimePhase = 'puppy-phase-chew-everything-in-sight';
32+
/** Mocks of the arguments passed to the result of `withSentryConfig` (when it's a function). */
33+
const runtimePhase = 'ball-fetching';
4034
const defaultNextConfig = { nappingHoursPerDay: 20, oversizeFeet: true, shouldChaseTail: true };
4135

4236
/** mocks of the arguments passed to `nextConfig.webpack` */
4337
const serverWebpackConfig = {
44-
entry: () => Promise.resolve({ 'pages/api/dogs/[name]': 'private-next-pages/api/dogs/[name].js' }),
38+
entry: () =>
39+
Promise.resolve({
40+
'pages/api/dogs/[name]': 'private-next-pages/api/dogs/[name].js',
41+
'pages/_app': ['./node_modules/smellOVision/index.js', 'private-next-pages/_app.js'],
42+
'pages/api/simulator/dogStats/[name]': { import: 'private-next-pages/api/simulator/dogStats/[name].js' },
43+
'pages/api/simulator/leaderboard': {
44+
import: ['./node_modules/dogPoints/converter.js', 'private-next-pages/api/simulator/leaderboard.js'],
45+
},
46+
'pages/api/tricks/[trickName]': {
47+
import: 'private-next-pages/api/tricks/[trickName].js',
48+
dependOn: 'treats',
49+
},
50+
treats: './node_modules/dogTreats/treatProvider.js',
51+
}),
4552
output: { filename: '[name].js', path: '/Users/Maisey/projects/squirrelChasingSimulator/.next' },
4653
target: 'node',
4754
context: '/Users/Maisey/projects/squirrelChasingSimulator',
4855
};
4956
const clientWebpackConfig = {
50-
entry: () => Promise.resolve({ main: './src/index.ts' }),
57+
entry: () =>
58+
Promise.resolve({
59+
main: './src/index.ts',
60+
'pages/_app': 'next-client-pages-loader?page=%2F_app',
61+
}),
5162
output: { filename: 'static/chunks/[name].js', path: '/Users/Maisey/projects/squirrelChasingSimulator/.next' },
5263
target: 'web',
5364
context: '/Users/Maisey/projects/squirrelChasingSimulator',
@@ -212,7 +223,7 @@ describe('webpack config', () => {
212223
});
213224

214225
describe('webpack `entry` property config', () => {
215-
it('injects correct code when building server bundle', async () => {
226+
it('handles various entrypoint shapes', async () => {
216227
const finalWebpackConfig = await materializeFinalWebpackConfig({
217228
userNextConfig,
218229
incomingWebpackConfig: serverWebpackConfig,
@@ -221,38 +232,53 @@ describe('webpack config', () => {
221232

222233
expect(finalWebpackConfig.entry).toEqual(
223234
expect.objectContaining({
224-
[SERVER_SDK_INIT_PATH.slice(0, -3)]: SENTRY_SERVER_CONFIG_FILE,
235+
// original entry point value is a string
236+
// (was 'private-next-pages/api/dogs/[name].js')
237+
'pages/api/dogs/[name]': [SERVER_SDK_CONFIG_FILE, 'private-next-pages/api/dogs/[name].js'],
238+
239+
// original entry point value is a string array
240+
// (was ['./node_modules/smellOVision/index.js', 'private-next-pages/_app.js'])
241+
'pages/_app': [SERVER_SDK_CONFIG_FILE, './node_modules/smellOVision/index.js', 'private-next-pages/_app.js'],
242+
243+
// original entry point value is an object containg string `import` value
244+
// (`import` was 'private-next-pages/api/simulator/dogStats/[name].js')
245+
'pages/api/simulator/dogStats/[name]': {
246+
import: [SERVER_SDK_CONFIG_FILE, 'private-next-pages/api/simulator/dogStats/[name].js'],
247+
},
248+
249+
// original entry point value is an object containg string array `import` value
250+
// (`import` was ['./node_modules/dogPoints/converter.js', 'private-next-pages/api/simulator/leaderboard.js'])
251+
'pages/api/simulator/leaderboard': {
252+
import: [
253+
SERVER_SDK_CONFIG_FILE,
254+
'./node_modules/dogPoints/converter.js',
255+
'private-next-pages/api/simulator/leaderboard.js',
256+
],
257+
},
258+
259+
// original entry point value is an object containg properties besides `import`
260+
// (`dependOn` remains untouched)
261+
'pages/api/tricks/[trickName]': {
262+
import: [SERVER_SDK_CONFIG_FILE, 'private-next-pages/api/tricks/[trickName].js'],
263+
dependOn: 'treats',
264+
},
225265
}),
226266
);
227267
});
228268

229-
it('injects correct code when building client bundle', async () => {
269+
it('does not inject into non-_app, non-API routes', async () => {
230270
const finalWebpackConfig = await materializeFinalWebpackConfig({
231271
userNextConfig,
232272
incomingWebpackConfig: clientWebpackConfig,
233273
incomingWebpackBuildContext: clientBuildContext,
234274
});
235275

236-
expect(finalWebpackConfig.entry).toEqual(
237-
expect.objectContaining({ main: ['./src/index.ts', './sentry.client.config.js'] }),
238-
);
239-
});
240-
241-
// see https://github.com/getsentry/sentry-javascript/pull/3696#issuecomment-863363803
242-
it('handles non-empty `main.js` entry point', async () => {
243-
const finalWebpackConfig = await materializeFinalWebpackConfig({
244-
userNextConfig,
245-
incomingWebpackConfig: {
246-
...clientWebpackConfig,
247-
entry: () => Promise.resolve({ main: './src/index.ts', 'main.js': ['sitLieDownRollOver.config.js'] }),
248-
},
249-
incomingWebpackBuildContext: clientBuildContext,
250-
});
251-
252276
expect(finalWebpackConfig.entry).toEqual(
253277
expect.objectContaining({
254-
main: ['sitLieDownRollOver.config.js', './src/index.ts', './sentry.client.config.js'],
255-
'main.js': [],
278+
// no injected file
279+
main: './src/index.ts',
280+
// was 'next-client-pages-loader?page=%2F_app'
281+
'pages/_app': [CLIENT_SDK_CONFIG_FILE, 'next-client-pages-loader?page=%2F_app'],
256282
}),
257283
);
258284
});

0 commit comments

Comments
 (0)