Files

160 lines
5.4 KiB
TypeScript
Raw Permalink Normal View History

import { dirname, join, resolve } from 'node:path';
2026-03-26 17:43:51 +08:00
import tsconfigPaths from 'vite-tsconfig-paths';
import { coverageConfigDefaults, defineConfig } from 'vitest/config';
2023-05-21 15:34:04 +08:00
if (process.env.NODE_ENV === 'production') {
Reflect.set(process.env, 'NODE_ENV', 'test');
}
const alias = {
// Downstream workspaces sometimes pnpm-override @lobechat/business-* packages to
// internal implementations whose source files import alias paths that only exist
// in the outer workspace, causing vite import-analysis to fail when running tests
// from this repo. Pin the package to the local stub so tests here stay hermetic.
'@lobechat/business-model-runtime': resolve(
__dirname,
'./packages/business/model-runtime/src/index.ts',
),
'@lobechat/business-model-bank/model-config': resolve(
__dirname,
'./packages/business/model-bank/src/model-config.ts',
),
'@lobechat/business-model-bank': resolve(
__dirname,
'./packages/business/model-bank/src/index.ts',
),
'@emoji-mart/data': resolve(__dirname, './tests/mocks/emojiMartData.ts'),
'@emoji-mart/react': resolve(__dirname, './tests/mocks/emojiMartReact.tsx'),
'@/utils/client/switchLang': resolve(__dirname, './src/utils/client/switchLang'),
'@/const/locale': resolve(__dirname, './src/const/locale'),
// TODO: after refactor the errorResponse, we can remove it
'@/utils/errorResponse': resolve(__dirname, './src/utils/errorResponse'),
'@/utils/unzipFile': resolve(__dirname, './src/utils/unzipFile'),
'@/utils/server': resolve(__dirname, './src/utils/server'),
'@/utils/identifier': resolve(__dirname, './src/utils/identifier'),
'@/utils/electron': resolve(__dirname, './src/utils/electron'),
'@/utils/markdownToTxt': resolve(__dirname, './src/utils/markdownToTxt'),
'@/utils/sanitizeFileName': resolve(__dirname, './src/utils/sanitizeFileName'),
2026-06-09 15:54:26 +08:00
// Workspace store lives in the cloud repo; submodule-only tests get a stub
// that reports no active workspace so workspace-aware nav helpers behave
// like plain react-router.
'@/store/workspace': resolve(__dirname, './tests/mocks/storeWorkspace.ts'),
'~test-utils': resolve(__dirname, './tests/utils.tsx'),
'lru_map': resolve(__dirname, './tests/mocks/lru_map'),
};
2023-05-21 15:34:04 +08:00
export default defineConfig({
define: {
'__CI__': process.env.CI === 'true' ? 'true' : 'false',
'__DEV__': process.env.NODE_ENV !== 'production' ? 'true' : 'false',
'__ELECTRON__': 'false',
'__MOBILE__': 'false',
'__TEST__': 'true',
},
optimizeDeps: {
exclude: ['crypto', 'util', 'tty'],
include: ['@lobehub/tts'],
},
plugins: [
tsconfigPaths({ projects: ['.'] }),
2026-03-26 17:43:51 +08:00
// Let `.md` imports resolve to their raw text content so Rollup/Vitest
// doesn't try to parse Markdown as JavaScript.
{
name: 'raw-md',
transform(_, id) {
if (id.endsWith('.md'))
return { code: 'export default ""', map: null };
},
},
/**
* @lobehub/fluent-emoji@4.0.0 ships `es/FluentEmoji/style.js` but its `es/FluentEmoji/index.js`
* imports `./style/index.js` which doesn't exist.
*
* In app bundlers this can be tolerated/rewritten, but Vite/Vitest resolves it strictly and
* fails the whole test run. Redirect it to the real file.
*/
{
enforce: 'pre',
name: 'fix-lobehub-fluent-emoji-style-import',
resolveId(id, importer) {
if (!importer) return null;
const isFluentEmojiEntry =
importer.endsWith('/@lobehub/fluent-emoji/es/FluentEmoji/index.js') ||
importer.includes('/@lobehub/fluent-emoji/es/FluentEmoji/index.js?');
const isMissingStyleIndex =
id === './style/index.js' ||
id.endsWith('/@lobehub/fluent-emoji/es/FluentEmoji/style/index.js') ||
id.endsWith('/@lobehub/fluent-emoji/es/FluentEmoji/style/index.js?') ||
id.endsWith('/FluentEmoji/style/index.js') ||
id.endsWith('/FluentEmoji/style/index.js?');
if (isFluentEmojiEntry && isMissingStyleIndex)
return resolve(dirname(importer), 'style.js');
return null;
},
},
],
resolve: {
alias,
},
2023-05-21 15:34:04 +08:00
test: {
alias,
2023-08-14 23:18:46 +08:00
coverage: {
all: false,
exclude: [
// https://github.com/lobehub/lobe-chat/pull/7265
...coverageConfigDefaults.exclude,
'__mocks__/**',
2025-08-21 20:39:29 +08:00
'**/packages/**',
// just ignore the migration code
// we will use pglite in the future
// so the coverage of this file is not important
'src/utils/fetch/fetchEventSource/*.ts',
],
2023-08-14 23:18:46 +08:00
provider: 'v8',
reporter: ['text', 'json', 'lcov', 'text-summary'],
reportsDirectory: './coverage/app',
2023-08-14 23:18:46 +08:00
},
2024-04-20 23:30:33 +08:00
environment: 'happy-dom',
2024-06-23 16:09:11 +08:00
exclude: [
'**/node_modules/**',
'**/.*/**',
2024-06-23 16:09:11 +08:00
'**/dist/**',
'**/build/**',
2025-12-19 23:13:22 +08:00
'**/tmp/**',
'**/temp/**',
'**/docs/**',
'**/locales/**',
'**/public/**',
2025-05-09 00:08:32 +08:00
'**/apps/desktop/**',
'**/apps/mobile/**',
2026-03-06 11:42:29 +08:00
'**/apps/cli/**',
2025-08-21 20:39:29 +08:00
'**/packages/**',
2025-10-14 07:32:52 +02:00
'**/e2e/**',
2024-06-23 16:09:11 +08:00
],
2023-07-22 18:46:31 +08:00
globals: true,
server: {
deps: {
inline: [
'vitest-canvas-mock',
/@emoji-mart/,
'emoji-mart',
'@lobehub/ui',
'@lobehub/fluent-emoji',
'@pierre/diffs',
'@pierre/diffs/react',
'lru_map',
'lexical',
/@lexical\//,
/@lobehub\//,
],
},
},
2025-05-09 00:08:32 +08:00
setupFiles: join(__dirname, './tests/setup.ts'),
2023-05-21 15:34:04 +08:00
},
});