Skip to content

Commit 2f37538

Browse files
tests: add test case for supplying relative path (#2082)
1 parent cd56712 commit 2f37538

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

test/plugin/plugin.test.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
const { existsSync } = require('fs');
1+
const { existsSync, mkdirSync } = require('fs');
22
const { join, resolve } = require('path');
33
const rimraf = require('rimraf');
44
const stripAnsi = require('strip-ansi');
55
const { run, runPromptWithAnswers } = require('../utils/test-utils');
6+
67
const ENTER = '\x0D';
8+
79
const firstPrompt = '? Plugin name';
810
const pluginName = 'test-plugin';
11+
912
const pluginPath = join(__dirname, pluginName);
10-
const customPluginPath = join(__dirname, 'test-assets', 'my-webpack-plugin');
13+
const genPath = join(__dirname, 'test-assets');
14+
const customPluginPath = join(genPath, pluginName);
1115

1216
describe('plugin command', () => {
13-
beforeAll(() => {
17+
beforeEach(() => {
1418
rimraf.sync(pluginPath);
15-
rimraf.sync(customPluginPath);
19+
rimraf.sync(genPath);
1620
});
1721

1822
it('should ask the plugin name when invoked', () => {
@@ -48,7 +52,7 @@ describe('plugin command', () => {
4852
});
4953

5054
it('should scaffold plugin template in the specified path', async () => {
51-
let { stdout } = await runPromptWithAnswers(__dirname, ['plugin', 'test-assets'], [ENTER]);
55+
let { stdout } = await runPromptWithAnswers(__dirname, ['plugin', 'test-assets'], [`${pluginName}${ENTER}`]);
5256

5357
expect(stripAnsi(stdout)).toContain(firstPrompt);
5458

@@ -68,7 +72,35 @@ describe('plugin command', () => {
6872
});
6973

7074
// Check if the the generated plugin works successfully
71-
stdout = run(__dirname, ['--config', './test-plugin/examples/simple/webpack.config.js'], false).stdout;
75+
stdout = run(customPluginPath, ['--config', './examples/simple/webpack.config.js'], false).stdout;
76+
expect(stdout).toContain('Hello World!');
77+
});
78+
79+
it('should scaffold plugin template in the current directory', async () => {
80+
// Create test-assets directory
81+
mkdirSync(genPath);
82+
83+
let { stdout } = await runPromptWithAnswers(genPath, ['plugin', './'], [`${pluginName}${ENTER}`]);
84+
85+
expect(stripAnsi(stdout)).toContain(firstPrompt);
86+
87+
// Check if the output directory exists with the appropriate plugin name
88+
expect(existsSync(customPluginPath)).toBeTruthy();
89+
90+
// Skip test in case installation fails
91+
if (!existsSync(resolve(customPluginPath, './yarn.lock'))) {
92+
return;
93+
}
94+
95+
// Test regressively files are scaffolded
96+
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
97+
98+
files.forEach((file) => {
99+
expect(existsSync(join(customPluginPath, file))).toBeTruthy();
100+
});
101+
102+
// Check if the the generated plugin works successfully
103+
stdout = run(customPluginPath, ['--config', './examples/simple/webpack.config.js'], false).stdout;
72104
expect(stdout).toContain('Hello World!');
73105
});
74106
});

0 commit comments

Comments
 (0)