build: 为项目创建单元测试框架 #45

Manually merged
remilia merged 8 commits from build/test into master 2025-10-27 14:16:26 +08:00
7 changed files with 490 additions and 17 deletions
Showing only changes of commit 9294cd3199 - Show all commits

View File

@ -0,0 +1,49 @@
import { describe, it, expect } from 'vitest';
describe('toProductSpecView', () => {
it('should map translations.key an value corrently', () => {
const raw: ProductSpec = {
id: 1,
value: '100W',
translations: [
{
id: 1,
key: 'Power',
},
],
};
const expected: ProductSpecView = {
id: 1,
key: 'Power',
value: '100W',
};
expect(toProductSpecView(raw)).toEqual(expected);
});
it('should handle missing translations gracefully', () => {
const raw: ProductSpec = { id: 2, value: '220V', translations: [] };
const view = toProductSpecView(raw);
expect(view.key).toBe('');
});
});
describe('toProductSpecGroupView', () => {
it('should map translations.name and specs correctly', () => {
const raw: ProductSpecGroup = {
id: 1,
translations: [{ id: 1, name: 'Electrical' }],
specs: [
{ id: 1, value: '100W', translations: [{ id: 1, key: 'Power' }] },
{ id: 2, value: '220V', translations: [{ id: 2, key: 'Voltage' }] },
],
};
const view: ProductSpecGroupView = toProductSpecGroupView(raw);
expect(view.name).toBe('Electrical');
expect(view.specs).toHaveLength(2);
expect(view.specs[0].key).toBe('Power');
expect(view.specs[1].value).toBe('220V');
});
});

View File

@ -2,7 +2,7 @@ import {
formatFileSize,
getFileExtension,
formatFileExtension,
} from '../../../app/utils/file';
} from '@/utils/file';
import { expect, test, describe } from 'vitest';
/**

View File

@ -1,23 +1,7 @@
import { defineConfig } from 'vitest/config';
import { defineVitestProject } from '@nuxt/test-utils/config';
import { defineVitestConfig } from '@nuxt/test-utils/config';
export default defineConfig({
export default defineVitestConfig({
test: {
projects: [
{
test: {
name: 'unit',
include: ['tests/{e2e,unit}/**/*.{test,spec}.ts'],
environment: 'node',
},
},
await defineVitestProject({
test: {
name: 'nuxt',
include: ['tests/nuxt/**/*.{test,spec}.ts'],
environment: 'nuxt',
},
}),
],
environment: 'nuxt',
},
});