Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

jest.mdc 2.9 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  1. ---
  2. description: Guidelines for writing Jest tests for core functionality
  3. globs: 'test/**/*.test.ts,test/**/*.spec.ts'
  4. alwaysApply: false
  5. ---
  6. # Jest Testing Guidelines
  7. Files: "test/**/\*.test.ts,test/**/\*.spec.ts"
  8. This rule provides guidance for writing and running Jest tests in the promptfoo project, which are different from the Vitest tests in the `src/app` directory.
  9. ## Jest Setup
  10. - Always run `nvm use` first to ensure you're using the correct Node.js version
  11. - Run Jest tests with coverage and randomize options:
  12. ```
  13. npm test -- --coverage --randomize
  14. ```
  15. - For targeted tests, use the specific pattern:
  16. ```
  17. npx jest providers/openai --coverage --randomize
  18. ```
  19. - Do NOT use watch mode for CI or shared development environments
  20. - Always run tests in a single pass to ensure consistent results
  21. ## Testing Best Practices
  22. - Mock as few functions as possible to keep tests realistic
  23. - Never increase the function timeout - fix the test instead
  24. - Organize tests in descriptive `describe` and `it` blocks:
  25. ```typescript
  26. describe('OpenAI Provider', () => {
  27. describe('chat completion', () => {
  28. it('should handle normal input correctly', () => {
  29. // test code
  30. });
  31. it('should handle edge cases', () => {
  32. // test code
  33. });
  34. });
  35. });
  36. ```
  37. - When writing expectations, prefer assertions on entire objects rather than individual keys:
  38. ```typescript
  39. // Preferred:
  40. expect(result).toEqual({ id: 1, name: 'test' });
  41. // Avoid:
  42. expect(result.id).toEqual(1);
  43. expect(result.name).toEqual('test');
  44. ```
  45. - Clean up after tests to prevent side effects:
  46. ```typescript
  47. afterEach(() => {
  48. jest.resetAllMocks();
  49. });
  50. ```
  51. - Run tests with `--randomize` flag to ensure your mocks setup and teardown don't affect other tests
  52. ## Mocking
  53. - Use Jest's mocking utilities rather than complex custom mocks:
  54. ```typescript
  55. jest.mock('axios');
  56. const axiosMock = axios as jest.Mocked<typeof axios>;
  57. axiosMock.post.mockResolvedValue({ data: { result: 'success' } });
  58. ```
  59. - Prefer shallow mocking over deep mocking
  60. - Mock external dependencies but not the code being tested
  61. - Reset mocks between tests to prevent test pollution
  62. - For database tests, use in-memory instances or proper test fixtures
  63. ## Provider Testing
  64. - Test both success and error cases for each provider
  65. - Mock API responses to avoid external dependencies in tests
  66. - Validate that provider options are properly passed to the underlying service
  67. - Test error handling and edge cases (rate limits, timeouts, etc.)
  68. - Ensure provider caching behaves as expected
  69. ## Test Execution Requirements
  70. - Always include both `--coverage` and `--randomize` flags when running tests
  71. - Run tests in a single pass (no watch mode for CI)
  72. - Ensure all tests are independent and can run in any order
  73. - Clean up any test data or mocks after each test
  74. - Run the full test suite before committing changes
  75. - Test failures should be deterministic and not depend on external state
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...