/
plugin.test.js
143 lines (108 loc) · 6.1 KB
/
plugin.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
142
143
const { existsSync, mkdirSync } = require('fs');
const { join, resolve } = require('path');
const { run, runPromptWithAnswers, uniqueDirectoryForTest, normalizeStdout } = require('../utils/test-utils');
const ENTER = '\x0D';
const firstPrompt = '? Plugin name';
const dataForTests = (rootAssetsPath) => ({
pluginName: 'test-plugin',
pluginPath: join(rootAssetsPath, 'test-plugin'),
defaultPluginPath: join(rootAssetsPath, 'my-webpack-plugin'),
genPath: join(rootAssetsPath, 'test-assets'),
customPluginPath: join(rootAssetsPath, 'test-assets', 'test-plugin'),
});
describe('plugin command', () => {
it('should ask the plugin name when invoked', async () => {
const { stdout, stderr } = await runPromptWithAnswers(__dirname, ['plugin']);
expect(stdout).toBeTruthy();
expect(stderr).toBeFalsy();
expect(normalizeStdout(stdout)).toContain(firstPrompt);
});
it('should scaffold plugin with default name if no plugin name provided', async () => {
const assetsPath = await uniqueDirectoryForTest();
const { defaultPluginPath } = dataForTests(assetsPath);
const { stdout } = await runPromptWithAnswers(assetsPath, ['plugin'], [`${ENTER}`]);
expect(normalizeStdout(stdout)).toContain(firstPrompt);
// Check if the output directory exists with the appropriate plugin name
expect(existsSync(defaultPluginPath)).toBeTruthy();
// Skip test in case installation fails
if (!existsSync(resolve(defaultPluginPath, './yarn.lock'))) {
return;
}
// Test regressively files are scaffolded
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
files.forEach((file) => {
expect(existsSync(join(defaultPluginPath, file))).toBeTruthy();
});
// Check if the the generated plugin works successfully
const { stdout: stdout2 } = await run(__dirname, ['--config', './my-webpack-plugin/examples/simple/webpack.config.js'], false);
expect(normalizeStdout(stdout2)).toContain('Hello World!');
});
it('should scaffold plugin template with a given name', async () => {
const assetsPath = await uniqueDirectoryForTest();
const { pluginName, pluginPath } = dataForTests(assetsPath);
const { stdout } = await runPromptWithAnswers(assetsPath, ['plugin'], [`${pluginName}${ENTER}`]);
expect(normalizeStdout(stdout)).toContain(firstPrompt);
// Check if the output directory exists with the appropriate plugin name
expect(existsSync(pluginPath)).toBeTruthy();
// Skip test in case installation fails
if (!existsSync(resolve(pluginPath, './yarn.lock'))) {
return;
}
// Test regressively files are scaffolded
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
files.forEach((file) => {
expect(existsSync(join(pluginPath, file))).toBeTruthy();
});
// Check if the the generated plugin works successfully
const { stdout: stdout2 } = await run(__dirname, ['--config', './test-plugin/examples/simple/webpack.config.js'], false);
expect(normalizeStdout(stdout2)).toContain('Hello World!');
});
it('should scaffold plugin template in the specified path', async () => {
const assetsPath = await uniqueDirectoryForTest();
const { pluginName, customPluginPath } = dataForTests(assetsPath);
const { stdout } = await runPromptWithAnswers(assetsPath, ['plugin', 'test-assets'], [`${pluginName}${ENTER}`]);
expect(normalizeStdout(stdout)).toContain(firstPrompt);
// Check if the output directory exists with the appropriate plugin name
expect(existsSync(customPluginPath)).toBeTruthy();
// Skip test in case installation fails
if (!existsSync(resolve(customPluginPath, './yarn.lock'))) {
return;
}
// Test regressively files are scaffolded
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
files.forEach((file) => {
expect(existsSync(join(customPluginPath, file))).toBeTruthy();
});
// Check if the the generated plugin works successfully
const { stdout: stdout2 } = await run(customPluginPath, ['--config', './examples/simple/webpack.config.js'], false);
expect(normalizeStdout(stdout2)).toContain('Hello World!');
});
it('should scaffold plugin template in the current directory', async () => {
const assetsPath = await uniqueDirectoryForTest();
const { genPath, customPluginPath, pluginName } = dataForTests(assetsPath);
if (!existsSync(genPath)) {
mkdirSync(genPath);
}
let { stdout } = await runPromptWithAnswers(genPath, ['plugin', './'], [`${pluginName}${ENTER}`]);
expect(normalizeStdout(stdout)).toContain(firstPrompt);
// Check if the output directory exists with the appropriate plugin name
expect(existsSync(customPluginPath)).toBeTruthy();
// Skip test in case installation fails
if (!existsSync(resolve(customPluginPath, './yarn.lock'))) {
return;
}
// Test regressively files are scaffolded
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
files.forEach((file) => {
expect(existsSync(join(customPluginPath, file))).toBeTruthy();
});
// Check if the the generated plugin works successfully
const { stdout: stdout2 } = await run(customPluginPath, ['--config', './examples/simple/webpack.config.js'], false);
expect(normalizeStdout(stdout2)).toContain('Hello World!');
});
it('should prompt on supplying an invalid template', async () => {
const assetsPath = await uniqueDirectoryForTest();
const { stdout } = await runPromptWithAnswers(assetsPath, ['plugin', '--template=unknown']);
expect(stdout).toContain('unknown is not a valid template');
});
});