Skip to content

test(react): Migrate to vitest #15492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 0 additions & 8 deletions packages/react/jest.config.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
"clean": "rimraf build coverage sentry-react-*.tgz",
"fix": "eslint . --format stylish --fix",
"lint": "eslint . --format stylish",
"test": "jest",
"test:watch": "jest --watch",
"test": "vitest run",
"test:watch": "vitest --watch",
"yalc:publish": "yalc publish --push --sig"
},
"volta": {
Expand Down
2 changes: 2 additions & 0 deletions packages/react/test/error.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, test } from 'vitest';

import { isAtLeastReact17 } from '../src/error';

describe('isAtLeastReact17', () => {
Expand Down
56 changes: 32 additions & 24 deletions packages/react/test/errorboundary.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @vitest-environment jsdom
*/
import { afterEach, describe, expect, it, vi } from 'vitest';

import { Scope, getClient, setCurrentClient } from '@sentry/browser';
import type { Client } from '@sentry/core';
import { fireEvent, render, screen } from '@testing-library/react';
Expand All @@ -7,15 +12,14 @@ import { useState } from 'react';
import type { ErrorBoundaryProps, FallbackRender } from '../src/errorboundary';
import { ErrorBoundary, UNKNOWN_COMPONENT, withErrorBoundary } from '../src/errorboundary';

const mockCaptureException = jest.fn();
const mockShowReportDialog = jest.fn();
const mockClientOn = jest.fn();
const mockCaptureException = vi.fn();
const mockShowReportDialog = vi.fn();
const mockClientOn = vi.fn();
const EVENT_ID = 'test-id-123';

jest.mock('@sentry/browser', () => {
const actual = jest.requireActual('@sentry/browser');
vi.mock('@sentry/browser', async requireActual => {
return {
...actual,
...(await requireActual()),
captureException: (...args: unknown[]) => {
mockCaptureException(...args);
return EVENT_ID;
Expand Down Expand Up @@ -92,7 +96,7 @@ describe('withErrorBoundary', () => {
});

describe('ErrorBoundary', () => {
jest.spyOn(console, 'error').mockImplementation();
vi.spyOn(console, 'error').mockImplementation(() => {});

afterEach(() => {
mockCaptureException.mockClear();
Expand Down Expand Up @@ -141,7 +145,7 @@ describe('ErrorBoundary', () => {
});

it('calls `onMount` when mounted', () => {
const mockOnMount = jest.fn();
const mockOnMount = vi.fn();
render(
<ErrorBoundary fallback={<h1>Error Component</h1>} onMount={mockOnMount}>
<h1>children</h1>
Expand All @@ -152,7 +156,7 @@ describe('ErrorBoundary', () => {
});

it('calls `onUnmount` when unmounted', () => {
const mockOnUnmount = jest.fn();
const mockOnUnmount = vi.fn();
const { unmount } = render(
<ErrorBoundary fallback={<h1>Error Component</h1>} onUnmount={mockOnUnmount}>
<h1>children</h1>
Expand Down Expand Up @@ -243,7 +247,7 @@ describe('ErrorBoundary', () => {

describe('error', () => {
it('calls `componentDidCatch() when an error occurs`', () => {
const mockOnError = jest.fn();
const mockOnError = vi.fn();
render(
<TestApp fallback={<p>You have hit an error</p>} onError={mockOnError}>
<h1>children</h1>
Expand All @@ -267,12 +271,14 @@ describe('ErrorBoundary', () => {
mechanism: { handled: true },
});

expect(mockOnError.mock.calls[0][0]).toEqual(mockCaptureException.mock.calls[0][0]);
expect(mockOnError.mock.calls[0]?.[0]).toEqual(mockCaptureException.mock.calls[0]?.[0]);

// Check if error.cause -> react component stack
const error = mockCaptureException.mock.calls[0][0];
const error = mockCaptureException.mock.calls[0]?.[0];
const cause = error.cause;
expect(cause.stack).toEqual(mockCaptureException.mock.calls[0][1]?.captureContext.contexts.react.componentStack);
expect(cause.stack).toEqual(
mockCaptureException.mock.calls[0]?.[1]?.captureContext.contexts.react.componentStack,
);
expect(cause.name).toContain('React ErrorBoundary');
expect(cause.message).toEqual(error.message);
});
Expand Down Expand Up @@ -326,12 +332,12 @@ describe('ErrorBoundary', () => {
});

// Check if error.cause -> react component stack
const error = mockCaptureException.mock.calls[0][0];
const error = mockCaptureException.mock.calls[0]?.[0];
expect(error.cause).not.toBeDefined();
});

it('handles when `error.cause` is nested', () => {
const mockOnError = jest.fn();
const mockOnError = vi.fn();

function CustomBam(): JSX.Element {
const firstError = new Error('bam');
Expand Down Expand Up @@ -364,19 +370,21 @@ describe('ErrorBoundary', () => {
mechanism: { handled: true },
});

expect(mockOnError.mock.calls[0][0]).toEqual(mockCaptureException.mock.calls[0][0]);
expect(mockOnError.mock.calls[0]?.[0]).toEqual(mockCaptureException.mock.calls[0]?.[0]);

const thirdError = mockCaptureException.mock.calls[0][0];
const thirdError = mockCaptureException.mock.calls[0]?.[0];
const secondError = thirdError.cause;
const firstError = secondError.cause;
const cause = firstError.cause;
expect(cause.stack).toEqual(mockCaptureException.mock.calls[0][1]?.captureContext.contexts.react.componentStack);
expect(cause.stack).toEqual(
mockCaptureException.mock.calls[0]?.[1]?.captureContext.contexts.react.componentStack,
);
expect(cause.name).toContain('React ErrorBoundary');
expect(cause.message).toEqual(thirdError.message);
});

it('handles when `error.cause` is recursive', () => {
const mockOnError = jest.fn();
const mockOnError = vi.fn();

function CustomBam(): JSX.Element {
const firstError = new Error('bam');
Expand Down Expand Up @@ -408,19 +416,19 @@ describe('ErrorBoundary', () => {
mechanism: { handled: true },
});

expect(mockOnError.mock.calls[0][0]).toEqual(mockCaptureException.mock.calls[0][0]);
expect(mockOnError.mock.calls[0]?.[0]).toEqual(mockCaptureException.mock.calls[0]?.[0]);

const error = mockCaptureException.mock.calls[0][0];
const error = mockCaptureException.mock.calls[0]?.[0];
const cause = error.cause;
// We need to make sure that recursive error.cause does not cause infinite loop
expect(cause.stack).not.toEqual(
mockCaptureException.mock.calls[0][1]?.captureContext.contexts.react.componentStack,
mockCaptureException.mock.calls[0]?.[1]?.captureContext.contexts.react.componentStack,
);
expect(cause.name).not.toContain('React ErrorBoundary');
});

it('calls `beforeCapture()` when an error occurs', () => {
const mockBeforeCapture = jest.fn();
const mockBeforeCapture = vi.fn();

const testBeforeCapture = (...args: any[]) => {
expect(mockCaptureException).toHaveBeenCalledTimes(0);
Expand Down Expand Up @@ -516,7 +524,7 @@ describe('ErrorBoundary', () => {
});

it('calls `onReset()` when reset', () => {
const mockOnReset = jest.fn();
const mockOnReset = vi.fn();
render(
<TestApp
onReset={mockOnReset}
Expand Down
14 changes: 9 additions & 5 deletions packages/react/test/profiler.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
/**
* @vitest-environment jsdom
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { SentrySpan } from '@sentry/core';
import type { StartSpanOptions } from '@sentry/core';
import { render } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
// biome-ignore lint/nursery/noUnusedImports: Need React import for JSX
import * as React from 'react';

import { REACT_MOUNT_OP, REACT_RENDER_OP, REACT_UPDATE_OP } from '../src/constants';
import { UNKNOWN_COMPONENT, useProfiler, withProfiler } from '../src/profiler';

const mockStartInactiveSpan = jest.fn((spanArgs: StartSpanOptions) => ({ ...spanArgs }));
const mockFinish = jest.fn();
const mockStartInactiveSpan = vi.fn((spanArgs: StartSpanOptions) => ({ ...spanArgs }));
const mockFinish = vi.fn();

class MockSpan extends SentrySpan {
public end(): void {
Expand All @@ -19,8 +23,8 @@ class MockSpan extends SentrySpan {

let activeSpan: Record<string, any>;

jest.mock('@sentry/browser', () => ({
...jest.requireActual('@sentry/browser'),
vi.mock('@sentry/browser', async requireActual => ({
...(await requireActual()),
getActiveSpan: () => activeSpan,
startInactiveSpan: (ctx: StartSpanOptions) => {
mockStartInactiveSpan(ctx);
Expand Down
24 changes: 14 additions & 10 deletions packages/react/test/reactrouter-descendant-routes.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @vitest-environment jsdom
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';

import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
Expand Down Expand Up @@ -28,19 +33,19 @@ import {
wrapUseRoutesV6,
} from '../src/reactrouterv6';

const mockStartBrowserTracingPageLoadSpan = jest.fn();
const mockStartBrowserTracingNavigationSpan = jest.fn();
const mockStartBrowserTracingPageLoadSpan = vi.fn();
const mockStartBrowserTracingNavigationSpan = vi.fn();

const mockRootSpan = {
updateName: jest.fn(),
setAttribute: jest.fn(),
updateName: vi.fn(),
setAttribute: vi.fn(),
getSpanJSON() {
return { op: 'pageload' };
},
};

jest.mock('@sentry/browser', () => {
const actual = jest.requireActual('@sentry/browser');
vi.mock('@sentry/browser', async requireActual => {
const actual = (await requireActual()) as any;
return {
...actual,
startBrowserTracingNavigationSpan: (...args: unknown[]) => {
Expand All @@ -54,10 +59,9 @@ jest.mock('@sentry/browser', () => {
};
});

jest.mock('@sentry/core', () => {
const actual = jest.requireActual('@sentry/core');
vi.mock('@sentry/core', async requireActual => {
return {
...actual,
...(await requireActual()),
getRootSpan: () => {
return mockRootSpan;
},
Expand All @@ -75,7 +79,7 @@ describe('React Router Descendant Routes', () => {
}

beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
getCurrentScope().setClient(undefined);
});

Expand Down
22 changes: 13 additions & 9 deletions packages/react/test/reactrouterv3.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @vitest-environment jsdom
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { BrowserClient } from '@sentry/browser';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
Expand All @@ -13,18 +18,18 @@ import * as React from 'react';
import { IndexRoute, Route, Router, createMemoryHistory, createRoutes, match } from 'react-router-3';
import { reactRouterV3BrowserTracingIntegration } from '../src/reactrouterv3';

const mockStartBrowserTracingPageLoadSpan = jest.fn();
const mockStartBrowserTracingNavigationSpan = jest.fn();
const mockStartBrowserTracingPageLoadSpan = vi.fn();
const mockStartBrowserTracingNavigationSpan = vi.fn();

const mockRootSpan = {
setAttribute: jest.fn(),
setAttribute: vi.fn(),
getSpanJSON() {
return { op: 'pageload' };
},
};

jest.mock('@sentry/browser', () => {
const actual = jest.requireActual('@sentry/browser');
vi.mock('@sentry/browser', async requireActual => {
const actual = (await requireActual()) as any;
return {
...actual,
startBrowserTracingNavigationSpan: (...args: unknown[]) => {
Expand All @@ -38,10 +43,9 @@ jest.mock('@sentry/browser', () => {
};
});

jest.mock('@sentry/core', () => {
const actual = jest.requireActual('@sentry/core');
vi.mock('@sentry/core', async requireActual => {
return {
...actual,
...(await requireActual()),
getRootSpan: () => {
return mockRootSpan;
},
Expand Down Expand Up @@ -78,7 +82,7 @@ describe('browserTracingReactRouterV3', () => {
}

beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
getCurrentScope().setClient(undefined);
});

Expand Down
24 changes: 14 additions & 10 deletions packages/react/test/reactrouterv4.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @vitest-environment jsdom
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';

import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
Expand All @@ -15,19 +20,19 @@ import { Route, Router, Switch, matchPath } from 'react-router-4';
import { BrowserClient, reactRouterV4BrowserTracingIntegration, withSentryRouting } from '../src';
import type { RouteConfig } from '../src/reactrouter';

const mockStartBrowserTracingPageLoadSpan = jest.fn();
const mockStartBrowserTracingNavigationSpan = jest.fn();
const mockStartBrowserTracingPageLoadSpan = vi.fn();
const mockStartBrowserTracingNavigationSpan = vi.fn();

const mockRootSpan = {
updateName: jest.fn(),
setAttribute: jest.fn(),
updateName: vi.fn(),
setAttribute: vi.fn(),
getSpanJSON() {
return { op: 'pageload' };
},
};

jest.mock('@sentry/browser', () => {
const actual = jest.requireActual('@sentry/browser');
vi.mock('@sentry/browser', async requireActual => {
const actual = (await requireActual()) as any;
return {
...actual,
startBrowserTracingNavigationSpan: (...args: unknown[]) => {
Expand All @@ -41,10 +46,9 @@ jest.mock('@sentry/browser', () => {
};
});

jest.mock('@sentry/core', () => {
const actual = jest.requireActual('@sentry/core');
vi.mock('@sentry/core', async requireActual => {
return {
...actual,
...(await requireActual()),
getRootSpan: () => {
return mockRootSpan;
},
Expand All @@ -62,7 +66,7 @@ describe('browserTracingReactRouterV4', () => {
}

beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
getCurrentScope().setClient(undefined);
});

Expand Down
Loading
Loading