Skip to content

Commit acef63c

Browse files
committed
fix tests
1 parent f38e2c2 commit acef63c

File tree

2 files changed

+63
-27
lines changed

2 files changed

+63
-27
lines changed

packages/nextjs/test/config/loaders.test.ts

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,74 @@ import {
1010
} from './fixtures';
1111
import { materializeFinalWebpackConfig } from './testUtils';
1212

13+
type MatcherResult = { pass: boolean; message: () => string };
14+
15+
expect.extend({
16+
stringEndingWith(received: string, expectedEnding: string): MatcherResult {
17+
const failsTest = !received.endsWith(expectedEnding);
18+
const generateErrorMessage = () =>
19+
failsTest
20+
? // Regular error message for match failing
21+
`expected string ending with '${expectedEnding}', but got '${received}'`
22+
: // Error message for the match passing if someone has called it with `expect.not`
23+
`expected string not ending with '${expectedEnding}', but got '${received}'`;
24+
25+
return {
26+
pass: !failsTest,
27+
message: generateErrorMessage,
28+
};
29+
},
30+
});
31+
32+
declare global {
33+
// eslint-disable-next-line @typescript-eslint/no-namespace
34+
namespace jest {
35+
interface Expect {
36+
stringEndingWith: (expectedEnding: string) => MatcherResult;
37+
}
38+
}
39+
}
40+
1341
describe('webpack loaders', () => {
14-
it('adds loader to server config', async () => {
15-
const finalWebpackConfig = await materializeFinalWebpackConfig({
16-
exportedNextConfig,
17-
incomingWebpackConfig: serverWebpackConfig,
18-
incomingWebpackBuildContext: serverBuildContext,
42+
describe('server loaders', () => {
43+
it('adds server `RewriteFrames` loader to server config', async () => {
44+
const finalWebpackConfig = await materializeFinalWebpackConfig({
45+
exportedNextConfig,
46+
incomingWebpackConfig: serverWebpackConfig,
47+
incomingWebpackBuildContext: serverBuildContext,
48+
});
49+
50+
expect(finalWebpackConfig.module.rules).toContainEqual({
51+
test: /sentry\.server\.config\.(jsx?|tsx?)/,
52+
use: [
53+
{
54+
loader: expect.stringEndingWith('prefixLoader.js'),
55+
options: expect.objectContaining({ templatePrefix: 'serverRewriteFrames' }),
56+
},
57+
],
58+
});
1959
});
60+
});
61+
62+
describe('client loaders', () => {
63+
it("doesn't add `RewriteFrames` loader to client config", async () => {
64+
const finalWebpackConfig = await materializeFinalWebpackConfig({
65+
exportedNextConfig,
66+
incomingWebpackConfig: clientWebpackConfig,
67+
incomingWebpackBuildContext: clientBuildContext,
68+
});
2069

21-
expect(finalWebpackConfig.module!.rules).toEqual(
22-
expect.arrayContaining([
23-
{
24-
test: expect.any(RegExp),
70+
expect(finalWebpackConfig.module.rules).not.toContainEqual(
71+
expect.objectContaining({
2572
use: [
2673
{
27-
loader: expect.any(String),
28-
// Having no criteria for what the object contains is better than using `expect.any(Object)`, because that
29-
// could be anything
30-
options: expect.objectContaining({}),
74+
loader: expect.stringEndingWith('prefixLoader.js'),
75+
options: expect.objectContaining({ templatePrefix: expect.stringContaining('RewriteFrames') }),
3176
},
3277
],
33-
},
34-
]),
35-
);
36-
});
37-
38-
it("doesn't add loader to client config", async () => {
39-
const finalWebpackConfig = await materializeFinalWebpackConfig({
40-
exportedNextConfig,
41-
incomingWebpackConfig: clientWebpackConfig,
42-
incomingWebpackBuildContext: clientBuildContext,
78+
}),
79+
);
4380
});
44-
45-
expect(finalWebpackConfig.module).toBeUndefined();
4681
});
4782
});
4883

packages/nextjs/test/config/testUtils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
NextConfigObject,
88
SentryWebpackPluginOptions,
99
WebpackConfigObject,
10+
WebpackConfigObjectWithModuleRules,
1011
} from '../../src/config/types';
1112
import { constructWebpackConfigFunction, SentryWebpackPlugin } from '../../src/config/webpack';
1213
import { withSentryConfig } from '../../src/config/withSentryConfig';
@@ -57,7 +58,7 @@ export async function materializeFinalWebpackConfig(options: {
5758
userSentryWebpackPluginConfig?: Partial<SentryWebpackPluginOptions>;
5859
incomingWebpackConfig: WebpackConfigObject;
5960
incomingWebpackBuildContext: BuildContext;
60-
}): Promise<WebpackConfigObject> {
61+
}): Promise<WebpackConfigObjectWithModuleRules> {
6162
const { exportedNextConfig, userSentryWebpackPluginConfig, incomingWebpackConfig, incomingWebpackBuildContext } =
6263
options;
6364

@@ -83,7 +84,7 @@ export async function materializeFinalWebpackConfig(options: {
8384
const webpackEntryProperty = finalWebpackConfigValue.entry as EntryPropertyFunction;
8485
finalWebpackConfigValue.entry = await webpackEntryProperty();
8586

86-
return finalWebpackConfigValue;
87+
return finalWebpackConfigValue as WebpackConfigObjectWithModuleRules;
8788
}
8889

8990
// helper function to make sure we're checking the correct plugin's data

0 commit comments

Comments
 (0)