forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdates.test.ts
73 lines (59 loc) · 2.03 KB
/
updates.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { getLatestVersion, checkForUpdates } from '../src/updates';
import { fetchWithTimeout } from '../src/fetch';
import packageJson from '../package.json';
jest.mock('../src/fetch', () => ({
fetchWithTimeout: jest.fn(),
}));
jest.mock('../package.json', () => ({
version: '0.11.0',
}));
describe('getLatestVersion', () => {
it('should return the latest version of the package', async () => {
jest.mocked(fetchWithTimeout).mockResolvedValueOnce({
ok: true,
json: async () => ({ latestVersion: '1.1.0' }),
});
const latestVersion = await getLatestVersion();
expect(latestVersion).toBe('1.1.0');
});
it('should throw an error if the response is not ok', async () => {
jest.mocked(fetchWithTimeout).mockResolvedValueOnce({
ok: false,
});
await expect(getLatestVersion()).rejects.toThrow(
'Failed to fetch package information for promptfoo',
);
});
});
describe('checkForUpdates', () => {
beforeEach(() => {
jest.spyOn(console, 'log').mockImplementation(() => {});
});
afterEach(() => {
jest.mocked(console.log).mockRestore();
});
it('should log an update message if a newer version is available - minor ver', async () => {
jest.mocked(fetchWithTimeout).mockResolvedValueOnce({
ok: true,
json: async () => ({ latestVersion: '1.1.0' }),
});
const result = await checkForUpdates();
expect(result).toBeTruthy();
});
it('should log an update message if a newer version is available - major ver', async () => {
jest.mocked(fetchWithTimeout).mockResolvedValueOnce({
ok: true,
json: async () => ({ latestVersion: '1.1.0' }),
});
const result = await checkForUpdates();
expect(result).toBeTruthy();
});
it('should not log an update message if the current version is up to date', async () => {
jest.mocked(fetchWithTimeout).mockResolvedValueOnce({
ok: true,
json: async () => ({ latestVersion: packageJson.version }),
});
const result = await checkForUpdates();
expect(result).toBeFalsy();
});
});