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

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

Comments

Loading...