-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtypescript-step.test.js
141 lines (130 loc) · 3.31 KB
/
typescript-step.test.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { expect } from 'chai';
import { deleteSync } from 'del';
import { existsSync, mkdirSync, readdirSync, readFileSync } from 'fs';
import path from 'path';
import { PROMPT_MESSAGES, testDir, setup, promiseWithTimeout, timeout } from './utils.js';
const inputs = {
emptyDir: './fixtures/select-typescript/empty-dir',
};
function isEmpty(dirPath) {
return !existsSync(dirPath) || readdirSync(dirPath).length === 0;
}
function ensureEmptyDir() {
const dirPath = path.resolve(testDir, inputs.emptyDir);
if (!existsSync(dirPath)) {
mkdirSync(dirPath, { recursive: true });
} else if (!isEmpty(dirPath)) {
const globPath = path.resolve(dirPath, '*');
deleteSync(globPath, { dot: true });
}
}
function getTsConfig(installDir) {
const filePath = path.resolve(testDir, installDir, 'tsconfig.json');
return JSON.parse(readFileSync(filePath, 'utf-8'));
}
describe('[create-astro] select typescript', function () {
this.timeout(timeout);
beforeEach(ensureEmptyDir);
afterEach(ensureEmptyDir);
it('should prompt for typescript when none is provided', async function () {
return promiseWithTimeout(
(resolve, onStdout) => {
const { stdout } = setup([
inputs.emptyDir,
'--template',
'minimal',
'--install',
'0',
'--git',
'0',
]);
stdout.on('data', (chunk) => {
onStdout(chunk);
if (chunk.includes(PROMPT_MESSAGES.typescript)) {
resolve();
}
});
},
() => lastStdout
);
});
it('should not prompt for typescript when provided', async function () {
return promiseWithTimeout(
(resolve, onStdout) => {
const { stdout } = setup([
inputs.emptyDir,
'--template',
'minimal',
'--install',
'0',
'--git',
'0',
'--typescript',
'base',
]);
stdout.on('data', (chunk) => {
onStdout(chunk);
if (chunk.includes(PROMPT_MESSAGES.typescriptSucceed)) {
resolve();
}
});
},
() => lastStdout
);
});
it('should use "strict" config when specified', async function () {
return promiseWithTimeout(
(resolve, onStdout) => {
let wrote = false;
const { stdout, stdin } = setup([
inputs.emptyDir,
'--template',
'minimal',
'--install',
'0',
'--git',
'0',
]);
stdout.on('data', (chunk) => {
onStdout(chunk);
if (!wrote && chunk.includes(PROMPT_MESSAGES.typescript)) {
stdin.write('\x1B\x5B\x42\x0D');
wrote = true;
}
if (chunk.includes(PROMPT_MESSAGES.typescriptSucceed)) {
const tsConfigJson = getTsConfig(inputs.emptyDir);
expect(tsConfigJson).to.deep.equal({ extends: 'astro/tsconfigs/strict' });
resolve();
}
});
},
() => lastStdout
);
});
it('should create tsconfig.json when missing', async function () {
return promiseWithTimeout(
(resolve, onStdout) => {
const { stdout } = setup([
inputs.emptyDir,
'--template',
'cassidoo/shopify-react-astro',
'--install',
'0',
'--git',
'0',
'--typescript',
'base',
]);
stdout.on('data', (chunk) => {
onStdout(chunk);
if (chunk.includes(PROMPT_MESSAGES.typescriptSucceed)) {
const tsConfigJson = getTsConfig(inputs.emptyDir);
expect(tsConfigJson).to.deep.equal({ extends: 'astro/tsconfigs/base' });
resolve();
}
});
},
() => lastStdout
);
});
});