/
withSentryConfig.test.ts
98 lines (75 loc) · 3.89 KB
/
withSentryConfig.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import * as isBuildModule from '../../src/utils/isBuild';
import { defaultsObject, exportedNextConfig, runtimePhase, userNextConfig } from './fixtures';
import { materializeFinalNextConfig } from './testUtils';
const isBuildSpy = jest.spyOn(isBuildModule, 'isBuild').mockReturnValue(true);
describe('withSentryConfig', () => {
it('includes expected properties', () => {
const finalConfig = materializeFinalNextConfig(exportedNextConfig);
expect(finalConfig).toEqual(
expect.objectContaining({
webpack: expect.any(Function), // `webpack` is tested specifically elsewhere
}),
);
});
it('preserves unrelated next config options', () => {
const finalConfig = materializeFinalNextConfig(exportedNextConfig);
expect(finalConfig.publicRuntimeConfig).toEqual(userNextConfig.publicRuntimeConfig);
});
it("works when user's overall config is an object", () => {
const finalConfig = materializeFinalNextConfig(exportedNextConfig);
expect(finalConfig).toEqual(
expect.objectContaining({
...userNextConfig,
webpack: expect.any(Function), // `webpack` is tested specifically elsewhere
}),
);
});
it("works when user's overall config is a function", () => {
const exportedNextConfigFunction = () => userNextConfig;
const finalConfig = materializeFinalNextConfig(exportedNextConfigFunction);
expect(finalConfig).toEqual(
expect.objectContaining({
...exportedNextConfigFunction(),
webpack: expect.any(Function), // `webpack` is tested specifically elsewhere
}),
);
});
it('correctly passes `phase` and `defaultConfig` through to functional `userNextConfig`', () => {
const exportedNextConfigFunction = jest.fn().mockReturnValue(userNextConfig);
materializeFinalNextConfig(exportedNextConfigFunction);
expect(exportedNextConfigFunction).toHaveBeenCalledWith(runtimePhase, defaultsObject);
});
it('removes `sentry` property', () => {
// It's unclear why we need this cast -
const finalConfig = materializeFinalNextConfig({ ...exportedNextConfig, sentry: {} });
// const finalConfig = materializeFinalNextConfig({ ...exportedNextConfig, sentry: {} } as ExportedNextConfig);
// 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
// directly
expect('sentry' in finalConfig).toBe(false);
});
describe('conditional use of `constructWebpackConfigFunction`', () => {
// Note: In these tests, it would be nice to be able to spy on `constructWebpackConfigFunction` to see whether or
// 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
// cached and the `require` call we care about (inside of `withSentryConfig`) doesn't actually run the module code.
// Alternatively, if we call `jest.resetModules()` after setting up the spy, then the module code *is* run a second
// time, but the spy belongs to the first instance of the module and therefore never registers a call. Thus we have
// to test whether or not the file is required instead.
it('imports from `webpack.ts` if `isBuild` returns true', () => {
jest.isolateModules(() => {
// In case this is still set from elsewhere, reset it
delete (global as any)._sentryWebpackModuleLoaded;
materializeFinalNextConfig(exportedNextConfig);
expect((global as any)._sentryWebpackModuleLoaded).toBe(true);
});
});
it("doesn't import from `webpack.ts` if `isBuild` returns false", () => {
jest.isolateModules(() => {
isBuildSpy.mockReturnValueOnce(false);
// In case this is still set from elsewhere, reset it
delete (global as any)._sentryWebpackModuleLoaded;
materializeFinalNextConfig(exportedNextConfig);
expect((global as any)._sentryWebpackModuleLoaded).toBeUndefined();
});
});
});
});