Skip to content

Commit a9af4aa

Browse files
committed
add and fix tests
1 parent bf47570 commit a9af4aa

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

packages/nextjs/test/buildProcess/tests/nft.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ it('excludes build-time SDK dependencies from nft files', () => {
1111

1212
// These are all of the files which control the way we modify the user's app build by changing the webpack config.
1313
// They get mistakenly included by the nft plugin because we export `withSentryConfig` from `index.server.ts`. Because
14-
// the wrappers are referenced from the code we inject at build time, they need to stay.
14+
// the wrappers are referenced from the code we inject at build time, they need to stay. `withSentryConfig.ts` itself
15+
// also needs to stay because nextjs loads `next.config.js` at runtime
1516
const sentryConfigDirEntries = dogNFTjson.files.filter(entry =>
1617
entry.includes('node_modules/@sentry/nextjs/build/cjs/config'),
1718
);
18-
const withSentryConfigEntries = sentryConfigDirEntries.filter(entry => !entry.includes('config/wrappers'));
1919
const sentryWrapperEntries = sentryConfigDirEntries.filter(entry => entry.includes('config/wrappers'));
20+
const excludedSentryConfigEntries = sentryConfigDirEntries.filter(
21+
entry => !sentryWrapperEntries.includes(entry) && !entry.includes('withSentryConfig'),
22+
);
2023

2124
// Sucrase and rollup are dependencies of one of the webpack loaders we add to the config - also not needed at runtime.
2225
const sucraseEntries = dogNFTjson.files.filter(entry => entry.includes('node_modules/sucrase'));
@@ -25,7 +28,7 @@ it('excludes build-time SDK dependencies from nft files', () => {
2528
);
2629

2730
// None of the build-time dependencies should be listed
28-
expect(withSentryConfigEntries.length).toEqual(0);
31+
expect(excludedSentryConfigEntries.length).toEqual(0);
2932
expect(sucraseEntries.length).toEqual(0);
3033
expect(rollupEntries.length).toEqual(0);
3134

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import * as isBuildModule from '../../src/utils/isBuild';
12
import { defaultsObject, exportedNextConfig, runtimePhase, userNextConfig } from './fixtures';
23
import { materializeFinalNextConfig } from './testUtils';
34

5+
const isBuildSpy = jest.spyOn(isBuildModule, 'isBuild').mockReturnValue(true);
6+
47
describe('withSentryConfig', () => {
58
it('includes expected properties', () => {
69
const finalConfig = materializeFinalNextConfig(exportedNextConfig);
@@ -59,4 +62,37 @@ describe('withSentryConfig', () => {
5962
// directly
6063
expect('sentry' in finalConfig).toBe(false);
6164
});
65+
66+
describe('conditional use of `constructWebpackConfigFunction`', () => {
67+
// Note: In these tests, it would be nice to be able to spy on `constructWebpackConfigFunction` to see whether or
68+
// not it's called, but that sets up a catch-22: If you import or require the module to spy on the function, it gets
69+
// cached and the `require` call we care about (inside of `withSentryConfig`) doesn't actually run the module code.
70+
// Alternatively, if we call `jest.resetModules()` after setting up the spy, then the module code *is* run a second
71+
// time, but the spy belongs to the first instance of the module and therefore never registers a call. Thus we have
72+
// to test whether or not the file is required instead.
73+
74+
it('imports from `webpack.ts` if `isBuild` returns true', () => {
75+
jest.isolateModules(() => {
76+
// In case this is still set from elsewhere, reset it
77+
delete process.env.SENTRY_WEBPACK_MODULE_LOADED;
78+
79+
materializeFinalNextConfig(exportedNextConfig);
80+
81+
expect(process.env.SENTRY_WEBPACK_MODULE_LOADED).toEqual('true');
82+
});
83+
});
84+
85+
it("doesn't import from `webpack.ts` if `isBuild` returns false", () => {
86+
jest.isolateModules(() => {
87+
isBuildSpy.mockReturnValueOnce(false);
88+
89+
// In case this is still set from elsewhere, reset it
90+
delete process.env.SENTRY_WEBPACK_MODULE_LOADED;
91+
92+
materializeFinalNextConfig(exportedNextConfig);
93+
94+
expect(process.env.SENTRY_WEBPACK_MODULE_LOADED).toBeUndefined();
95+
});
96+
});
97+
});
6298
});

0 commit comments

Comments
 (0)