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

globalConfig.test.ts 1.4 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
  1. import * as fs from 'fs';
  2. import yaml from 'js-yaml';
  3. import { readGlobalConfig, writeGlobalConfig } from '../src/globalConfig/globalConfig';
  4. jest.mock('fs', () => ({
  5. readFileSync: jest.fn(),
  6. writeFileSync: jest.fn(),
  7. statSync: jest.fn(),
  8. readdirSync: jest.fn(),
  9. existsSync: jest.fn(),
  10. mkdirSync: jest.fn(),
  11. }));
  12. jest.mock('proxy-agent', () => ({
  13. ProxyAgent: jest.fn().mockImplementation(() => ({})),
  14. }));
  15. jest.mock('glob', () => ({
  16. globSync: jest.fn(),
  17. }));
  18. jest.mock('../src/database');
  19. describe('readCliConfig', () => {
  20. afterEach(() => {
  21. jest.clearAllMocks();
  22. writeGlobalConfig({});
  23. });
  24. it('reads from existing config', () => {
  25. const config = { account: { email: 'test@example.com' } };
  26. jest.mocked(fs.existsSync).mockReturnValue(true);
  27. jest.mocked(fs.readFileSync).mockReturnValue(yaml.dump(config));
  28. const result = readGlobalConfig();
  29. expect(fs.existsSync).toHaveBeenCalledTimes(1);
  30. expect(fs.readFileSync).toHaveBeenCalledTimes(1);
  31. expect(result).toEqual(config);
  32. });
  33. it('creates new config if none exists', () => {
  34. jest.mocked(fs.existsSync).mockReturnValue(false);
  35. jest.mocked(fs.writeFileSync).mockImplementation();
  36. const result = readGlobalConfig();
  37. expect(fs.existsSync).toHaveBeenCalledTimes(3);
  38. expect(fs.writeFileSync).toHaveBeenCalledTimes(2);
  39. expect(result).toEqual({});
  40. });
  41. });
Tip!

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

Comments

Loading...