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

export.test.ts 3.5 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  1. import { Command } from 'commander';
  2. import { exportCommand } from '../../src/commands/export';
  3. import logger from '../../src/logger';
  4. import Eval from '../../src/models/eval';
  5. import { writeOutput } from '../../src/util';
  6. jest.mock('../../src/telemetry', () => ({
  7. record: jest.fn(),
  8. }));
  9. jest.mock('../../src/util', () => ({
  10. writeOutput: jest.fn(),
  11. createOutputMetadata: jest.fn().mockReturnValue({
  12. promptfooVersion: '1.0.0',
  13. nodeVersion: 'v18.0.0',
  14. platform: 'linux',
  15. arch: 'x64',
  16. exportedAt: '2025-07-01T00:00:00.000Z',
  17. evaluationCreatedAt: '2025-07-01T00:00:00.000Z',
  18. author: 'test-author',
  19. }),
  20. }));
  21. jest.mock('../../src/logger', () => ({
  22. info: jest.fn(),
  23. error: jest.fn(),
  24. }));
  25. describe('exportCommand', () => {
  26. let program: Command;
  27. let mockExit: jest.SpyInstance;
  28. let mockEval: any;
  29. beforeEach(() => {
  30. program = new Command();
  31. mockExit = jest.spyOn(process, 'exit').mockImplementation(() => undefined as never);
  32. mockEval = {
  33. id: 'test-id',
  34. createdAt: '2025-07-01T00:00:00.000Z',
  35. author: 'test-author',
  36. config: { test: 'config' },
  37. toEvaluateSummary: jest.fn().mockResolvedValue({ test: 'summary' }),
  38. };
  39. jest.useFakeTimers();
  40. jest.setSystemTime(new Date('2025-07-01T00:00:00.000Z'));
  41. });
  42. afterEach(() => {
  43. jest.clearAllMocks();
  44. jest.useRealTimers();
  45. });
  46. it('should export latest eval record', async () => {
  47. jest.spyOn(Eval, 'latest').mockResolvedValue(mockEval);
  48. exportCommand(program);
  49. await program.parseAsync(['node', 'test', 'export', 'latest', '--output', 'test.json']);
  50. expect(Eval.latest).toHaveBeenCalledWith();
  51. expect(writeOutput).toHaveBeenCalledWith('test.json', mockEval, null);
  52. expect(mockExit).not.toHaveBeenCalled();
  53. });
  54. it('should export eval record by id', async () => {
  55. jest.spyOn(Eval, 'findById').mockResolvedValue(mockEval);
  56. exportCommand(program);
  57. await program.parseAsync(['node', 'test', 'export', 'test-id', '--output', 'test.json']);
  58. expect(Eval.findById).toHaveBeenCalledWith('test-id');
  59. expect(writeOutput).toHaveBeenCalledWith('test.json', mockEval, null);
  60. expect(mockExit).not.toHaveBeenCalled();
  61. });
  62. it('should log JSON data when no output specified', async () => {
  63. jest.spyOn(Eval, 'findById').mockResolvedValue(mockEval);
  64. exportCommand(program);
  65. await program.parseAsync(['node', 'test', 'export', 'test-id']);
  66. const expectedJson = {
  67. evalId: 'test-id',
  68. results: { test: 'summary' },
  69. config: { test: 'config' },
  70. shareableUrl: null,
  71. metadata: {
  72. promptfooVersion: '1.0.0',
  73. nodeVersion: 'v18.0.0',
  74. platform: 'linux',
  75. arch: 'x64',
  76. exportedAt: '2025-07-01T00:00:00.000Z',
  77. evaluationCreatedAt: '2025-07-01T00:00:00.000Z',
  78. author: 'test-author',
  79. },
  80. };
  81. expect(logger.info).toHaveBeenCalledWith(JSON.stringify(expectedJson, null, 2));
  82. });
  83. it('should exit with error when eval not found', async () => {
  84. jest.spyOn(Eval, 'findById').mockResolvedValue(undefined);
  85. exportCommand(program);
  86. await program.parseAsync(['node', 'test', 'export', 'non-existent-id']);
  87. expect(mockExit).toHaveBeenCalledWith(1);
  88. });
  89. it('should handle export errors', async () => {
  90. jest.spyOn(Eval, 'findById').mockRejectedValue(new Error('Export failed'));
  91. exportCommand(program);
  92. await program.parseAsync(['node', 'test', 'export', 'test-id']);
  93. expect(mockExit).toHaveBeenCalledWith(1);
  94. });
  95. });
Tip!

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

Comments

Loading...