-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsync-version.test.js
More file actions
89 lines (71 loc) · 2.99 KB
/
Copy pathsync-version.test.js
File metadata and controls
89 lines (71 loc) · 2.99 KB
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
83
84
85
86
87
88
89
const fs = require('fs');
const path = require('path');
describe('sync-version.js', () => {
let consoleSpy;
let readFileSyncSpy;
let writeFileSyncSpy;
beforeEach(() => {
jest.resetModules();
consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
readFileSyncSpy = jest.spyOn(fs, 'readFileSync');
writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
});
afterEach(() => {
consoleSpy.mockRestore();
readFileSyncSpy.mockRestore();
writeFileSyncSpy.mockRestore();
});
test('updates manifest.json when versions differ', () => {
const packageJson = { version: '1.2.3' };
const manifestContent = '{\n "version": "1.0.0",\n "name": "test"\n}';
readFileSyncSpy.mockImplementation((filePath) => {
if (filePath.endsWith('package.json')) return JSON.stringify(packageJson);
if (filePath.endsWith('manifest.json')) return manifestContent;
return '{}';
});
require('../scripts/sync-version');
expect(writeFileSyncSpy).toHaveBeenCalledWith(
expect.stringContaining('manifest.json'),
'{\n "version": "1.2.3",\n "name": "test"\n}'
);
expect(consoleSpy).toHaveBeenCalledWith('Updated manifest.json version to 1.2.3');
});
test('does not update manifest.json when versions match', () => {
const packageJson = { version: '1.0.0' };
const manifestContent = '{\n "version": "1.0.0",\n "name": "test"\n}';
readFileSyncSpy.mockImplementation((filePath) => {
if (filePath.endsWith('package.json')) return JSON.stringify(packageJson);
if (filePath.endsWith('manifest.json')) return manifestContent;
return '{}';
});
require('../scripts/sync-version');
expect(writeFileSyncSpy).not.toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalledWith('manifest.json already matches version 1.0.0');
});
test('throws error if manifest.json does not contain version field', () => {
const packageJson = { version: '1.2.3' };
const manifestContent = '{\n "name": "test"\n}';
readFileSyncSpy.mockImplementation((filePath) => {
if (filePath.endsWith('package.json')) return JSON.stringify(packageJson);
if (filePath.endsWith('manifest.json')) return manifestContent;
return '{}';
});
expect(() => {
require('../scripts/sync-version');
}).toThrow('Could not find version field in manifest.json');
});
test('handles different formatting in manifest.json', () => {
const packageJson = { version: '1.2.3' };
const manifestContent = '{\n"version" : "1.0.0",\n"name": "test"\n}';
readFileSyncSpy.mockImplementation((filePath) => {
if (filePath.endsWith('package.json')) return JSON.stringify(packageJson);
if (filePath.endsWith('manifest.json')) return manifestContent;
return '{}';
});
require('../scripts/sync-version');
expect(writeFileSyncSpy).toHaveBeenCalledWith(
expect.stringContaining('manifest.json'),
'{\n"version": "1.2.3",\n"name": "test"\n}'
);
});
});