-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs-utils.spec.ts
90 lines (86 loc) · 3.16 KB
/
fs-utils.spec.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { describe, it, expect, jest } from '@jest/globals';
import { within, cd, $, ProcessOutput, fs } from 'zx';
async function withinFsUtils<R>(callback: () => R|Promise<R>): Promise<R> {
return within(async () => {
cd('test/fixtures/fs-utils');
return Promise.resolve(callback());
})
}
async function withChangedFile(file: string, change: (content: string) => string, callback: () => Promise<void>): Promise<void> {
const originalContent = fs.readFileSync(file, 'utf8');
const changedContent = change(originalContent);
expect(changedContent).not.toEqual(originalContent);
fs.writeFileSync(file, changedContent);
try {
await callback();
} finally {
fs.writeFileSync(file, originalContent);
}
}
jest.setTimeout(60000);
describe('Test project fs-utils', function () {
it('npm ci', async () => {
await withinFsUtils(async () => {
await $`npm ci`;
});
});
it('npm test', async () => {
await withinFsUtils(async () => {
await $`npm test`;
});
});
it('generate-api-docs-and-update-readme', async () => {
await withinFsUtils(async () => {
function readFileContentAndTimestamp() {
return {
content: fs.readFileSync('README.md', 'utf8'),
timestamp: fs.statSync('README.md').mtimeMs,
};
}
const originalReadme = readFileContentAndTimestamp();
await $`node ../../../../node_modules/@handy-common-utils/dev-utils/dist/bin/generate-api-docs-and-update-readme.js`;
const updatedReadme = readFileContentAndTimestamp();
expect(updatedReadme.content).toEqual(originalReadme.content);
expect(updatedReadme.timestamp).not.toEqual(originalReadme.timestamp);
});
});
it(`should "npm test" fail when there's compilation error in src`, async () => {
await withinFsUtils(async () => {
await withChangedFile('src/fs-utils.ts', content => content.replace('Promise.', 'Promise1.'), async () => {
try {
const output = await $`npm test`;
expect(output.exitCode).toEqual(1);
} catch (error) {
const output = error as ProcessOutput;
expect(output.exitCode).toEqual(1);
}
});
});
});
it(`should "npm test" fail when there's compilation error in test`, async () => {
await withinFsUtils(async () => {
await withChangedFile('test/fs-utils.spec.ts', content => content.replace('FsUtils.replaceInFiles', 'FsUtils.replaceInFiles2'), async () => {
try {
const output = await $`npm test`;
expect(output.exitCode).toEqual(1);
} catch (error) {
const output = error as ProcessOutput;
expect(output.exitCode).toEqual(1);
}
});
});
});
it(`should "npm test" fail when there's test case failed`, async () => {
await withinFsUtils(async () => {
await withChangedFile('test/fs-utils.spec.ts', content => content.replace('.toEqual(', '.not.toEqual('), async () => {
try {
const output = await $`npm test`;
expect(output.exitCode).toEqual(1);
} catch (error) {
const output = error as ProcessOutput;
expect(output.exitCode).toEqual(1);
}
});
});
});
});