1
- const { existsSync } = require ( 'fs' ) ;
1
+ const { existsSync, mkdirSync } = require ( 'fs' ) ;
2
2
const { join, resolve } = require ( 'path' ) ;
3
3
const rimraf = require ( 'rimraf' ) ;
4
4
const stripAnsi = require ( 'strip-ansi' ) ;
5
5
const { run, runPromptWithAnswers } = require ( '../utils/test-utils' ) ;
6
+
6
7
const ENTER = '\x0D' ;
8
+
7
9
const firstPrompt = '? Plugin name' ;
8
10
const pluginName = 'test-plugin' ;
11
+
9
12
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 ) ;
11
15
12
16
describe ( 'plugin command' , ( ) => {
13
- beforeAll ( ( ) => {
17
+ beforeEach ( ( ) => {
14
18
rimraf . sync ( pluginPath ) ;
15
- rimraf . sync ( customPluginPath ) ;
19
+ rimraf . sync ( genPath ) ;
16
20
} ) ;
17
21
18
22
it ( 'should ask the plugin name when invoked' , ( ) => {
@@ -48,7 +52,7 @@ describe('plugin command', () => {
48
52
} ) ;
49
53
50
54
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 } ` ] ) ;
52
56
53
57
expect ( stripAnsi ( stdout ) ) . toContain ( firstPrompt ) ;
54
58
@@ -68,7 +72,35 @@ describe('plugin command', () => {
68
72
} ) ;
69
73
70
74
// 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 ;
72
104
expect ( stdout ) . toContain ( 'Hello World!' ) ;
73
105
} ) ;
74
106
} ) ;
0 commit comments