forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalConfig.test.ts
52 lines (41 loc) · 1.36 KB
/
globalConfig.test.ts
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
import * as fs from 'fs';
import yaml from 'js-yaml';
import { readGlobalConfig, writeGlobalConfig } from '../src/globalConfig/globalConfig';
jest.mock('fs', () => ({
readFileSync: jest.fn(),
writeFileSync: jest.fn(),
statSync: jest.fn(),
readdirSync: jest.fn(),
existsSync: jest.fn(),
mkdirSync: jest.fn(),
}));
jest.mock('proxy-agent', () => ({
ProxyAgent: jest.fn().mockImplementation(() => ({})),
}));
jest.mock('glob', () => ({
globSync: jest.fn(),
}));
jest.mock('../src/database');
describe('readCliConfig', () => {
afterEach(() => {
jest.clearAllMocks();
writeGlobalConfig({});
});
it('reads from existing config', () => {
const config = { account: { email: 'test@example.com' } };
jest.mocked(fs.existsSync).mockReturnValue(true);
jest.mocked(fs.readFileSync).mockReturnValue(yaml.dump(config));
const result = readGlobalConfig();
expect(fs.existsSync).toHaveBeenCalledTimes(1);
expect(fs.readFileSync).toHaveBeenCalledTimes(1);
expect(result).toEqual(config);
});
it('creates new config if none exists', () => {
jest.mocked(fs.existsSync).mockReturnValue(false);
jest.mocked(fs.writeFileSync).mockImplementation();
const result = readGlobalConfig();
expect(fs.existsSync).toHaveBeenCalledTimes(3);
expect(fs.writeFileSync).toHaveBeenCalledTimes(2);
expect(result).toEqual({});
});
});