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

updates.test.ts 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
  1. import packageJson from '../package.json';
  2. import { fetchWithTimeout } from '../src/fetch';
  3. import { getLatestVersion, checkForUpdates } from '../src/updates';
  4. jest.mock('../src/logger');
  5. jest.mock('../src/fetch', () => ({
  6. fetchWithTimeout: jest.fn(),
  7. }));
  8. jest.mock('../package.json', () => ({
  9. version: '0.11.0',
  10. }));
  11. describe('getLatestVersion', () => {
  12. it('should return the latest version of the package', async () => {
  13. jest.mocked(fetchWithTimeout).mockResolvedValueOnce({
  14. ok: true,
  15. json: async () => ({ latestVersion: '1.1.0' }),
  16. } as never);
  17. const latestVersion = await getLatestVersion();
  18. expect(latestVersion).toBe('1.1.0');
  19. });
  20. it('should throw an error if the response is not ok', async () => {
  21. jest.mocked(fetchWithTimeout).mockResolvedValueOnce({
  22. ok: false,
  23. } as never);
  24. await expect(getLatestVersion()).rejects.toThrow(
  25. 'Failed to fetch package information for promptfoo',
  26. );
  27. });
  28. });
  29. describe('checkForUpdates', () => {
  30. beforeEach(() => {
  31. jest.spyOn(console, 'log').mockImplementation(() => {});
  32. });
  33. afterEach(() => {
  34. jest.mocked(console.log).mockRestore();
  35. });
  36. it('should log an update message if a newer version is available - minor ver', async () => {
  37. jest.mocked(fetchWithTimeout).mockResolvedValueOnce({
  38. ok: true,
  39. json: async () => ({ latestVersion: '1.1.0' }),
  40. } as never);
  41. const result = await checkForUpdates();
  42. expect(result).toBeTruthy();
  43. });
  44. it('should log an update message if a newer version is available - major ver', async () => {
  45. jest.mocked(fetchWithTimeout).mockResolvedValueOnce({
  46. ok: true,
  47. json: async () => ({ latestVersion: '1.1.0' }),
  48. } as never);
  49. const result = await checkForUpdates();
  50. expect(result).toBeTruthy();
  51. });
  52. it('should not log an update message if the current version is up to date', async () => {
  53. jest.mocked(fetchWithTimeout).mockResolvedValueOnce({
  54. ok: true,
  55. json: async () => ({ latestVersion: packageJson.version }),
  56. } as never);
  57. const result = await checkForUpdates();
  58. expect(result).toBeFalsy();
  59. });
  60. });
Tip!

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

Comments

Loading...