@@ -2,10 +2,9 @@ import { SchematicContext, Tree } from '@angular-devkit/schematics';
22
33import { ngAdd } from './ng-add' ;
44
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' ;
98
109describe ( 'ng-add' , ( ) => {
1110 describe ( 'generating files' , ( ) => {
@@ -17,118 +16,124 @@ describe('ng-add', () => {
1716 } ) ;
1817
1918 it ( 'generates new files if starting from scratch' , async ( ) => {
20- const result = ngAdd ( {
19+ const result = await ngAdd ( {
2120 project : PROJECT_NAME
2221 } ) ( tree , { } as SchematicContext ) ;
2322
24- expect ( result . read ( 'angular.json' ) ! . toString ( ) ) . toEqual (
25- initialAngularJson
26- ) ;
23+ const actual = result . read ( 'angular.json' ) ! . toString ( ) ;
24+ expect ( prettifyJSON ( actual ) ) . toMatchSnapshot ( ) ;
2725 } ) ;
2826
2927 it ( 'overrides existing files' , async ( ) => {
30- const tempTree = ngAdd ( {
28+ const tempTree = await ngAdd ( {
3129 project : PROJECT_NAME
3230 } ) ( tree , { } as SchematicContext ) ;
3331
34- const result = ngAdd ( {
32+ const result = await ngAdd ( {
3533 project : OTHER_PROJECT_NAME
3634 } ) ( tempTree , { } as SchematicContext ) ;
3735
3836 const actual = result . read ( 'angular.json' ) ! . toString ( ) ;
3937
40- expect ( actual ) . toEqual ( overwriteAngularJson ) ;
38+ expect ( prettifyJSON ( actual ) ) . toMatchSnapshot ( ) ;
4139 } ) ;
4240 } ) ;
4341
4442 describe ( 'error handling' , ( ) => {
45- it ( 'fails if project not defined' , ( ) => {
43+ it ( 'should fail if project not defined' , async ( ) => {
4644 const tree = Tree . empty ( ) ;
4745 const angularJSON = generateAngularJson ( ) ;
4846 delete angularJSON . defaultProject ;
4947 tree . create ( 'angular.json' , JSON . stringify ( angularJSON ) ) ;
5048
51- expect ( ( ) =>
49+ await expect (
5250 ngAdd ( {
5351 project : ''
5452 } ) ( tree , { } as SchematicContext )
55- ) . toThrowError (
53+ ) . rejects . toThrowError (
5654 'No Angular project selected and no default project in the workspace'
5755 ) ;
5856 } ) ;
5957
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 (
6260 ngAdd ( {
6361 project : PROJECT_NAME
6462 } ) ( Tree . empty ( ) , { } as SchematicContext )
65- ) . toThrowError ( 'Could not find angular.json ' ) ;
63+ ) . rejects . toThrowError ( 'Unable to determine format for workspace path. ' ) ;
6664 } ) ;
6765
68- it ( 'Should throw if angular.json can not be parsed' , async ( ) => {
66+ it ( 'should throw if angular.json can not be parsed' , async ( ) => {
6967 const tree = Tree . empty ( ) ;
7068 tree . create ( 'angular.json' , 'hi' ) ;
7169
72- expect ( ( ) =>
70+ await expect (
7371 ngAdd ( {
7472 project : PROJECT_NAME
7573 } ) ( tree , { } as SchematicContext )
76- ) . toThrowError ( 'Could not parse angular.json ' ) ;
74+ ) . rejects . toThrowError ( 'Invalid JSON character: "h" at 0:0. ' ) ;
7775 } ) ;
7876
79- it ( 'Should throw if specified project does not exist ' , async ( ) => {
77+ it ( 'should throw if specified project does not exist' , async ( ) => {
8078 const tree = Tree . empty ( ) ;
81- tree . create ( 'angular.json' , JSON . stringify ( { projects : { } } ) ) ;
79+ tree . create ( 'angular.json' , JSON . stringify ( { version : 1 , projects : { } } ) ) ;
8280
83- expect ( ( ) =>
81+ await expect (
8482 ngAdd ( {
8583 project : PROJECT_NAME
8684 } ) ( tree , { } as SchematicContext )
87- ) . toThrowError (
85+ ) . rejects . toThrowError (
8886 'The specified Angular project is not defined in this workspace'
8987 ) ;
9088 } ) ;
9189
92- it ( 'Should throw if specified project is not application' , async ( ) => {
90+ it ( 'should throw if specified project is not application' , async ( ) => {
9391 const tree = Tree . empty ( ) ;
9492 tree . create (
9593 'angular.json' ,
9694 JSON . stringify ( {
97- projects : { [ PROJECT_NAME ] : { projectType : 'pokemon' } }
95+ version : 1 ,
96+ projects : { [ PROJECT_NAME ] : { projectType : 'invalid' } }
9897 } )
9998 ) ;
10099
101- expect ( ( ) =>
100+ await expect (
102101 ngAdd ( {
103102 project : PROJECT_NAME
104103 } ) ( tree , { } as SchematicContext )
105- ) . toThrowError (
104+ ) . rejects . toThrowError (
106105 'Deploy requires an Angular project type of "application" in angular.json'
107106 ) ;
108107 } ) ;
109108
110- it ( 'Should throw if app does not have architect configured' , async ( ) => {
109+ it ( 'should throw if app does not have architect configured' , async ( ) => {
111110 const tree = Tree . empty ( ) ;
112111 tree . create (
113112 'angular.json' ,
114113 JSON . stringify ( {
114+ version : 1 ,
115115 projects : { [ PROJECT_NAME ] : { projectType : 'application' } }
116116 } )
117117 ) ;
118118
119- expect ( ( ) =>
119+ await expect (
120120 ngAdd ( {
121121 project : PROJECT_NAME
122122 } ) ( 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'
125125 ) ;
126126 } ) ;
127127 } ) ;
128128} ) ;
129129
130+ function prettifyJSON ( json : string ) {
131+ return JSON . stringify ( JSON . parse ( json ) , null , 2 ) ;
132+ }
133+
130134function generateAngularJson ( ) {
131135 return {
136+ version : 1 ,
132137 defaultProject : PROJECT_NAME as string | undefined ,
133138 projects : {
134139 [ PROJECT_NAME ] : {
@@ -137,7 +142,7 @@ function generateAngularJson() {
137142 architect : {
138143 build : {
139144 options : {
140- outputPath : 'dist/ikachu'
145+ outputPath : 'dist/' + PROJECT_NAME
141146 }
142147 }
143148 }
@@ -148,79 +153,11 @@ function generateAngularJson() {
148153 architect : {
149154 build : {
150155 options : {
151- outputPath : 'dist/ikachu'
156+ outputPath : 'dist/' + OTHER_PROJECT_NAME
152157 }
153158 }
154159 }
155160 }
156161 }
157162 } ;
158163}
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-schule/ngx-deploy-starter: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-schule/ngx-deploy-starter: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-schule/ngx-deploy-starter:deploy",
221- "options": {}
222- }
223- }
224- }
225- }
226- }` ;
0 commit comments