Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update tests
  • Loading branch information
chargome committed Sep 3, 2025
commit a6d8e5f906a00c7db49965d93a824389fb909073
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('handleRunAfterProductionCompile', () => {
expect(mockCreateSentryBuildPluginManager).not.toHaveBeenCalled();
});

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

const debugOptions = {
Expand All @@ -139,7 +139,7 @@ describe('handleRunAfterProductionCompile', () => {
debugOptions,
);

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

consoleSpy.mockRestore();
});
Expand Down
115 changes: 68 additions & 47 deletions packages/nextjs/test/config/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { parseSemver } from '@sentry/core';
import * as fs from 'fs';
import { afterEach, describe, expect, it, vi } from 'vitest';
import * as util from '../../src/config/util';

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

describe('util', () => {
describe('supportsProductionCompileHook', () => {
Expand All @@ -14,134 +13,156 @@ describe('util', () => {

describe('supported versions', () => {
it('returns true for Next.js 15.4.1', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
expect(util.supportsProductionCompileHook()).toBe(true);
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1' }));

const result = util.supportsProductionCompileHook();
expect(result).toBe(true);
});

it('returns true for Next.js 15.4.2', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.2');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 2 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.2' }));

expect(util.supportsProductionCompileHook()).toBe(true);
});

it('returns true for Next.js 15.5.0', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.5.0');
(parseSemver as any).mockReturnValue({ major: 15, minor: 5, patch: 0 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.5.0' }));

expect(util.supportsProductionCompileHook()).toBe(true);
});

it('returns true for Next.js 16.0.0', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('16.0.0');
(parseSemver as any).mockReturnValue({ major: 16, minor: 0, patch: 0 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '16.0.0' }));

expect(util.supportsProductionCompileHook()).toBe(true);
});

it('returns true for Next.js 17.0.0', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('17.0.0');
(parseSemver as any).mockReturnValue({ major: 17, minor: 0, patch: 0 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '17.0.0' }));

expect(util.supportsProductionCompileHook()).toBe(true);
});

it('returns true for supported canary versions', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1-canary.42');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1-canary.42' }));

expect(util.supportsProductionCompileHook()).toBe(true);
});

it('returns true for supported rc versions', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1-rc.1');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1-rc.1' }));

expect(util.supportsProductionCompileHook()).toBe(true);
});
});

describe('unsupported versions', () => {
it('returns false for Next.js 15.4.0', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 0 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.0' }));

expect(util.supportsProductionCompileHook()).toBe(false);
});

it('returns false for Next.js 15.3.9', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.9');
(parseSemver as any).mockReturnValue({ major: 15, minor: 3, patch: 9 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.3.9' }));

expect(util.supportsProductionCompileHook()).toBe(false);
});

it('returns false for Next.js 15.0.0', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.0.0');
(parseSemver as any).mockReturnValue({ major: 15, minor: 0, patch: 0 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.0.0' }));

expect(util.supportsProductionCompileHook()).toBe(false);
});

it('returns false for Next.js 14.2.0', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('14.2.0');
(parseSemver as any).mockReturnValue({ major: 14, minor: 2, patch: 0 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '14.2.0' }));

expect(util.supportsProductionCompileHook()).toBe(false);
});

it('returns false for unsupported canary versions', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0-canary.42');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 0 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.0-canary.42' }));

expect(util.supportsProductionCompileHook()).toBe(false);
});
});

describe('edge cases', () => {
it('returns false for invalid version strings', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('invalid.version');
(parseSemver as any).mockReturnValue({ major: undefined, minor: undefined, patch: undefined });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: 'invalid.version' }));

expect(util.supportsProductionCompileHook()).toBe(false);
});

it('handles versions with build metadata', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1+build.123');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1+build.123' }));

expect(util.supportsProductionCompileHook()).toBe(true);
});

it('handles versions with pre-release identifiers', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1-alpha.1');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1-alpha.1' }));

expect(util.supportsProductionCompileHook()).toBe(true);
});

it('returns false for versions missing patch number', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: undefined });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4' }));

expect(util.supportsProductionCompileHook()).toBe(false);
});

it('returns false for versions missing minor number', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15');
(parseSemver as any).mockReturnValue({ major: 15, minor: undefined, patch: undefined });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15' }));

expect(util.supportsProductionCompileHook()).toBe(false);
});
});

describe('version boundary tests', () => {
it('returns false for 15.4.0 (just below threshold)', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 0 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.0' }));

expect(util.supportsProductionCompileHook()).toBe(false);
});

it('returns true for 15.4.1 (exact threshold)', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 1 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.1' }));

expect(util.supportsProductionCompileHook()).toBe(true);
});

it('returns true for 15.4.2 (just above threshold)', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.2');
(parseSemver as any).mockReturnValue({ major: 15, minor: 4, patch: 2 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.4.2' }));

expect(util.supportsProductionCompileHook()).toBe(true);
});

it('returns false for 15.3.999 (high patch but wrong minor)', () => {
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.999');
(parseSemver as any).mockReturnValue({ major: 15, minor: 3, patch: 999 });
const mockReadFileSync = fs.readFileSync as any;
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '15.3.999' }));

expect(util.supportsProductionCompileHook()).toBe(false);
});
});
Expand Down
Loading