Skip to content

Commit a6d8e5f

Browse files
committed
update tests
1 parent e418dd2 commit a6d8e5f

File tree

2 files changed

+70
-49
lines changed

2 files changed

+70
-49
lines changed

packages/nextjs/test/config/handleRunAfterProductionCompile.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('handleRunAfterProductionCompile', () => {
122122
expect(mockCreateSentryBuildPluginManager).not.toHaveBeenCalled();
123123
});
124124

125-
it('logs debug message even for webpack builds when debug is enabled', async () => {
125+
it('does not log debug message for webpack builds when debug is enabled', async () => {
126126
const consoleSpy = vi.spyOn(console, 'debug').mockImplementation(() => {});
127127

128128
const debugOptions = {
@@ -139,7 +139,7 @@ describe('handleRunAfterProductionCompile', () => {
139139
debugOptions,
140140
);
141141

142-
expect(consoleSpy).toHaveBeenCalledWith('[@sentry/nextjs] Running runAfterProductionCompile logic.');
142+
expect(consoleSpy).not.toHaveBeenCalledWith('[@sentry/nextjs] Running runAfterProductionCompile logic.');
143143

144144
consoleSpy.mockRestore();
145145
});

packages/nextjs/test/config/util.test.ts

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { parseSemver } from '@sentry/core';
1+
import * as fs from 'fs';
22
import { afterEach, describe, expect, it, vi } from 'vitest';
33
import * as util from '../../src/config/util';
44

5-
vi.mock('@sentry/core', () => ({
6-
parseSemver: vi.fn(),
7-
}));
5+
// Mock fs to control what getNextjsVersion reads
6+
vi.mock('fs');
87

98
describe('util', () => {
109
describe('supportsProductionCompileHook', () => {
@@ -14,134 +13,156 @@ describe('util', () => {
1413

1514
describe('supported versions', () => {
1615
it('returns true for Next.js 15.4.1', () => {
17-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
18-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
19-
expect(util.supportsProductionCompileHook()).toBe(true);
16+
const mockReadFileSync = fs.readFileSync as any;
17+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1' }));
18+
19+
const result = util.supportsProductionCompileHook();
20+
expect(result).toBe(true);
2021
});
2122

2223
it('returns true for Next.js 15.4.2', () => {
23-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.2');
24-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 2 });
24+
const mockReadFileSync = fs.readFileSync as any;
25+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.2' }));
26+
2527
expect(util.supportsProductionCompileHook()).toBe(true);
2628
});
2729

2830
it('returns true for Next.js 15.5.0', () => {
29-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.5.0');
30-
(parseSemver as any).mockReturnValue({ major: 15, minor: 5, patch: 0 });
31+
const mockReadFileSync = fs.readFileSync as any;
32+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.5.0' }));
33+
3134
expect(util.supportsProductionCompileHook()).toBe(true);
3235
});
3336

3437
it('returns true for Next.js 16.0.0', () => {
35-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('16.0.0');
36-
(parseSemver as any).mockReturnValue({ major: 16, minor: 0, patch: 0 });
38+
const mockReadFileSync = fs.readFileSync as any;
39+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '16.0.0' }));
40+
3741
expect(util.supportsProductionCompileHook()).toBe(true);
3842
});
3943

4044
it('returns true for Next.js 17.0.0', () => {
41-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('17.0.0');
42-
(parseSemver as any).mockReturnValue({ major: 17, minor: 0, patch: 0 });
45+
const mockReadFileSync = fs.readFileSync as any;
46+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '17.0.0' }));
47+
4348
expect(util.supportsProductionCompileHook()).toBe(true);
4449
});
4550

4651
it('returns true for supported canary versions', () => {
47-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1-canary.42');
48-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
52+
const mockReadFileSync = fs.readFileSync as any;
53+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1-canary.42' }));
54+
4955
expect(util.supportsProductionCompileHook()).toBe(true);
5056
});
5157

5258
it('returns true for supported rc versions', () => {
53-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1-rc.1');
54-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
59+
const mockReadFileSync = fs.readFileSync as any;
60+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1-rc.1' }));
61+
5562
expect(util.supportsProductionCompileHook()).toBe(true);
5663
});
5764
});
5865

5966
describe('unsupported versions', () => {
6067
it('returns false for Next.js 15.4.0', () => {
61-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0');
62-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 0 });
68+
const mockReadFileSync = fs.readFileSync as any;
69+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.0' }));
70+
6371
expect(util.supportsProductionCompileHook()).toBe(false);
6472
});
6573

6674
it('returns false for Next.js 15.3.9', () => {
67-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.9');
68-
(parseSemver as any).mockReturnValue({ major: 15, minor: 3, patch: 9 });
75+
const mockReadFileSync = fs.readFileSync as any;
76+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.3.9' }));
77+
6978
expect(util.supportsProductionCompileHook()).toBe(false);
7079
});
7180

7281
it('returns false for Next.js 15.0.0', () => {
73-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.0.0');
74-
(parseSemver as any).mockReturnValue({ major: 15, minor: 0, patch: 0 });
82+
const mockReadFileSync = fs.readFileSync as any;
83+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.0.0' }));
84+
7585
expect(util.supportsProductionCompileHook()).toBe(false);
7686
});
7787

7888
it('returns false for Next.js 14.2.0', () => {
79-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('14.2.0');
80-
(parseSemver as any).mockReturnValue({ major: 14, minor: 2, patch: 0 });
89+
const mockReadFileSync = fs.readFileSync as any;
90+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '14.2.0' }));
91+
8192
expect(util.supportsProductionCompileHook()).toBe(false);
8293
});
8394

8495
it('returns false for unsupported canary versions', () => {
85-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0-canary.42');
86-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 0 });
96+
const mockReadFileSync = fs.readFileSync as any;
97+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.0-canary.42' }));
98+
8799
expect(util.supportsProductionCompileHook()).toBe(false);
88100
});
89101
});
90102

91103
describe('edge cases', () => {
92104
it('returns false for invalid version strings', () => {
93-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('invalid.version');
94-
(parseSemver as any).mockReturnValue({ major: undefined, minor: undefined, patch: undefined });
105+
const mockReadFileSync = fs.readFileSync as any;
106+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: 'invalid.version' }));
107+
95108
expect(util.supportsProductionCompileHook()).toBe(false);
96109
});
97110

98111
it('handles versions with build metadata', () => {
99-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1+build.123');
100-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
112+
const mockReadFileSync = fs.readFileSync as any;
113+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1+build.123' }));
114+
101115
expect(util.supportsProductionCompileHook()).toBe(true);
102116
});
103117

104118
it('handles versions with pre-release identifiers', () => {
105-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1-alpha.1');
106-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
119+
const mockReadFileSync = fs.readFileSync as any;
120+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1-alpha.1' }));
121+
107122
expect(util.supportsProductionCompileHook()).toBe(true);
108123
});
109124

110125
it('returns false for versions missing patch number', () => {
111-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4');
112-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: undefined });
126+
const mockReadFileSync = fs.readFileSync as any;
127+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4' }));
128+
113129
expect(util.supportsProductionCompileHook()).toBe(false);
114130
});
115131

116132
it('returns false for versions missing minor number', () => {
117-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15');
118-
(parseSemver as any).mockReturnValue({ major: 15, minor: undefined, patch: undefined });
133+
const mockReadFileSync = fs.readFileSync as any;
134+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15' }));
135+
119136
expect(util.supportsProductionCompileHook()).toBe(false);
120137
});
121138
});
122139

123140
describe('version boundary tests', () => {
124141
it('returns false for 15.4.0 (just below threshold)', () => {
125-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0');
126-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 0 });
142+
const mockReadFileSync = fs.readFileSync as any;
143+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.0' }));
144+
127145
expect(util.supportsProductionCompileHook()).toBe(false);
128146
});
129147

130148
it('returns true for 15.4.1 (exact threshold)', () => {
131-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
132-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
149+
const mockReadFileSync = fs.readFileSync as any;
150+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1' }));
151+
133152
expect(util.supportsProductionCompileHook()).toBe(true);
134153
});
135154

136155
it('returns true for 15.4.2 (just above threshold)', () => {
137-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.2');
138-
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 2 });
156+
const mockReadFileSync = fs.readFileSync as any;
157+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.2' }));
158+
139159
expect(util.supportsProductionCompileHook()).toBe(true);
140160
});
141161

142162
it('returns false for 15.3.999 (high patch but wrong minor)', () => {
143-
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.999');
144-
(parseSemver as any).mockReturnValue({ major: 15, minor: 3, patch: 999 });
163+
const mockReadFileSync = fs.readFileSync as any;
164+
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.3.999' }));
165+
145166
expect(util.supportsProductionCompileHook()).toBe(false);
146167
});
147168
});

0 commit comments

Comments
 (0)