@@ -59,16 +59,28 @@ function getTargetedProjectOptions(project: WorkspaceProject<ProjectType>, targe
59
59
throw new SchematicsException ( `Cannot determine the project's configuration for: ${ target } ` ) ;
60
60
}
61
61
62
- export function getConfigFile ( project : WorkspaceProject < ProjectType > , option : string ) : string {
63
- const buildOptions = getTargetedProjectOptions ( project , 'build' ) ;
64
- if ( ! buildOptions [ option ] ) {
65
- throw new SchematicsException ( `Could not find the project ${ option } file inside of the ` +
66
- `workspace config (${ project . sourceRoot } )` ) ;
67
- }
62
+ export function getConfigFile ( project : WorkspaceProject < ProjectType > , option : string , configSection : string ) : string {
63
+ switch ( configSection ) {
64
+ case 'build' :
65
+ const buildOptions = getTargetedProjectOptions ( project , 'build' ) ;
66
+ if ( ! buildOptions [ option ] ) {
67
+ throw new SchematicsException ( `Could not find the project ${ option } file inside of the ` +
68
+ `workspace config ${ project . sourceRoot } ` ) ;
69
+ }
70
+ return buildOptions [ option ] ;
68
71
69
- return buildOptions [ option ] ;
72
+ case 'test' :
73
+ const testOptions = getTargetedProjectOptions ( project , 'test' ) ;
74
+ if ( ! testOptions [ option ] ) {
75
+ throw new SchematicsException ( `Could not find the project ${ option } file inside of the ` +
76
+ `workspace config ${ project . sourceRoot } ` ) ;
77
+ }
78
+ return testOptions [ option ] ;
79
+ default :
80
+ throw new SchematicsException ( `Could not find matching ${ configSection } section` +
81
+ `inside of the workspace config ${ project . sourceRoot } ` ) ;
82
+ }
70
83
}
71
-
72
84
export function overwriteJsonFile ( tree : Tree , targetFile : string , data : any ) {
73
85
tree . overwrite ( targetFile , JSON . stringify ( data , null , 2 ) + '\n' ) ;
74
86
}
@@ -148,15 +160,25 @@ function includeDependencies(pkgJson: any, context: SchematicContext, tree: Tree
148
160
const workspace = getWorkspace ( tree ) ;
149
161
const project = workspace . projects [ workspace . defaultProject ] ;
150
162
const projectOptions = getTargetedProjectOptions ( project , 'build' ) ;
151
- const mainTsPath = getConfigFile ( project , 'main' ) ;
163
+ const projectTestOptions = getTargetedProjectOptions ( project , 'test' ) ;
164
+ const mainTsPath = getConfigFile ( project , 'main' , 'build' ) ;
165
+ const testTsPath = getConfigFile ( project , 'main' , 'test' ) ;
152
166
const hammerImport = 'import \'hammerjs\';\n' ;
153
167
const mainTsContent = tree . read ( mainTsPath ) . toString ( ) ;
168
+ const testTsContent = tree . read ( testTsPath ) . toString ( ) ;
154
169
// if there are no elements in the architect.build.options.scripts array that contain hammerjs
155
170
// and main.ts does not contain an import with hammerjs
156
171
if ( ! projectOptions . scripts . some ( el => el . includes ( 'hammerjs' ) ) && ! mainTsContent . includes ( hammerImport ) ) {
157
172
// import hammerjs in the main.ts file
158
- const contents = hammerImport + mainTsContent ;
159
- tree . overwrite ( mainTsPath , contents ) ;
173
+ const mainContents = hammerImport + mainTsContent ;
174
+ tree . overwrite ( mainTsPath , mainContents ) ;
175
+ }
176
+ // make sure test.ts has hammerjs import even for projects already containing it in main.ts
177
+ // if there are no elements in the architect.test.options.scripts array that contain hammerjs
178
+ // and test.ts does not contain an import with hammerjs
179
+ if ( ! projectTestOptions . scripts . some ( el => el . includes ( 'hammerjs' ) ) && ! testTsContent . includes ( hammerImport ) ) {
180
+ const testContents = hammerImport + testTsContent ;
181
+ tree . overwrite ( testTsPath , testContents ) ;
160
182
}
161
183
break ;
162
184
default :
0 commit comments