Skip to content

Commit d47ee55

Browse files
committed
fix existing tests
1 parent c37804c commit d47ee55

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

packages/nextjs/test/config.test.ts

+34-14
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@ import {
77
SentryWebpackPluginOptions,
88
WebpackConfigObject,
99
} from '../src/config/types';
10-
import {
11-
CLIENT_SDK_CONFIG_FILE,
12-
constructWebpackConfigFunction,
13-
SentryWebpackPlugin,
14-
SERVER_SDK_CONFIG_FILE,
15-
} from '../src/config/webpack';
10+
import { constructWebpackConfigFunction, SentryWebpackPlugin } from '../src/config/webpack';
11+
12+
const SERVER_SDK_CONFIG_FILE = 'sentry.server.config.js';
13+
const CLIENT_SDK_CONFIG_FILE = 'sentry.client.config.js';
14+
15+
// We use `fs.existsSync()` in `getUserConfigFile()`. When we're not testing `getUserConfigFile()` specifically, all we
16+
// need is for it to give us any valid answer, so make it always find what it's looking for. Since this is a core node
17+
// built-in, though, which jest itself uses, otherwise let it do the normal thing. Storing the real version of the
18+
// function also lets us restore the original when we do want to test `getUserConfigFile()`.
19+
const realExistsSync = jest.requireActual('fs').existsSync;
20+
const mockExistsSync = (path: fs.PathLike) => {
21+
if ((path as string).endsWith(SERVER_SDK_CONFIG_FILE) || (path as string).endsWith(CLIENT_SDK_CONFIG_FILE)) {
22+
return true;
23+
}
24+
25+
return realExistsSync(path);
26+
};
27+
const exitsSync = jest.spyOn(fs, 'existsSync').mockImplementation(mockExistsSync);
1628

1729
/** Mocks of the arguments passed to `withSentryConfig` */
1830
const userNextConfig = {
@@ -63,8 +75,13 @@ const clientWebpackConfig = {
6375
target: 'web',
6476
context: '/Users/Maisey/projects/squirrelChasingSimulator',
6577
};
66-
const serverBuildContext = { isServer: true, dev: false, buildId: 'doGsaREgReaT' };
67-
const clientBuildContext = { isServer: false, dev: false, buildId: 'doGsaREgReaT' };
78+
const baseBuildContext = {
79+
dev: false,
80+
buildId: 'doGsaREgReaT',
81+
dir: '/Users/Maisey/projects/squirrelChasingSimulator',
82+
};
83+
const serverBuildContext = { isServer: true, ...baseBuildContext };
84+
const clientBuildContext = { isServer: false, ...baseBuildContext };
6885

6986
/**
7087
* Derive the final values of all next config options, by first applying `withSentryConfig` and then, if it returns a
@@ -223,6 +240,9 @@ describe('webpack config', () => {
223240
});
224241

225242
describe('webpack `entry` property config', () => {
243+
const serverConfigFilePath = `./${SERVER_SDK_CONFIG_FILE}`;
244+
const clientConfigFilePath = `./${CLIENT_SDK_CONFIG_FILE}`;
245+
226246
it('handles various entrypoint shapes', async () => {
227247
const finalWebpackConfig = await materializeFinalWebpackConfig({
228248
userNextConfig,
@@ -234,23 +254,23 @@ describe('webpack config', () => {
234254
expect.objectContaining({
235255
// original entry point value is a string
236256
// (was 'private-next-pages/api/dogs/[name].js')
237-
'pages/api/dogs/[name]': [SERVER_SDK_CONFIG_FILE, 'private-next-pages/api/dogs/[name].js'],
257+
'pages/api/dogs/[name]': [serverConfigFilePath, 'private-next-pages/api/dogs/[name].js'],
238258

239259
// original entry point value is a string array
240260
// (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'],
261+
'pages/_app': [serverConfigFilePath, './node_modules/smellOVision/index.js', 'private-next-pages/_app.js'],
242262

243263
// original entry point value is an object containing a string `import` value
244264
// (`import` was 'private-next-pages/api/simulator/dogStats/[name].js')
245265
'pages/api/simulator/dogStats/[name]': {
246-
import: [SERVER_SDK_CONFIG_FILE, 'private-next-pages/api/simulator/dogStats/[name].js'],
266+
import: [serverConfigFilePath, 'private-next-pages/api/simulator/dogStats/[name].js'],
247267
},
248268

249269
// original entry point value is an object containing a string array `import` value
250270
// (`import` was ['./node_modules/dogPoints/converter.js', 'private-next-pages/api/simulator/leaderboard.js'])
251271
'pages/api/simulator/leaderboard': {
252272
import: [
253-
SERVER_SDK_CONFIG_FILE,
273+
serverConfigFilePath,
254274
'./node_modules/dogPoints/converter.js',
255275
'private-next-pages/api/simulator/leaderboard.js',
256276
],
@@ -259,7 +279,7 @@ describe('webpack config', () => {
259279
// original entry point value is an object containg properties besides `import`
260280
// (`dependOn` remains untouched)
261281
'pages/api/tricks/[trickName]': {
262-
import: [SERVER_SDK_CONFIG_FILE, 'private-next-pages/api/tricks/[trickName].js'],
282+
import: [serverConfigFilePath, 'private-next-pages/api/tricks/[trickName].js'],
263283
dependOn: 'treats',
264284
},
265285
}),
@@ -278,7 +298,7 @@ describe('webpack config', () => {
278298
// no injected file
279299
main: './src/index.ts',
280300
// was 'next-client-pages-loader?page=%2F_app'
281-
'pages/_app': [CLIENT_SDK_CONFIG_FILE, 'next-client-pages-loader?page=%2F_app'],
301+
'pages/_app': [clientConfigFilePath, 'next-client-pages-loader?page=%2F_app'],
282302
}),
283303
);
284304
});

0 commit comments

Comments
 (0)