Skip to content

Commit 5664610

Browse files
committed
test: rewrite test to include sub-app instead of lib
1 parent ff02433 commit 5664610

File tree

3 files changed

+87
-89
lines changed

3 files changed

+87
-89
lines changed

schematics/install/index.test.ts

Lines changed: 81 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ describe('Schematic Tests Nest Add', () => {
2929
nestTree = await createTestNest(runner);
3030
});
3131

32-
it('should handle nest add for default app', async () => {
32+
it('should add azure func for default setup', async () => {
3333
const options: Schema = {
34-
sourceRoot: 'src',
3534
skipInstall: true,
36-
rootDir: 'src',
3735
rootModuleFileName: 'app.module',
3836
rootModuleClassName: 'AppModule'
3937
};
@@ -68,20 +66,20 @@ describe('Schematic Tests Nest Add', () => {
6866
]);
6967
});
7068

71-
it('should handle nest add for project', async () => {
69+
it('should add azure-func for monorepo app', async () => {
70+
const projectName = 'azure-2';
7271
const options: Schema = {
73-
sourceRoot: '/libs/lib1/src',
7472
skipInstall: true,
75-
rootDir: 'src',
76-
project: 'lib1'
73+
project: projectName,
74+
sourceRoot: `apps/${projectName}/src`
7775
};
76+
7877
await runner
7978
.runExternalSchematicAsync(
8079
'@nestjs/schematics',
81-
'library',
80+
'sub-app',
8281
{
83-
name: 'lib1',
84-
prefix: '@app'
82+
name: projectName
8583
},
8684
nestTree
8785
)
@@ -90,6 +88,7 @@ describe('Schematic Tests Nest Add', () => {
9088
const tree = await runner
9189
.runSchematicAsync('nest-add', options, nestTree)
9290
.toPromise();
91+
9392
const files: string[] = tree.files;
9493
expect(files).toEqual([
9594
'/.eslintrc.js',
@@ -106,37 +105,41 @@ describe('Schematic Tests Nest Add', () => {
106105
'/src/main.ts',
107106
'/test/app.e2e-spec.ts',
108107
'/test/jest-e2e.json',
109-
'/libs/lib1/tsconfig.lib.json',
110-
'/libs/lib1/src/index.ts',
111-
'/libs/lib1/src/lib1.module.ts',
112-
'/libs/lib1/src/lib1.service.spec.ts',
113-
'/libs/lib1/src/lib1.service.ts',
114-
'/libs/lib1/src/.funcignore',
115-
'/libs/lib1/src/host.json',
116-
'/libs/lib1/src/local.settings.json',
117-
'/libs/lib1/src/main.azure.ts',
118-
'/libs/lib1/src/proxies.json',
119-
'/libs/lib1/src/webpack.config.js',
120-
'/libs/lib1/src/main/function.json',
121-
'/libs/lib1/src/main/index.ts',
122-
'/libs/lib1/src/main/sample.dat'
108+
'/apps/nestjs-azure-func-http/tsconfig.app.json',
109+
`/apps/${projectName}/tsconfig.app.json`,
110+
`/apps/${projectName}/src/app.controller.spec.ts`,
111+
`/apps/${projectName}/src/app.controller.ts`,
112+
`/apps/${projectName}/src/app.module.ts`,
113+
`/apps/${projectName}/src/app.service.ts`,
114+
`/apps/${projectName}/src/main.ts`,
115+
`/apps/${projectName}/src/.funcignore`,
116+
`/apps/${projectName}/src/host.json`,
117+
`/apps/${projectName}/src/local.settings.json`,
118+
`/apps/${projectName}/src/main.azure.ts`,
119+
`/apps/${projectName}/src/proxies.json`,
120+
`/apps/${projectName}/src/webpack.config.js`,
121+
`/apps/${projectName}/src/main/function.json`,
122+
`/apps/${projectName}/src/main/index.ts`,
123+
`/apps/${projectName}/src/main/sample.dat`,
124+
`/apps/${projectName}/test/app.e2e-spec.ts`,
125+
`/apps/${projectName}/test/jest-e2e.json`
123126
]);
124127
});
125128

126-
it('should have a nest-cli.json for project', async () => {
129+
it('should have a nest-cli.json for monorepo app', async () => {
130+
const projectName = 'azure-2';
127131
const options: Schema = {
128-
sourceRoot: '/libs/lib1/src',
129132
skipInstall: true,
130-
rootDir: 'src',
131-
project: 'lib1'
133+
project: projectName,
134+
sourceRoot: `apps/${projectName}/src`
132135
};
136+
133137
await runner
134138
.runExternalSchematicAsync(
135139
'@nestjs/schematics',
136-
'library',
140+
'sub-app',
137141
{
138-
name: 'lib1',
139-
prefix: '@app'
142+
name: projectName
140143
},
141144
nestTree
142145
)
@@ -147,10 +150,12 @@ describe('Schematic Tests Nest Add', () => {
147150
.toPromise();
148151
const fileContent = getFileContent(tree, '/nest-cli.json');
149152
const parsedFile = JSON.parse(fileContent);
150-
expect(parsedFile.projects.lib1.sourceRoot).toEqual('libs/lib1/src');
153+
expect(parsedFile.projects[projectName].sourceRoot).toEqual(
154+
`apps/${projectName}/src`
155+
);
151156
});
152157

153-
it('should a nest-cli.json for default app', async () => {
158+
it('should have a nest-cli.json for default app', async () => {
154159
const options: Schema = {
155160
sourceRoot: 'src',
156161
skipInstall: true,
@@ -167,20 +172,20 @@ describe('Schematic Tests Nest Add', () => {
167172
expect(fileContent).toContain(`"sourceRoot": "src"`);
168173
});
169174

170-
it('should import the app.module int main azure file for project', async () => {
175+
it('should import the app.module int main azure file for monorepo app', async () => {
176+
const projectName = 'azure-2';
171177
const options: Schema = {
172-
sourceRoot: '/libs/lib1/src',
173178
skipInstall: true,
174-
rootDir: 'src',
175-
project: 'lib1'
179+
project: projectName,
180+
sourceRoot: `apps/${projectName}/src`
176181
};
182+
177183
await runner
178184
.runExternalSchematicAsync(
179185
'@nestjs/schematics',
180-
'library',
186+
'sub-app',
181187
{
182-
name: 'lib1',
183-
prefix: '@app'
188+
name: projectName
184189
},
185190
nestTree
186191
)
@@ -189,7 +194,10 @@ describe('Schematic Tests Nest Add', () => {
189194
const tree = await runner
190195
.runSchematicAsync('nest-add', options, nestTree)
191196
.toPromise();
192-
const fileContent = getFileContent(tree, '/libs/lib1/src/main.azure.ts');
197+
const fileContent = getFileContent(
198+
tree,
199+
`/apps/${projectName}/src/main.azure.ts`
200+
);
193201

194202
expect(fileContent).toContain(`import { AppModule } from './app.module';`);
195203
});
@@ -212,20 +220,20 @@ describe('Schematic Tests Nest Add', () => {
212220
expect(fileContent).toContain(`import { AppModule } from './app.module';`);
213221
});
214222

215-
it('should have the root dir for index file in main azure dir for project', async () => {
223+
it('should have the root dir for index file in main azure dir for monorepo app', async () => {
224+
const projectName = 'azure-2';
216225
const options: Schema = {
217-
sourceRoot: '/libs/lib1/src',
218226
skipInstall: true,
219-
rootDir: 'src',
220-
project: 'lib1'
227+
project: projectName,
228+
sourceRoot: `apps/${projectName}/src`
221229
};
230+
222231
await runner
223232
.runExternalSchematicAsync(
224233
'@nestjs/schematics',
225-
'library',
234+
'sub-app',
226235
{
227-
name: 'lib1',
228-
prefix: '@app'
236+
name: projectName
229237
},
230238
nestTree
231239
)
@@ -234,7 +242,10 @@ describe('Schematic Tests Nest Add', () => {
234242
const tree = await runner
235243
.runSchematicAsync('nest-add', options, nestTree)
236244
.toPromise();
237-
const fileContent = getFileContent(tree, '/libs/lib1/src/main/index.ts');
245+
const fileContent = getFileContent(
246+
tree,
247+
`/apps/${projectName}/src/main/index.ts`
248+
);
238249

239250
expect(fileContent).toContain(`import { createApp } from '../main.azure';`);
240251
});
@@ -259,34 +270,35 @@ describe('Schematic Tests Nest Add', () => {
259270
);
260271
});
261272

262-
it('should import the webpack config for a project', async () => {
273+
it('should import the webpack config for monorepo app', async () => {
274+
const projectName = 'azure-2';
263275
const options: Schema = {
264-
sourceRoot: '/libs/lib1/src',
265276
skipInstall: true,
266-
rootDir: 'src',
267-
project: 'lib1'
277+
project: projectName,
278+
sourceRoot: `apps/${projectName}/src`
268279
};
280+
269281
await runner
270282
.runExternalSchematicAsync(
271283
'@nestjs/schematics',
272-
'library',
284+
'sub-app',
273285
{
274-
name: 'lib1',
275-
prefix: '@app'
286+
name: projectName
276287
},
277288
nestTree
278289
)
279290
.toPromise();
280-
281291
const tree = await runner
282292
.runSchematicAsync('nest-add', options, nestTree)
283293
.toPromise();
284294

285295
const fileContent = getFileContent(
286296
tree,
287-
'/libs/lib1/src/webpack.config.js'
297+
`/apps/${projectName}/src/webpack.config.js`
298+
);
299+
expect(fileContent).toContain(
300+
`filename: 'apps/${projectName}/main/index.js'`
288301
);
289-
expect(fileContent).toContain(`filename: 'apps/lib1/main/index.js'`);
290302
});
291303

292304
it('should not import the webpack config for a default app', async () => {
@@ -307,48 +319,35 @@ describe('Schematic Tests Nest Add', () => {
307319
expect(fileContent).toBeNull();
308320
});
309321

310-
it('should add a custom webpack config to the compilerOptions for a project', async () => {
322+
it('should add a custom webpack config to the compilerOptions for monorepo app', async () => {
323+
const projectName = 'azure-2';
311324
const options: Schema = {
312-
sourceRoot: '/apps/azure-func-http/src',
313325
skipInstall: true,
314-
rootDir: '',
315-
project: 'azure-func-http'
326+
project: projectName,
327+
sourceRoot: `apps/${projectName}/src`
316328
};
317-
// await runner
318-
// .runExternalSchematicAsync(
319-
// '@nestjs/schematics',
320-
// 'application',
321-
// {
322-
// name: 'azure-func-http',
323-
// prefix: '@app'
324-
// },
325-
// nestTree
326-
// )
327-
// .toPromise();
328329

329330
await runner
330331
.runExternalSchematicAsync(
331332
'@nestjs/schematics',
332333
'sub-app',
333334
{
334-
name: 'azure-func-http'
335+
name: projectName
335336
},
336337
nestTree
337338
)
338339
.toPromise();
339-
340340
const tree = await runner
341341
.runSchematicAsync('nest-add', options, nestTree)
342342
.toPromise();
343343

344344
const fileContent = getFileContent(tree, 'nest-cli.json');
345345
const parsedFile = JSON.parse(fileContent);
346-
347-
const compilerOptions = parsedFile.projects.lib1.compilerOptions;
348-
expect(compilerOptions).toContain({
349-
webpack: 'true',
350-
webpackConfigPath: 'apps/lib1/src/webpack.config.js',
351-
tsConfigPath: 'libs/lib1/tsconfig.lib.json'
346+
const compilerOptions = parsedFile.projects[projectName].compilerOptions;
347+
expect(compilerOptions).toEqual({
348+
tsConfigPath: `apps/${projectName}/tsconfig.app.json`,
349+
webpack: true,
350+
webpackConfigPath: `apps/${projectName}/src/webpack.config.js`
352351
});
353352
});
354353

schematics/install/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from '@schematics/angular/utility/dependencies';
1818
import { Schema as AzureOptions } from './schema';
1919
type UpdateJsonFn<T> = (obj: T) => T | void;
20-
20+
const DEFAULT_PATH_NAME = 'src';
2121
function addDependenciesAndScripts(): Rule {
2222
return (host: Tree) => {
2323
addPackageJsonDependency(host, {
@@ -60,7 +60,6 @@ export default function(options: AzureOptions): Rule {
6060
context.addTask(new NodePackageInstallTask());
6161
}
6262
const projectName = options.project;
63-
console.log(projectName);
6463
if (projectName) {
6564
let nestCliFileExists = host.exists('nest-cli.json');
6665

@@ -69,7 +68,6 @@ export default function(options: AzureOptions): Rule {
6968
host,
7069
'nest-cli.json',
7170
(optionsFile: Record<string, any>) => {
72-
console.log(optionsFile);
7371
if (optionsFile.projects[projectName].compilerOptions) {
7472
optionsFile.projects[projectName].compilerOptions = {
7573
...optionsFile.projects[projectName].compilerOptions,
@@ -83,14 +81,15 @@ export default function(options: AzureOptions): Rule {
8381
);
8482
}
8583
}
86-
84+
const defaultSourceRoot =
85+
options.sourceRoot !== undefined ? options.sourceRoot : DEFAULT_PATH_NAME;
8786
const rootSource = apply(
8887
options.project ? url('./files/project') : url('./files/root'),
8988
[
9089
template({
9190
...strings,
92-
...(options as object),
93-
rootDir: options.sourceRoot,
91+
...(options as AzureOptions),
92+
rootDir: defaultSourceRoot,
9493
getRootDirectory: () => options.sourceRoot,
9594
stripTsExtension: (s: string) => s.replace(/\.ts$/, ''),
9695
getRootModuleName: () => options.rootModuleClassName,

schematics/install/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface Schema {
1818
/**
1919
* .
2020
*/
21-
sourceRoot: string;
21+
sourceRoot?: string;
2222
/**
2323
* The project where generate the azure files.
2424
*/

0 commit comments

Comments
 (0)