1
1
'use strict' ;
2
2
3
- const { existsSync } = require ( 'fs' ) ;
3
+ const { existsSync, mkdirSync } = require ( 'fs' ) ;
4
4
const { join, resolve } = require ( 'path' ) ;
5
5
const rimraf = require ( 'rimraf' ) ;
6
6
const stripAnsi = require ( 'strip-ansi' ) ;
@@ -10,13 +10,16 @@ const firstPrompt = '? Loader name (my-loader)';
10
10
const ENTER = '\x0D' ;
11
11
const loaderName = 'test-loader' ;
12
12
const loaderPath = join ( __dirname , loaderName ) ;
13
+ const genPath = join ( __dirname , 'test-assets' ) ;
14
+ const customLoaderPath = join ( genPath , loaderName ) ;
13
15
14
16
describe ( 'loader command' , ( ) => {
15
- beforeAll ( ( ) => {
17
+ beforeEach ( ( ) => {
16
18
rimraf . sync ( loaderPath ) ;
19
+ rimraf . sync ( genPath ) ;
17
20
} ) ;
18
21
19
- it ( 'Should ask the loader name when invoked' , ( ) => {
22
+ it ( 'should ask the loader name when invoked' , ( ) => {
20
23
const { stdout, stderr } = run ( __dirname , [ 'loader' ] , false ) ;
21
24
expect ( stdout ) . toBeTruthy ( ) ;
22
25
expect ( stderr ) . toBeFalsy ( ) ;
@@ -33,19 +36,74 @@ describe('loader command', () => {
33
36
return ;
34
37
}
35
38
36
- // check if the output directory exists with the appropriate loader name
37
- expect ( existsSync ( join ( __dirname , loaderName ) ) ) . toBeTruthy ( ) ;
39
+ // Check if the output directory exists with the appropriate loader name
40
+ expect ( existsSync ( loaderPath ) ) . toBeTruthy ( ) ;
38
41
39
42
// All test files are scaffolded
40
43
const files = [ 'package.json' , 'examples' , 'src' , 'test' , 'src/index.js' , 'examples/simple/webpack.config.js' ] ;
41
44
42
45
files . forEach ( ( file ) => {
43
- expect ( existsSync ( join ( __dirname , ` ${ loaderName } / ${ file } ` ) ) ) . toBeTruthy ( ) ;
46
+ expect ( existsSync ( loaderPath , file ) ) . toBeTruthy ( ) ;
44
47
} ) ;
45
48
46
- //check if the the generated plugin works successfully
49
+ // Check if the the generated loader works successfully
47
50
const path = resolve ( __dirname , './test-loader/examples/simple/' ) ;
48
51
( { stdout } = run ( path , [ ] , false ) ) ;
49
52
expect ( stdout ) . toContain ( 'test-loader' ) ;
50
53
} ) ;
54
+
55
+ it ( 'should scaffold loader template in the specified path' , async ( ) => {
56
+ let { stdout } = await runPromptWithAnswers ( __dirname , [ 'loader' , 'test-assets' ] , [ `${ loaderName } ${ ENTER } ` ] ) ;
57
+
58
+ expect ( stripAnsi ( stdout ) ) . toContain ( firstPrompt ) ;
59
+
60
+ // Skip test in case installation fails
61
+ if ( ! existsSync ( resolve ( customLoaderPath , './yarn.lock' ) ) ) {
62
+ return ;
63
+ }
64
+
65
+ // Check if the output directory exists with the appropriate loader name
66
+ expect ( existsSync ( customLoaderPath ) ) . toBeTruthy ( ) ;
67
+
68
+ // All test files are scaffolded
69
+ const files = [ 'package.json' , 'examples' , 'src' , 'test' , 'src/index.js' , 'examples/simple/webpack.config.js' ] ;
70
+
71
+ files . forEach ( ( file ) => {
72
+ expect ( existsSync ( customLoaderPath , file ) ) . toBeTruthy ( ) ;
73
+ } ) ;
74
+
75
+ // Check if the the generated loader works successfully
76
+ const path = resolve ( customLoaderPath , './examples/simple/' ) ;
77
+ ( { stdout } = run ( path , [ ] , false ) ) ;
78
+ expect ( stdout ) . toContain ( 'test-loader' ) ;
79
+ } ) ;
80
+
81
+ it ( 'should scaffold loader template in the current directory' , async ( ) => {
82
+ // Create test-assets directory
83
+ mkdirSync ( genPath ) ;
84
+
85
+ let { stdout } = await runPromptWithAnswers ( genPath , [ 'loader' , './' ] , [ `${ loaderName } ${ ENTER } ` ] ) ;
86
+
87
+ expect ( stripAnsi ( stdout ) ) . toContain ( firstPrompt ) ;
88
+
89
+ // Skip test in case installation fails
90
+ if ( ! existsSync ( resolve ( customLoaderPath , './yarn.lock' ) ) ) {
91
+ return ;
92
+ }
93
+
94
+ // Check if the output directory exists with the appropriate loader name
95
+ expect ( existsSync ( customLoaderPath ) ) . toBeTruthy ( ) ;
96
+
97
+ // All test files are scaffolded
98
+ const files = [ 'package.json' , 'examples' , 'src' , 'test' , 'src/index.js' , 'examples/simple/webpack.config.js' ] ;
99
+
100
+ files . forEach ( ( file ) => {
101
+ expect ( existsSync ( customLoaderPath , file ) ) . toBeTruthy ( ) ;
102
+ } ) ;
103
+
104
+ // Check if the the generated loader works successfully
105
+ const path = resolve ( customLoaderPath , './examples/simple/' ) ;
106
+ ( { stdout } = run ( path , [ ] , false ) ) ;
107
+ expect ( stdout ) . toContain ( 'test-loader' ) ;
108
+ } ) ;
51
109
} ) ;
0 commit comments