15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
76
77
78
79
80
81
82
import hasNewVersion from './hasNewVersion';
import { getLastUpdate } from './cache';
import getDistVersion from './getDistVersion';
 
jest.mock('./getDistVersion', () => jest.fn().mockReturnValue('1.0.0'));
jest.mock('./cache', () => ({
  getLastUpdate: jest.fn().mockReturnValue(undefined),
  createConfigDir: jest.fn(),
  saveLastUpdate: jest.fn(),
}));
 
const pkg = { name: 'test', version: '1.0.0' };
 
afterEach(() => jest.clearAllMocks());
 
const defaultArgs = {
  pkg,
  shouldNotifyInNpmScript: true,
  alwaysRun: true,
};
 
test('it should not trigger update for same version', async () => {
  const newVersion = await hasNewVersion(defaultArgs);
 
  expect(newVersion).toBe(false);
});
 
test('it should trigger update for patch version bump', async () => {
  (getDistVersion as jest.Mock).mockReturnValue('1.0.1');
 
  const newVersion = await hasNewVersion(defaultArgs);
 
  expect(newVersion).toBe('1.0.1');
});
 
test('it should trigger update for minor version bump', async () => {
  (getDistVersion as jest.Mock).mockReturnValue('1.1.0');
 
  const newVersion = await hasNewVersion(defaultArgs);
 
  expect(newVersion).toBe('1.1.0');
});
 
test('it should trigger update for major version bump', async () => {
  (getDistVersion as jest.Mock).mockReturnValue('2.0.0');
 
  const newVersion = await hasNewVersion(defaultArgs);
 
  expect(newVersion).toBe('2.0.0');
});
 
test('it should not trigger update if version is lower', async () => {
  (getDistVersion as jest.Mock).mockReturnValue('0.0.9');
 
  const newVersion = await hasNewVersion(defaultArgs);
 
  expect(newVersion).toBe(false);
});
 
it('should trigger update check if last update older than config', async () => {
  const TWO_WEEKS = new Date().getTime() - 1000 * 60 * 60 * 24 * 14;
  (getLastUpdate as jest.Mock).mockReturnValue(TWO_WEEKS);
  const newVersion = await hasNewVersion({
    pkg,
    shouldNotifyInNpmScript: true,
  });
 
  expect(newVersion).toBe(false);
  expect(getDistVersion).toHaveBeenCalled();
});
 
it('should not trigger update check if last update is too recent', async () => {
  const TWELVE_HOURS = new Date().getTime() - 1000 * 60 * 60 * 12;
  (getLastUpdate as jest.Mock).mockReturnValue(TWELVE_HOURS);
  const newVersion = await hasNewVersion({
    pkg,
    shouldNotifyInNpmScript: true,
  });
 
  expect(newVersion).toBe(false);
  expect(getDistVersion).not.toHaveBeenCalled();
});