@@ -2,10 +2,9 @@ import { SchematicContext, Tree } from '@angular-devkit/schematics';
2
2
3
3
import { ngAdd } from './ng-add' ;
4
4
5
- const PROJECT_NAME = 'pie-ka-chu' ;
6
- const PROJECT_ROOT = 'pirojok' ;
7
-
8
- const OTHER_PROJECT_NAME = 'pi-catch-you' ;
5
+ const PROJECT_NAME = 'THEPROJECT' ;
6
+ const PROJECT_ROOT = 'PROJECTROOT' ;
7
+ const OTHER_PROJECT_NAME = 'OTHERPROJECT' ;
9
8
10
9
describe ( 'ng-add' , ( ) => {
11
10
describe ( 'generating files' , ( ) => {
@@ -17,118 +16,124 @@ describe('ng-add', () => {
17
16
} ) ;
18
17
19
18
it ( 'generates new files if starting from scratch' , async ( ) => {
20
- const result = ngAdd ( {
19
+ const result = await ngAdd ( {
21
20
project : PROJECT_NAME
22
21
} ) ( tree , { } as SchematicContext ) ;
23
22
24
- expect ( result . read ( 'angular.json' ) ! . toString ( ) ) . toEqual (
25
- initialAngularJson
26
- ) ;
23
+ const actual = result . read ( 'angular.json' ) ! . toString ( ) ;
24
+ expect ( prettifyJSON ( actual ) ) . toMatchSnapshot ( ) ;
27
25
} ) ;
28
26
29
27
it ( 'overrides existing files' , async ( ) => {
30
- const tempTree = ngAdd ( {
28
+ const tempTree = await ngAdd ( {
31
29
project : PROJECT_NAME
32
30
} ) ( tree , { } as SchematicContext ) ;
33
31
34
- const result = ngAdd ( {
32
+ const result = await ngAdd ( {
35
33
project : OTHER_PROJECT_NAME
36
34
} ) ( tempTree , { } as SchematicContext ) ;
37
35
38
36
const actual = result . read ( 'angular.json' ) ! . toString ( ) ;
39
37
40
- expect ( actual ) . toEqual ( overwriteAngularJson ) ;
38
+ expect ( prettifyJSON ( actual ) ) . toMatchSnapshot ( ) ;
41
39
} ) ;
42
40
} ) ;
43
41
44
42
describe ( 'error handling' , ( ) => {
45
- it ( 'fails if project not defined' , ( ) => {
43
+ it ( 'should fail if project not defined' , async ( ) => {
46
44
const tree = Tree . empty ( ) ;
47
45
const angularJSON = generateAngularJson ( ) ;
48
46
delete angularJSON . defaultProject ;
49
47
tree . create ( 'angular.json' , JSON . stringify ( angularJSON ) ) ;
50
48
51
- expect ( ( ) =>
49
+ await expect (
52
50
ngAdd ( {
53
51
project : ''
54
52
} ) ( tree , { } as SchematicContext )
55
- ) . toThrowError (
53
+ ) . rejects . toThrowError (
56
54
'No Angular project selected and no default project in the workspace'
57
55
) ;
58
56
} ) ;
59
57
60
- it ( 'Should throw if angular.json not found' , async ( ) => {
61
- expect ( ( ) =>
58
+ it ( 'should throw if angular.json not found' , async ( ) => {
59
+ await expect (
62
60
ngAdd ( {
63
61
project : PROJECT_NAME
64
62
} ) ( Tree . empty ( ) , { } as SchematicContext )
65
- ) . toThrowError ( 'Could not find angular.json ' ) ;
63
+ ) . rejects . toThrowError ( 'Unable to determine format for workspace path. ' ) ;
66
64
} ) ;
67
65
68
- it ( 'Should throw if angular.json can not be parsed' , async ( ) => {
66
+ it ( 'should throw if angular.json can not be parsed' , async ( ) => {
69
67
const tree = Tree . empty ( ) ;
70
68
tree . create ( 'angular.json' , 'hi' ) ;
71
69
72
- expect ( ( ) =>
70
+ await expect (
73
71
ngAdd ( {
74
72
project : PROJECT_NAME
75
73
} ) ( tree , { } as SchematicContext )
76
- ) . toThrowError ( 'Could not parse angular.json ' ) ;
74
+ ) . rejects . toThrowError ( 'Invalid JSON character: "h" at 0:0. ' ) ;
77
75
} ) ;
78
76
79
- it ( 'Should throw if specified project does not exist ' , async ( ) => {
77
+ it ( 'should throw if specified project does not exist' , async ( ) => {
80
78
const tree = Tree . empty ( ) ;
81
- tree . create ( 'angular.json' , JSON . stringify ( { projects : { } } ) ) ;
79
+ tree . create ( 'angular.json' , JSON . stringify ( { version : 1 , projects : { } } ) ) ;
82
80
83
- expect ( ( ) =>
81
+ await expect (
84
82
ngAdd ( {
85
83
project : PROJECT_NAME
86
84
} ) ( tree , { } as SchematicContext )
87
- ) . toThrowError (
85
+ ) . rejects . toThrowError (
88
86
'The specified Angular project is not defined in this workspace'
89
87
) ;
90
88
} ) ;
91
89
92
- it ( 'Should throw if specified project is not application' , async ( ) => {
90
+ it ( 'should throw if specified project is not application' , async ( ) => {
93
91
const tree = Tree . empty ( ) ;
94
92
tree . create (
95
93
'angular.json' ,
96
94
JSON . stringify ( {
97
- projects : { [ PROJECT_NAME ] : { projectType : 'pokemon' } }
95
+ version : 1 ,
96
+ projects : { [ PROJECT_NAME ] : { projectType : 'invalid' } }
98
97
} )
99
98
) ;
100
99
101
- expect ( ( ) =>
100
+ await expect (
102
101
ngAdd ( {
103
102
project : PROJECT_NAME
104
103
} ) ( tree , { } as SchematicContext )
105
- ) . toThrowError (
104
+ ) . rejects . toThrowError (
106
105
'Deploy requires an Angular project type of "application" in angular.json'
107
106
) ;
108
107
} ) ;
109
108
110
- it ( 'Should throw if app does not have architect configured' , async ( ) => {
109
+ it ( 'should throw if app does not have architect configured' , async ( ) => {
111
110
const tree = Tree . empty ( ) ;
112
111
tree . create (
113
112
'angular.json' ,
114
113
JSON . stringify ( {
114
+ version : 1 ,
115
115
projects : { [ PROJECT_NAME ] : { projectType : 'application' } }
116
116
} )
117
117
) ;
118
118
119
- expect ( ( ) =>
119
+ await expect (
120
120
ngAdd ( {
121
121
project : PROJECT_NAME
122
122
} ) ( tree , { } as SchematicContext )
123
- ) . toThrowError (
124
- 'Cannot read the output path (architect.build.options.outputPath) of the Angular project "pie-ka-chu " in angular.json'
123
+ ) . rejects . toThrowError (
124
+ 'Cannot read the output path (architect.build.options.outputPath) of the Angular project "THEPROJECT " in angular.json'
125
125
) ;
126
126
} ) ;
127
127
} ) ;
128
128
} ) ;
129
129
130
+ function prettifyJSON ( json : string ) {
131
+ return JSON . stringify ( JSON . parse ( json ) , null , 2 ) ;
132
+ }
133
+
130
134
function generateAngularJson ( ) {
131
135
return {
136
+ version : 1 ,
132
137
defaultProject : PROJECT_NAME as string | undefined ,
133
138
projects : {
134
139
[ PROJECT_NAME ] : {
@@ -137,7 +142,7 @@ function generateAngularJson() {
137
142
architect : {
138
143
build : {
139
144
options : {
140
- outputPath : 'dist/ikachu'
145
+ outputPath : 'dist/' + PROJECT_NAME
141
146
}
142
147
}
143
148
}
@@ -148,79 +153,11 @@ function generateAngularJson() {
148
153
architect : {
149
154
build : {
150
155
options : {
151
- outputPath : 'dist/ikachu'
156
+ outputPath : 'dist/' + OTHER_PROJECT_NAME
152
157
}
153
158
}
154
159
}
155
160
}
156
161
}
157
162
} ;
158
163
}
159
-
160
- const initialAngularJson = `{
161
- "defaultProject": "pie-ka-chu",
162
- "projects": {
163
- "pie-ka-chu": {
164
- "projectType": "application",
165
- "root": "pirojok",
166
- "architect": {
167
- "build": {
168
- "options": {
169
- "outputPath": "dist/ikachu"
170
- }
171
- },
172
- "deploy": {
173
- \"builder\": \"angular-cli-ghpages:deploy\",
174
- "options": {}
175
- }
176
- }
177
- },
178
- "pi-catch-you": {
179
- "projectType": "application",
180
- "root": "pirojok",
181
- "architect": {
182
- "build": {
183
- "options": {
184
- "outputPath": "dist/ikachu"
185
- }
186
- }
187
- }
188
- }
189
- }
190
- }` ;
191
-
192
- const overwriteAngularJson = `{
193
- "defaultProject": "pie-ka-chu",
194
- "projects": {
195
- "pie-ka-chu": {
196
- "projectType": "application",
197
- "root": "pirojok",
198
- "architect": {
199
- "build": {
200
- "options": {
201
- "outputPath": "dist/ikachu"
202
- }
203
- },
204
- "deploy": {
205
- \"builder\": \"angular-cli-ghpages:deploy\",
206
- "options": {}
207
- }
208
- }
209
- },
210
- "pi-catch-you": {
211
- "projectType": "application",
212
- "root": "pirojok",
213
- "architect": {
214
- "build": {
215
- "options": {
216
- "outputPath": "dist/ikachu"
217
- }
218
- },
219
- "deploy": {
220
- "builder": "angular-cli-ghpages:deploy",
221
- "options": {}
222
- }
223
- }
224
- }
225
- }
226
- }` ;
0 commit comments