Skip to content

Commit 2841cd7

Browse files
authored
test: default loader name and plugin name (#2392)
1 parent b50d71b commit 2841cd7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

test/loader/loader.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ const firstPrompt = '? Loader name (my-loader)';
1010
const ENTER = '\x0D';
1111
const loaderName = 'test-loader';
1212
const loaderPath = join(__dirname, loaderName);
13+
const defaultLoaderPath = join(__dirname, 'my-loader');
1314
const genPath = join(__dirname, 'test-assets');
1415
const customLoaderPath = join(genPath, loaderName);
1516

1617
describe('loader command', () => {
1718
beforeEach(() => {
19+
rimraf.sync(defaultLoaderPath);
1820
rimraf.sync(loaderPath);
1921
rimraf.sync(genPath);
2022
});
@@ -26,6 +28,32 @@ describe('loader command', () => {
2628
expect(stripAnsi(stdout)).toContain(firstPrompt);
2729
});
2830

31+
it('should scaffold loader with default name if no loader name provided', async () => {
32+
let { stdout } = await runPromptWithAnswers(__dirname, ['loader'], [`${ENTER}`]);
33+
34+
expect(stripAnsi(stdout)).toContain(firstPrompt);
35+
36+
// Skip test in case installation fails
37+
if (!existsSync(resolve(defaultLoaderPath, './yarn.lock'))) {
38+
return;
39+
}
40+
41+
// Check if the output directory exists with the appropriate loader name
42+
expect(existsSync(defaultLoaderPath)).toBeTruthy();
43+
44+
// All test files are scaffolded
45+
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
46+
47+
files.forEach((file) => {
48+
expect(existsSync(defaultLoaderPath, file)).toBeTruthy();
49+
});
50+
51+
// Check if the the generated loader works successfully
52+
const path = resolve(__dirname, './my-loader/examples/simple/');
53+
({ stdout } = run(path, [], false));
54+
expect(stdout).toContain('my-loader');
55+
});
56+
2957
it('should scaffold loader template with a given name', async () => {
3058
let { stdout } = await runPromptWithAnswers(__dirname, ['loader'], [`${loaderName}${ENTER}`]);
3159

test/plugin/plugin.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ const firstPrompt = '? Plugin name';
1010
const pluginName = 'test-plugin';
1111

1212
const pluginPath = join(__dirname, pluginName);
13+
const defaultPluginPath = join(__dirname, 'my-webpack-plugin');
1314
const genPath = join(__dirname, 'test-assets');
1415
const customPluginPath = join(genPath, pluginName);
1516

1617
describe('plugin command', () => {
1718
beforeEach(() => {
19+
rimraf.sync(defaultPluginPath);
1820
rimraf.sync(pluginPath);
1921
rimraf.sync(genPath);
2022
});
@@ -26,6 +28,31 @@ describe('plugin command', () => {
2628
expect(stripAnsi(stdout)).toContain(firstPrompt);
2729
});
2830

31+
it('should scaffold plugin with default name if no plugin name provided', async () => {
32+
let { stdout } = await runPromptWithAnswers(__dirname, ['plugin'], [`${ENTER}`]);
33+
34+
expect(stripAnsi(stdout)).toContain(firstPrompt);
35+
36+
// Check if the output directory exists with the appropriate plugin name
37+
expect(existsSync(defaultPluginPath)).toBeTruthy();
38+
39+
// Skip test in case installation fails
40+
if (!existsSync(resolve(defaultPluginPath, './yarn.lock'))) {
41+
return;
42+
}
43+
44+
// Test regressively files are scaffolded
45+
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
46+
47+
files.forEach((file) => {
48+
expect(existsSync(join(defaultPluginPath, file))).toBeTruthy();
49+
});
50+
51+
// Check if the the generated plugin works successfully
52+
stdout = run(__dirname, ['--config', './my-webpack-plugin/examples/simple/webpack.config.js'], false).stdout;
53+
expect(stdout).toContain('Hello World!');
54+
});
55+
2956
it('should scaffold plugin template with a given name', async () => {
3057
let { stdout } = await runPromptWithAnswers(__dirname, ['plugin'], [`${pluginName}${ENTER}`]);
3158

0 commit comments

Comments
 (0)