index.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @vitest-environment node
  3. */
  4. import { describe, it, expect } from 'vitest';
  5. import { BaseOptions } from '../../src/types';
  6. import { E2ETestError } from '../../src/errors';
  7. import { DEFAULT_TIMEOUTS, SELECTOR_STRATEGIES } from '../../src/constants';
  8. describe('@d8d/e2e-test-utils 基础功能', () => {
  9. describe('类型定义', () => {
  10. it('应该正确导出 BaseOptions 类型', () => {
  11. const options: BaseOptions = { timeout: 1000 };
  12. expect(options.timeout).toBe(1000);
  13. });
  14. it('BaseOptions 应该支持可选属性', () => {
  15. const options: BaseOptions = {};
  16. expect(options.timeout).toBeUndefined();
  17. });
  18. });
  19. describe('错误类', () => {
  20. it('应该能够创建 E2ETestError 实例', () => {
  21. const error = new E2ETestError({
  22. operation: 'testOperation',
  23. target: 'testTarget',
  24. expected: 'expectedValue',
  25. actual: 'actualValue',
  26. });
  27. expect(error).toBeInstanceOf(Error);
  28. expect(error.name).toBe('E2ETestError');
  29. expect(error.context.operation).toBe('testOperation');
  30. expect(error.context.target).toBe('testTarget');
  31. });
  32. it('E2ETestError 应该包含格式化的错误消息', () => {
  33. const error = new E2ETestError({
  34. operation: 'selectOption',
  35. target: 'dropdown',
  36. expected: 'Option A',
  37. actual: 'Option B',
  38. });
  39. expect(error.message).toContain('selectOption failed');
  40. expect(error.message).toContain('dropdown');
  41. });
  42. });
  43. describe('常量定义', () => {
  44. it('DEFAULT_TIMEOUTS 应该包含所有必需的超时配置', () => {
  45. expect(DEFAULT_TIMEOUTS.static).toBeDefined();
  46. expect(DEFAULT_TIMEOUTS.async).toBeDefined();
  47. expect(DEFAULT_TIMEOUTS.networkIdle).toBeDefined();
  48. expect(DEFAULT_TIMEOUTS.static).toBe(2000);
  49. expect(DEFAULT_TIMEOUTS.async).toBe(5000);
  50. expect(DEFAULT_TIMEOUTS.networkIdle).toBe(10000);
  51. });
  52. it('SELECTOR_STRATEGIES 应该包含所有策略', () => {
  53. expect(SELECTOR_STRATEGIES).toContain('data-testid');
  54. expect(SELECTOR_STRATEGIES).toContain('aria-label + role');
  55. expect(SELECTOR_STRATEGIES).toContain('text content + role');
  56. expect(SELECTOR_STRATEGIES).toHaveLength(3);
  57. });
  58. });
  59. describe('Vitest 配置验证', () => {
  60. it('当前测试应该正常运行', () => {
  61. expect(true).toBe(true);
  62. });
  63. it('Vitest 应该支持 TypeScript', () => {
  64. const value: string = 'TypeScript support verified';
  65. expect(value).toBe('TypeScript support verified');
  66. });
  67. });
  68. });