Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions test/plugin/plugin.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
const { existsSync } = require('fs');
const { existsSync, mkdirSync } = require('fs');
const { join, resolve } = require('path');
const rimraf = require('rimraf');
const stripAnsi = require('strip-ansi');
const { run, runPromptWithAnswers } = require('../utils/test-utils');

const ENTER = '\x0D';

const firstPrompt = '? Plugin name';
const pluginName = 'test-plugin';

const pluginPath = join(__dirname, pluginName);
const customPluginPath = join(__dirname, 'test-assets', 'my-webpack-plugin');
const genPath = join(__dirname, 'test-assets');
const customPluginPath = join(genPath, pluginName);

describe('plugin command', () => {
beforeAll(() => {
beforeEach(() => {
rimraf.sync(pluginPath);
rimraf.sync(customPluginPath);
rimraf.sync(genPath);
});

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

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

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

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

// Check if the the generated plugin works successfully
stdout = run(__dirname, ['--config', './test-plugin/examples/simple/webpack.config.js'], false).stdout;
stdout = run(customPluginPath, ['--config', './examples/simple/webpack.config.js'], false).stdout;
expect(stdout).toContain('Hello World!');
});

it('should scaffold plugin template in the current directory', async () => {
// Create test-assets directory
mkdirSync(genPath);

let { stdout } = await runPromptWithAnswers(genPath, ['plugin', './'], [`${pluginName}${ENTER}`]);

expect(stripAnsi(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
stdout = run(customPluginPath, ['--config', './examples/simple/webpack.config.js'], false).stdout;
expect(stdout).toContain('Hello World!');
});
});