-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathinit.test.ts
More file actions
121 lines (94 loc) · 2.73 KB
/
init.test.ts
File metadata and controls
121 lines (94 loc) · 2.73 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { afterEach, beforeEach, expect, it, jest } from '@jest/globals';
import { readFile } from 'fs-extra';
import mockFs from 'mock-fs';
import { stdin } from 'mock-stdin';
import { join } from 'path';
import { init } from '../init';
let io: ReturnType<typeof stdin> | undefined;
const root = '/path/to/library';
const enter = '\x0D';
const waitFor = async (callback: () => void) => {
const interval = 10;
let timeout = 50;
return new Promise((resolve, reject) => {
const intervalId = setInterval(() => {
try {
callback();
clearInterval(intervalId);
resolve(undefined);
} catch (error) {
if (timeout <= 0) {
clearInterval(intervalId);
reject(error);
}
timeout -= interval;
}
}, interval);
});
};
beforeEach(() => {
io = stdin();
mockFs({
[root]: {
'package.json': JSON.stringify({
name: 'library',
version: '1.0.0',
}),
'src': {
'index.ts': "export default 'hello world';",
},
},
});
});
afterEach(() => {
io?.restore();
mockFs.restore();
jest.restoreAllMocks();
});
it('initializes the configuration', async () => {
jest.spyOn(process.stdout, 'write').mockImplementation(() => true);
process.chdir(root);
const run = async () => {
await waitFor(() => {
const lastCall = (process.stdout.write as jest.Mock).mock.lastCall;
if (lastCall == null) {
throw new Error('No output');
}
if (/The working directory is not clean/.test(String(lastCall[0]))) {
io?.send('y');
}
});
await waitFor(() =>
expect(process.stdout.write).toHaveBeenLastCalledWith(
expect.stringMatching('Where are your source files?')
)
);
io?.send(enter);
await waitFor(() =>
expect(process.stdout.write).toHaveBeenLastCalledWith(
expect.stringMatching('Where do you want to generate the output files?')
)
);
io?.send(enter);
await waitFor(() =>
expect(process.stdout.write).toHaveBeenLastCalledWith(
expect.stringMatching('Which targets do you want to build?')
)
);
io?.send(enter);
await waitFor(() =>
expect(process.stdout.write).toHaveBeenLastCalledWith(
expect.stringMatching(
"You have enabled 'typescript' compilation, but we couldn't find a 'tsconfig.json' in project root"
)
)
);
io?.send(enter);
};
await Promise.all([run(), init()]);
expect(process.stdout.write).toHaveBeenLastCalledWith(
expect.stringMatching('configured successfully!')
);
expect(await readFile(join(root, 'package.json'), 'utf8')).toMatchSnapshot();
expect(await readFile(join(root, 'tsconfig.json'), 'utf8')).toMatchSnapshot();
});