refactor(web): migrate to Vitest and esm (#29974)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: yyh <yuanyouhuilyz@gmail.com>
This commit is contained in:
authored and GitHub committed 2025-12-22 16:35:22 +08:00
1 parent 42f7ecda12
commit eabdc5f0eb
268 files changed
+5455 -6307

No files matched your search

@@ -1,3 +1,4 @@
import type { Mock } from 'vitest'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import React from 'react'
import EmbeddingProcess from './index'
@@ -12,24 +13,24 @@ import { IndexingType } from '@/app/components/datasets/create/step-two'
// ==========================================
// Mock next/navigation
const mockPush = jest.fn()
jest.mock('next/navigation', () => ({
const mockPush = vi.fn()
vi.mock('next/navigation', () => ({
useRouter: () => ({
push: mockPush,
}),
}))
// Mock next/link
jest.mock('next/link', () => {
return function MockLink({ children, href, ...props }: { children: React.ReactNode; href: string }) {
vi.mock('next/link', () => ({
default: function MockLink({ children, href, ...props }: { children: React.ReactNode; href: string }) {
return <a href={href} {...props}>{children}</a>
}
})
},
}))
// Mock provider context
let mockEnableBilling = false
let mockPlanType: Plan = Plan.sandbox
jest.mock('@/context/provider-context', () => ({
vi.mock('@/context/provider-context', () => ({
useProviderContext: () => ({
enableBilling: mockEnableBilling,
plan: { type: mockPlanType },
@@ -37,9 +38,9 @@ jest.mock('@/context/provider-context', () => ({
}))
// Mock useIndexingStatusBatch hook
let mockFetchIndexingStatus: jest.Mock
let mockFetchIndexingStatus: Mock
let mockIndexingStatusData: IndexingStatusResponse[] = []
jest.mock('@/service/knowledge/use-dataset', () => ({
vi.mock('@/service/knowledge/use-dataset', () => ({
useIndexingStatusBatch: () => ({
mutateAsync: mockFetchIndexingStatus,
}),
@@ -52,13 +53,13 @@ jest.mock('@/service/knowledge/use-dataset', () => ({
}))
// Mock useInvalidDocumentList hook
const mockInvalidDocumentList = jest.fn()
jest.mock('@/service/knowledge/use-document', () => ({
const mockInvalidDocumentList = vi.fn()
vi.mock('@/service/knowledge/use-document', () => ({
useInvalidDocumentList: () => mockInvalidDocumentList,
}))
// Mock useDatasetApiAccessUrl hook
jest.mock('@/hooks/use-api-access-url', () => ({
vi.mock('@/hooks/use-api-access-url', () => ({
useDatasetApiAccessUrl: () => 'https://docs.dify.ai/api-reference/datasets',
}))
@@ -126,8 +127,8 @@ const createDefaultProps = (overrides: Partial<{
describe('EmbeddingProcess', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.useFakeTimers()
vi.clearAllMocks()
vi.useFakeTimers({ shouldAdvanceTime: true })
// Reset deterministic ID counter for reproducible tests
documentIdCounter = 0
@@ -138,7 +139,7 @@ describe('EmbeddingProcess', () => {
mockIndexingStatusData = []
// Setup default mock for fetchIndexingStatus
mockFetchIndexingStatus = jest.fn().mockImplementation((_, options) => {
mockFetchIndexingStatus = vi.fn().mockImplementation((_, options) => {
options?.onSuccess?.({ data: mockIndexingStatusData })
options?.onSettled?.()
return Promise.resolve({ data: mockIndexingStatusData })
@@ -146,7 +147,7 @@ describe('EmbeddingProcess', () => {
})
afterEach(() => {
jest.useRealTimers()
vi.useRealTimers()
})
// ==========================================
@@ -549,7 +550,7 @@ describe('EmbeddingProcess', () => {
const afterInitialCount = mockFetchIndexingStatus.mock.calls.length
// Advance timer for next poll
jest.advanceTimersByTime(2500)
vi.advanceTimersByTime(2500)
// Assert - should poll again
await waitFor(() => {
@@ -576,7 +577,7 @@ describe('EmbeddingProcess', () => {
const callCountAfterComplete = mockFetchIndexingStatus.mock.calls.length
// Advance timer - polling should have stopped
jest.advanceTimersByTime(5000)
vi.advanceTimersByTime(5000)
// Assert - call count should not increase significantly after completion
// Note: Due to React Strict Mode, there might be double renders
@@ -602,7 +603,7 @@ describe('EmbeddingProcess', () => {
const callCountAfterError = mockFetchIndexingStatus.mock.calls.length
// Advance timer
jest.advanceTimersByTime(5000)
vi.advanceTimersByTime(5000)
// Assert - should not poll significantly more after error state
expect(mockFetchIndexingStatus.mock.calls.length).toBeLessThanOrEqual(callCountAfterError + 1)
@@ -627,7 +628,7 @@ describe('EmbeddingProcess', () => {
const callCountAfterPaused = mockFetchIndexingStatus.mock.calls.length
// Advance timer
jest.advanceTimersByTime(5000)
vi.advanceTimersByTime(5000)
// Assert - should not poll significantly more after paused state
expect(mockFetchIndexingStatus.mock.calls.length).toBeLessThanOrEqual(callCountAfterPaused + 1)
@@ -655,7 +656,7 @@ describe('EmbeddingProcess', () => {
unmount()
// Advance timer
jest.advanceTimersByTime(5000)
vi.advanceTimersByTime(5000)
// Assert - should not poll after unmount
expect(mockFetchIndexingStatus.mock.calls.length).toBe(callCountBeforeUnmount)
@@ -921,7 +922,7 @@ describe('EmbeddingProcess', () => {
const props = createDefaultProps({ documents: [] })
// Suppress console errors for expected error
const consoleError = jest.spyOn(console, 'error').mockImplementation(Function.prototype as () => void)
const consoleError = vi.spyOn(console, 'error').mockImplementation(Function.prototype as () => void)
// Act & Assert - explicitly assert the error behavior
expect(() => {
@@ -10,7 +10,7 @@ import { IndexingType } from '@/app/components/datasets/create/step-two'
// ==========================================
// Mock next/image (using img element for simplicity in tests)
jest.mock('next/image', () => ({
vi.mock('next/image', () => ({
__esModule: true,
default: function MockImage({ src, alt, className }: { src: string; alt: string; className?: string }) {
// eslint-disable-next-line @next/next/no-img-element
@@ -19,7 +19,7 @@ jest.mock('next/image', () => ({
}))
// Mock FieldInfo component
jest.mock('@/app/components/datasets/documents/detail/metadata', () => ({
vi.mock('@/app/components/datasets/documents/detail/metadata', () => ({
FieldInfo: ({ label, displayedValue, valueIcon }: { label: string; displayedValue: string; valueIcon?: React.ReactNode }) => (
<div data-testid="field-info" data-label={label}>
<span data-testid="field-label">{label}</span>
@@ -30,7 +30,7 @@ jest.mock('@/app/components/datasets/documents/detail/metadata', () => ({
}))
// Mock icons - provides simple string paths for testing instead of Next.js static import objects
jest.mock('@/app/components/datasets/create/icons', () => ({
vi.mock('@/app/components/datasets/create/icons', () => ({
indexMethodIcon: {
economical: '/icons/economical.svg',
high_quality: '/icons/high_quality.svg',
@@ -77,7 +77,7 @@ const createMockProcessRule = (overrides: Partial<ProcessRuleResponse> = {}): Pr
describe('RuleDetail', () => {
beforeEach(() => {
jest.clearAllMocks()
vi.clearAllMocks()
})
// ==========================================