-
-
Notifications
You must be signed in to change notification settings - Fork 591
/
loader.test.js
154 lines (111 loc) · 5.9 KB
/
loader.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
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const { existsSync } = require('fs');
const { join, resolve } = require('path');
const { run, runPromptWithAnswers, uniqueDirectoryForTest, normalizeStdout } = require('../utils/test-utils');
const firstPrompt = '? Loader name (my-loader)';
const ENTER = '\x0D';
const dataForTests = (rootAssetsPath) => ({
loaderName: 'test-loader',
loaderPath: join(rootAssetsPath, 'test-loader'),
defaultLoaderPath: join(rootAssetsPath, 'my-loader'),
genPath: join(rootAssetsPath, 'test-assets'),
customLoaderPath: join(rootAssetsPath, 'test-assets', 'loaderName'),
});
describe('loader command', () => {
it('should ask the loader name when invoked', async () => {
const assetsPath = await uniqueDirectoryForTest();
const { stdout, stderr } = await runPromptWithAnswers(assetsPath, ['loader']);
expect(stdout).toBeTruthy();
expect(stderr).toBeFalsy();
expect(normalizeStdout(stdout)).toContain(firstPrompt);
});
it('should scaffold loader with default name if no loader name provided', async () => {
const assetsPath = await uniqueDirectoryForTest();
const { defaultLoaderPath } = dataForTests(assetsPath);
let { stdout } = await runPromptWithAnswers(assetsPath, ['loader'], [`${ENTER}`]);
expect(normalizeStdout(stdout)).toContain(firstPrompt);
// Skip test in case installation fails
if (!existsSync(resolve(defaultLoaderPath, './yarn.lock'))) {
return;
}
// Check if the output directory exists with the appropriate loader name
expect(existsSync(defaultLoaderPath)).toBeTruthy();
// All test files are scaffolded
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
files.forEach((file) => {
expect(existsSync(defaultLoaderPath, file)).toBeTruthy();
});
// Check if the the generated loader works successfully
const path = resolve(__dirname, './my-loader/examples/simple/');
({ stdout } = await run(path, []));
expect(stdout).toContain('my-loader');
});
it('should scaffold loader template with a given name', async () => {
const assetsPath = await uniqueDirectoryForTest();
const { loaderName, loaderPath } = dataForTests(assetsPath);
let { stdout } = await runPromptWithAnswers(assetsPath, ['loader'], [`${loaderName}${ENTER}`]);
expect(normalizeStdout(stdout)).toContain(firstPrompt);
// Skip test in case installation fails
if (!existsSync(resolve(loaderPath, './yarn.lock'))) {
return;
}
// Check if the output directory exists with the appropriate loader name
expect(existsSync(loaderPath)).toBeTruthy();
// All test files are scaffolded
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
files.forEach((file) => {
expect(existsSync(loaderPath, file)).toBeTruthy();
});
// Check if the the generated loader works successfully
const path = resolve(__dirname, './test-loader/examples/simple/');
({ stdout } = await run(path, []));
expect(stdout).toContain('test-loader');
});
it('should scaffold loader template in the specified path', async () => {
const assetsPath = await uniqueDirectoryForTest();
const { loaderName, customLoaderPath } = dataForTests(assetsPath);
let { stdout } = await runPromptWithAnswers(assetsPath, ['loader', 'test-assets'], [`${loaderName}${ENTER}`]);
expect(normalizeStdout(stdout)).toContain(firstPrompt);
// Skip test in case installation fails
if (!existsSync(resolve(customLoaderPath, './yarn.lock'))) {
return;
}
// Check if the output directory exists with the appropriate loader name
expect(existsSync(customLoaderPath)).toBeTruthy();
// All test files are scaffolded
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
files.forEach((file) => {
expect(existsSync(customLoaderPath, file)).toBeTruthy();
});
// Check if the the generated loader works successfully
const path = resolve(customLoaderPath, './examples/simple/');
({ stdout } = await run(path, []));
expect(stdout).toContain('test-loader');
});
it('should scaffold loader template in the current directory', async () => {
const assetsPath = await uniqueDirectoryForTest();
const { loaderName, customLoaderPath } = dataForTests(assetsPath);
let { stdout } = await runPromptWithAnswers(assetsPath, ['loader', './'], [`${loaderName}${ENTER}`]);
expect(normalizeStdout(stdout)).toContain(firstPrompt);
// Skip test in case installation fails
if (!existsSync(resolve(customLoaderPath, './yarn.lock'))) {
return;
}
// Check if the output directory exists with the appropriate loader name
expect(existsSync(customLoaderPath)).toBeTruthy();
// All test files are scaffolded
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
files.forEach((file) => {
expect(existsSync(customLoaderPath, file)).toBeTruthy();
});
// Check if the the generated loader works successfully
const path = resolve(customLoaderPath, './examples/simple/');
({ stdout } = await run(path, []));
expect(stdout).toContain('test-loader');
});
it('should prompt on supplying an invalid template', async () => {
const assetsPath = await uniqueDirectoryForTest();
const { stdout } = await runPromptWithAnswers(assetsPath, ['loader', '--template=unknown']);
expect(stdout).toContain('unknown is not a valid template');
});
});