committed by
GitHub
11 changed files with 9461 additions and 9852 deletions
@ -1,36 +0,0 @@ |
|||||
import { readFileSync } from 'node:fs'; |
|
||||
import { join } from 'node:path'; |
|
||||
import { configUmiAlias, createConfig } from '@umijs/max/test.js'; |
|
||||
|
|
||||
const readPkgVersion = (pkg: string) => |
|
||||
JSON.parse(readFileSync(join('node_modules', pkg, 'package.json'), 'utf-8')) |
|
||||
.version; |
|
||||
|
|
||||
export default async (): Promise<any> => { |
|
||||
const config = await configUmiAlias({ |
|
||||
...createConfig({ |
|
||||
target: 'browser', |
|
||||
}), |
|
||||
}); |
|
||||
return { |
|
||||
...config, |
|
||||
testPathIgnorePatterns: ['/node_modules/', '/.worktrees/'], |
|
||||
moduleNameMapper: { |
|
||||
'\\.md$': '<rootDir>/tests/__mocks__/raw.js', |
|
||||
...(config.moduleNameMapper || {}), |
|
||||
'^mermaid$': '<rootDir>/tests/__mocks__/mermaid.js', |
|
||||
}, |
|
||||
testEnvironmentOptions: { |
|
||||
...(config?.testEnvironmentOptions || {}), |
|
||||
url: 'http://localhost:8000', |
|
||||
}, |
|
||||
setupFiles: [...(config.setupFiles || []), './tests/setupTests.jsx'], |
|
||||
globals: { |
|
||||
...config.globals, |
|
||||
localStorage: null, |
|
||||
__APP_VERSION__: 'test', |
|
||||
__UMI_VERSION__: readPkgVersion('@umijs/max'), |
|
||||
__UTOO_VERSION__: readPkgVersion('@utoo/pack'), |
|
||||
}, |
|
||||
}; |
|
||||
}; |
|
||||
File diff suppressed because it is too large
@ -1 +0,0 @@ |
|||||
module.exports = {}; |
|
||||
@ -0,0 +1 @@ |
|||||
|
export default {}; |
||||
@ -1 +0,0 @@ |
|||||
module.exports = 'mock-md-content'; |
|
||||
@ -0,0 +1 @@ |
|||||
|
export default 'mock-md-content'; |
||||
@ -1,118 +0,0 @@ |
|||||
import { defaultConfig } from 'antd/lib/theme/internal'; |
|
||||
|
|
||||
defaultConfig.hashed = false; |
|
||||
|
|
||||
const store = {}; |
|
||||
|
|
||||
const localStorageMock = { |
|
||||
getItem: jest.fn((key) => store[key] ?? null), |
|
||||
setItem: jest.fn((key, value) => { |
|
||||
store[key] = String(value); |
|
||||
}), |
|
||||
removeItem: jest.fn((key) => { |
|
||||
delete store[key]; |
|
||||
}), |
|
||||
clear: jest.fn(() => { |
|
||||
Object.keys(store).forEach((key) => { |
|
||||
delete store[key]; |
|
||||
}); |
|
||||
}), |
|
||||
}; |
|
||||
|
|
||||
globalThis.localStorage = localStorageMock; |
|
||||
|
|
||||
Object.defineProperty(URL, 'createObjectURL', { |
|
||||
writable: true, |
|
||||
value: jest.fn(), |
|
||||
}); |
|
||||
|
|
||||
class MockWorker { |
|
||||
constructor(stringUrl) { |
|
||||
this.url = stringUrl; |
|
||||
this.onmessage = null; |
|
||||
} |
|
||||
|
|
||||
postMessage(msg) { |
|
||||
if (typeof this.onmessage === 'function') { |
|
||||
this.onmessage({ data: msg }); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
terminate() {} |
|
||||
addEventListener() {} |
|
||||
removeEventListener() {} |
|
||||
} |
|
||||
|
|
||||
globalThis.Worker = MockWorker; |
|
||||
|
|
||||
if (typeof globalThis.MessageChannel === 'undefined') { |
|
||||
class PolyMessageChannel { |
|
||||
constructor() { |
|
||||
const channel = this; |
|
||||
|
|
||||
this.port1 = { |
|
||||
onmessage: null, |
|
||||
postMessage(msg) { |
|
||||
setTimeout(() => { |
|
||||
if (typeof channel.port2.onmessage === 'function') { |
|
||||
channel.port2.onmessage({ data: msg }); |
|
||||
} |
|
||||
}, 0); |
|
||||
}, |
|
||||
}; |
|
||||
|
|
||||
this.port2 = { |
|
||||
onmessage: null, |
|
||||
postMessage(msg) { |
|
||||
setTimeout(() => { |
|
||||
if (typeof channel.port1.onmessage === 'function') { |
|
||||
channel.port1.onmessage({ data: msg }); |
|
||||
} |
|
||||
}, 0); |
|
||||
}, |
|
||||
}; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
globalThis.MessageChannel = PolyMessageChannel; |
|
||||
} |
|
||||
|
|
||||
if (typeof window !== 'undefined' && !window.matchMedia) { |
|
||||
Object.defineProperty(window, 'matchMedia', { |
|
||||
writable: true, |
|
||||
configurable: true, |
|
||||
value: jest.fn((query) => ({ |
|
||||
matches: false, |
|
||||
media: query, |
|
||||
onchange: null, |
|
||||
addListener: jest.fn(), |
|
||||
removeListener: jest.fn(), |
|
||||
addEventListener: jest.fn(), |
|
||||
removeEventListener: jest.fn(), |
|
||||
dispatchEvent: jest.fn(), |
|
||||
})), |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
const originalError = console.error; |
|
||||
Object.defineProperty(window.console, 'error', { |
|
||||
writable: true, |
|
||||
configurable: true, |
|
||||
value: (...rest) => { |
|
||||
const logStr = rest.join(''); |
|
||||
if ( |
|
||||
logStr.includes( |
|
||||
'Warning: An update to %s inside a test was not wrapped in act(...)', |
|
||||
) |
|
||||
) { |
|
||||
return; |
|
||||
} |
|
||||
originalError(...rest); |
|
||||
}, |
|
||||
}); |
|
||||
|
|
||||
globalThis.ResizeObserver = class { |
|
||||
observe() {} |
|
||||
unobserve() {} |
|
||||
disconnect() {} |
|
||||
}; |
|
||||
@ -0,0 +1,104 @@ |
|||||
|
import { vi } from 'vitest'; |
||||
|
|
||||
|
const store: Record<string, string> = {}; |
||||
|
|
||||
|
const localStorageMock = { |
||||
|
getItem: vi.fn((key: string) => store[key] ?? null), |
||||
|
setItem: vi.fn((key: string, value: string) => { |
||||
|
store[key] = String(value); |
||||
|
}), |
||||
|
removeItem: vi.fn((key: string) => { |
||||
|
delete store[key]; |
||||
|
}), |
||||
|
clear: vi.fn(() => { |
||||
|
Object.keys(store).forEach((key) => { |
||||
|
delete store[key]; |
||||
|
}); |
||||
|
}), |
||||
|
}; |
||||
|
|
||||
|
globalThis.localStorage = localStorageMock as unknown as Storage; |
||||
|
|
||||
|
Object.defineProperty(URL, 'createObjectURL', { |
||||
|
writable: true, |
||||
|
value: vi.fn(), |
||||
|
}); |
||||
|
|
||||
|
class MockWorker { |
||||
|
url: string; |
||||
|
onmessage: ((e: MessageEvent) => void) | null = null; |
||||
|
constructor(stringUrl: string) { |
||||
|
this.url = stringUrl; |
||||
|
} |
||||
|
postMessage(msg: unknown) { |
||||
|
if (typeof this.onmessage === 'function') { |
||||
|
this.onmessage({ data: msg } as MessageEvent); |
||||
|
} |
||||
|
} |
||||
|
terminate() {} |
||||
|
addEventListener() {} |
||||
|
removeEventListener() {} |
||||
|
} |
||||
|
|
||||
|
globalThis.Worker = MockWorker as unknown as typeof Worker; |
||||
|
|
||||
|
if (typeof globalThis.MessageChannel === 'undefined') { |
||||
|
class PolyMessageChannel { |
||||
|
port1: { |
||||
|
onmessage: ((e: MessageEvent) => void) | null; |
||||
|
postMessage: (msg: unknown) => void; |
||||
|
}; |
||||
|
port2: { |
||||
|
onmessage: ((e: MessageEvent) => void) | null; |
||||
|
postMessage: (msg: unknown) => void; |
||||
|
}; |
||||
|
constructor() { |
||||
|
const channel = this; |
||||
|
this.port1 = { |
||||
|
onmessage: null, |
||||
|
postMessage(msg: unknown) { |
||||
|
setTimeout(() => { |
||||
|
if (typeof channel.port2.onmessage === 'function') { |
||||
|
channel.port2.onmessage({ data: msg } as MessageEvent); |
||||
|
} |
||||
|
}, 0); |
||||
|
}, |
||||
|
}; |
||||
|
this.port2 = { |
||||
|
onmessage: null, |
||||
|
postMessage(msg: unknown) { |
||||
|
setTimeout(() => { |
||||
|
if (typeof channel.port1.onmessage === 'function') { |
||||
|
channel.port1.onmessage({ data: msg } as MessageEvent); |
||||
|
} |
||||
|
}, 0); |
||||
|
}, |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
|
globalThis.MessageChannel = |
||||
|
PolyMessageChannel as unknown as typeof MessageChannel; |
||||
|
} |
||||
|
|
||||
|
if (typeof window !== 'undefined' && !window.matchMedia) { |
||||
|
Object.defineProperty(window, 'matchMedia', { |
||||
|
writable: true, |
||||
|
configurable: true, |
||||
|
value: vi.fn((query: string) => ({ |
||||
|
matches: false, |
||||
|
media: query, |
||||
|
onchange: null, |
||||
|
addListener: vi.fn(), |
||||
|
removeListener: vi.fn(), |
||||
|
addEventListener: vi.fn(), |
||||
|
removeEventListener: vi.fn(), |
||||
|
dispatchEvent: vi.fn(), |
||||
|
})), |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
globalThis.ResizeObserver = class { |
||||
|
observe() {} |
||||
|
unobserve() {} |
||||
|
disconnect() {} |
||||
|
}; |
||||
@ -0,0 +1,39 @@ |
|||||
|
import { join } from 'node:path'; |
||||
|
import { defineConfig } from 'vitest/config'; |
||||
|
|
||||
|
export default defineConfig({ |
||||
|
resolve: { |
||||
|
alias: { |
||||
|
'@': join(__dirname, 'src'), |
||||
|
'@root': join(__dirname), |
||||
|
'@@': join(__dirname, 'src', '.umi'), |
||||
|
}, |
||||
|
}, |
||||
|
test: { |
||||
|
environment: 'happy-dom', |
||||
|
globals: true, |
||||
|
setupFiles: ['./tests/setupTests.ts'], |
||||
|
include: ['src/**/*.{test,spec}.{ts,tsx}'], |
||||
|
// Exclude Umi integration tests that depend on @umijs/max test infrastructure
|
||||
|
// These require Umi's Jest runner and cannot be used with Vitest directly
|
||||
|
exclude: [ |
||||
|
'src/pages/user/login/login.test.tsx', |
||||
|
'node_modules', |
||||
|
'dist', |
||||
|
'.umi', |
||||
|
], |
||||
|
coverage: { |
||||
|
provider: 'v8', |
||||
|
reporter: ['text', 'json', 'html'], |
||||
|
include: ['src/**/*.{ts,tsx}'], |
||||
|
exclude: [ |
||||
|
'src/.umi/**', |
||||
|
'src/services/ant-design-pro/**', |
||||
|
'src/**/*.d.ts', |
||||
|
'src/**/index.style.ts', |
||||
|
], |
||||
|
}, |
||||
|
passWithNoTests: true, |
||||
|
testTimeout: 15000, |
||||
|
}, |
||||
|
}); |
||||
Loading…
Reference in new issue