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.setup.js 2.1 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
  1. /* eslint-disable jest/require-top-level-describe */
  2. import { rm } from 'fs/promises';
  3. import fs from 'fs';
  4. import yaml from 'js-yaml';
  5. import nock from 'nock';
  6. import { TEST_CONFIG_DIR } from './.jest/setEnvVars';
  7. // Disable all real network requests
  8. nock.disableNetConnect();
  9. nock.enableNetConnect('127.0.0.1');
  10. nock.emitter.on('no match', (req) => {
  11. if (!req.host.includes('127.0.0.1') && !req.host.includes('localhost')) {
  12. console.error(`Unexpected HTTP request: ${req.method} ${req.href}`);
  13. }
  14. });
  15. // Ensure mocks are loaded before tests
  16. jest.mock('fs');
  17. jest.mock('better-sqlite3', () => {
  18. const mockDb = {
  19. prepare: jest.fn().mockReturnValue({
  20. run: jest.fn(),
  21. get: jest.fn(),
  22. all: jest.fn(),
  23. finalize: jest.fn(),
  24. }),
  25. transaction: jest.fn((fn) => fn()),
  26. exec: jest.fn(),
  27. close: jest.fn(),
  28. backup: jest.fn().mockReturnValue({
  29. step: jest.fn().mockResolvedValue(true),
  30. finish: jest.fn(),
  31. }),
  32. };
  33. const Database = jest.fn().mockImplementation(() => mockDb);
  34. Database.prototype = mockDb;
  35. return { Database };
  36. });
  37. jest.mock('./src/globalConfig/globalConfig');
  38. jest.mock('./src/globalConfig/cloud');
  39. jest.mock('./src/globalConfig/accounts');
  40. jest.mock('./src/telemetry');
  41. // Reset all mocks before each test
  42. beforeEach(() => {
  43. jest.clearAllMocks();
  44. // Reset fs mock state
  45. const mockFs = fs;
  46. mockFs.__clearMockFiles();
  47. // Create necessary directories and files for tests
  48. mockFs.mkdirSync('/tmp', { recursive: true });
  49. mockFs.mkdirSync('/mock/config/dir', { recursive: true });
  50. mockFs.writeFileSync('promptfoo-errors.log', '');
  51. mockFs.writeFileSync('promptfoo-debug.log', '');
  52. // Initialize with default config
  53. const configPath = '/mock/config/dir/promptfoo.yaml';
  54. const defaultConfig = {
  55. account: { email: 'mock-user@example.com' },
  56. cloud: {
  57. appUrl: 'https://mock.example.com',
  58. apiHost: 'https://mock-api.example.com',
  59. apiKey: 'mock-key',
  60. },
  61. };
  62. mockFs.__setMockFileContent(configPath, yaml.dump(defaultConfig));
  63. });
  64. afterAll(async () => {
  65. try {
  66. await rm(TEST_CONFIG_DIR, { recursive: true });
  67. } catch {}
  68. });
Tip!

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

Comments

Loading...