diff --git a/src/lib/application/application.factory.test.ts b/src/lib/application/application.factory.test.ts index e88626f10..1a99cc891 100644 --- a/src/lib/application/application.factory.test.ts +++ b/src/lib/application/application.factory.test.ts @@ -19,7 +19,7 @@ describe('Application Factory', () => { .runSchematicAsync('application', options) .toPromise(); const files: string[] = tree.files; - expect(files).toEqual([ + expect(files.sort()).toEqual([ '/project/.eslintrc.js', '/project/.gitignore', '/project/.prettierrc', @@ -35,7 +35,7 @@ describe('Application Factory', () => { '/project/src/main.ts', '/project/test/app.e2e-spec.ts', '/project/test/jest-e2e.json', - ]); + ].sort()); expect( JSON.parse(tree.readContent('/project/package.json')), @@ -51,7 +51,7 @@ describe('Application Factory', () => { .runSchematicAsync('application', options) .toPromise(); const files: string[] = tree.files; - expect(files).toEqual([ + expect(files.sort()).toEqual([ `/project.foo.bar/.eslintrc.js`, `/project.foo.bar/.gitignore`, `/project.foo.bar/.prettierrc`, @@ -67,7 +67,7 @@ describe('Application Factory', () => { `/project.foo.bar/src/main.ts`, `/project.foo.bar/test/app.e2e-spec.ts`, `/project.foo.bar/test/jest-e2e.json`, - ]); + ].sort()); expect( JSON.parse(tree.readContent('/project.foo.bar/package.json')), @@ -83,7 +83,7 @@ describe('Application Factory', () => { .runSchematicAsync('application', options) .toPromise(); const files: string[] = tree.files; - expect(files).toEqual([ + expect(files.sort()).toEqual([ '/awesome-project/.eslintrc.js', '/awesome-project/.gitignore', '/awesome-project/.prettierrc', @@ -99,7 +99,7 @@ describe('Application Factory', () => { '/awesome-project/src/main.ts', '/awesome-project/test/app.e2e-spec.ts', '/awesome-project/test/jest-e2e.json', - ]); + ].sort()); expect( JSON.parse(tree.readContent('/awesome-project/package.json')), @@ -115,7 +115,7 @@ describe('Application Factory', () => { .runSchematicAsync('application', options) .toPromise(); const files: string[] = tree.files; - expect(files).toEqual([ + expect(files.sort()).toEqual([ '/_awesome-project/.eslintrc.js', '/_awesome-project/.gitignore', '/_awesome-project/.prettierrc', @@ -131,7 +131,7 @@ describe('Application Factory', () => { '/_awesome-project/src/main.ts', '/_awesome-project/test/app.e2e-spec.ts', '/_awesome-project/test/jest-e2e.json', - ]); + ].sort()); expect( JSON.parse(tree.readContent('/_awesome-project/package.json')), @@ -147,7 +147,7 @@ describe('Application Factory', () => { .runSchematicAsync('application', options) .toPromise(); const files: string[] = tree.files; - expect(files).toEqual([ + expect(files.sort()).toEqual([ '/@/package/.eslintrc.js', '/@/package/.gitignore', '/@/package/.prettierrc', @@ -163,7 +163,7 @@ describe('Application Factory', () => { '/@/package/src/main.ts', '/@/package/test/app.e2e-spec.ts', '/@/package/test/jest-e2e.json', - ]); + ].sort()); expect( JSON.parse(tree.readContent('/@/package/package.json')), @@ -179,7 +179,7 @@ describe('Application Factory', () => { .runSchematicAsync('application', options) .toPromise(); const files: string[] = tree.files; - expect(files).toEqual([ + expect(files.sort()).toEqual([ '/.eslintrc.js', '/.gitignore', '/.prettierrc', @@ -195,7 +195,7 @@ describe('Application Factory', () => { '/src/main.ts', '/test/app.e2e-spec.ts', '/test/jest-e2e.json', - ]); + ].sort()); expect(JSON.parse(tree.readContent('/package.json'))).toMatchObject({ name: path.basename(process.cwd()), @@ -211,7 +211,7 @@ describe('Application Factory', () => { .runSchematicAsync('application', options) .toPromise(); const files: string[] = tree.files; - expect(files).toEqual([ + expect(files.sort()).toEqual([ '/@scope/package/.eslintrc.js', '/@scope/package/.gitignore', '/@scope/package/.prettierrc', @@ -227,7 +227,7 @@ describe('Application Factory', () => { '/@scope/package/src/main.ts', '/@scope/package/test/app.e2e-spec.ts', '/@scope/package/test/jest-e2e.json', - ]); + ].sort()); expect( JSON.parse(tree.readContent('/@scope/package/package.json')), @@ -243,7 +243,7 @@ describe('Application Factory', () => { .runSchematicAsync('application', options) .toPromise(); const files: string[] = tree.files; - expect(files).toEqual([ + expect(files.sort()).toEqual([ '/@-/package/.eslintrc.js', '/@-/package/.gitignore', '/@-/package/.prettierrc', @@ -259,7 +259,7 @@ describe('Application Factory', () => { '/@-/package/src/main.ts', '/@-/package/test/app.e2e-spec.ts', '/@-/package/test/jest-e2e.json', - ]); + ].sort()); expect( JSON.parse(tree.readContent('/@-/package/package.json')), @@ -278,7 +278,7 @@ describe('Application Factory', () => { .runSchematicAsync('application', options) .toPromise(); const files: string[] = tree.files; - expect(files).toEqual([ + expect(files.sort()).toEqual([ '/123/.eslintrc.js', '/123/.gitignore', '/123/.prettierrc', @@ -294,7 +294,7 @@ describe('Application Factory', () => { '/123/src/main.ts', '/123/test/app.e2e-spec.ts', '/123/test/jest-e2e.json', - ]); + ].sort()); expect( JSON.parse(tree.readContent('/123/package.json')), @@ -311,7 +311,7 @@ describe('Application Factory', () => { .runSchematicAsync('application', options) .toPromise(); const files: string[] = tree.files; - expect(files).toEqual([ + expect(files.sort()).toEqual([ '/project/.babelrc', '/project/.gitignore', '/project/.prettierrc', @@ -328,7 +328,7 @@ describe('Application Factory', () => { '/project/src/main.js', '/project/test/app.e2e-spec.js', '/project/test/jest-e2e.json', - ]); + ].sort()); expect(JSON.parse(tree.readContent('/project/package.json'))).toMatchObject( { @@ -345,7 +345,7 @@ describe('Application Factory', () => { .runSchematicAsync('application', options) .toPromise(); const files: string[] = tree.files; - expect(files).toEqual([ + expect(files.sort()).toEqual([ '/app/.eslintrc.js', '/app/.gitignore', '/app/.prettierrc', @@ -361,10 +361,94 @@ describe('Application Factory', () => { '/app/src/main.ts', '/app/test/app.e2e-spec.ts', '/app/test/jest-e2e.json', - ]); + ].sort()); expect(JSON.parse(tree.readContent('/app/package.json'))).toMatchObject({ name: 'project', }); }); + it('should not create a spec file', async () => { + const options: ApplicationOptions = { + name: 'project', + spec: false, + language: 'js', + }; + const tree: UnitTestTree = await runner + .runSchematicAsync('application', options) + .toPromise(); + const files: string[] = tree.files; + expect(files.sort()).toEqual([ + '/project/.babelrc', + '/project/.gitignore', + '/project/.prettierrc', + '/project/README.md', + '/project/index.js', + '/project/jsconfig.json', + '/project/nest-cli.json', + '/project/nodemon.json', + '/project/package.json', + '/project/src/app.controller.js', + '/project/src/app.module.js', + '/project/src/app.service.js', + '/project/src/main.js', + '/project/test/jest-e2e.json', + ].sort()); + }); + it('should create a spec file', async () => { + const options: ApplicationOptions = { + name: 'project', + spec: true, + language: 'js', + }; + const tree: UnitTestTree = await runner + .runSchematicAsync('application', options) + .toPromise(); + const files: string[] = tree.files; + expect(files.sort()).toEqual([ + '/project/.babelrc', + '/project/.gitignore', + '/project/.prettierrc', + '/project/README.md', + '/project/index.js', + '/project/jsconfig.json', + '/project/nest-cli.json', + '/project/nodemon.json', + '/project/package.json', + '/project/src/app.controller.js', + '/project/src/app.controller.spec.js', + '/project/src/app.module.js', + '/project/src/app.service.js', + '/project/src/main.js', + '/project/test/app.e2e-spec.js', + '/project/test/jest-e2e.json', + ].sort()); + }); + it('should create a spec file with custom file suffix', async () => { + const options: ApplicationOptions = { + name: 'project', + spec: true, + specFileSuffix: 'test', + }; + const tree: UnitTestTree = await runner + .runSchematicAsync('application', options) + .toPromise(); + const files: string[] = tree.files; + expect(files.sort()).toEqual([ + '/project/.eslintrc.js', + '/project/.gitignore', + '/project/.prettierrc', + '/project/README.md', + '/project/nest-cli.json', + '/project/package.json', + '/project/tsconfig.build.json', + '/project/tsconfig.json', + '/project/src/app.controller.test.ts', + '/project/src/app.controller.ts', + '/project/src/app.module.ts', + '/project/src/app.service.ts', + '/project/src/main.ts', + '/project/test/app.e2e-test.ts', + '/project/test/jest-e2e.json', + ].sort()); + }); }); diff --git a/src/lib/application/application.factory.ts b/src/lib/application/application.factory.ts index 4ff0c2346..2b57ffb40 100644 --- a/src/lib/application/application.factory.ts +++ b/src/lib/application/application.factory.ts @@ -1,8 +1,10 @@ import { join, Path, strings } from '@angular-devkit/core'; import { apply, + filter, mergeWith, move, + noop, Rule, Source, template, @@ -40,6 +42,9 @@ function transform(options: ApplicationOptions): ApplicationOptions { target.language = !!target.language ? target.language : DEFAULT_LANGUAGE; target.name = resolvePackageName(target.name.toString()); target.version = !!target.version ? target.version : DEFAULT_VERSION; + target.specFileSuffix = normalizeToKebabOrSnakeCase( + options.specFileSuffix || 'spec', + ); target.packageManager = !target.packageManager || target.packageManager === 'undefined' @@ -76,6 +81,14 @@ function resolvePackageName(path: string) { function generate(options: ApplicationOptions, path: string): Source { return apply(url(join('./files' as Path, options.language)), [ + options.spec ? noop() : filter((path) => !path.endsWith('__specFileSuffix__.ts')), + options.spec + ? noop() + : filter((path) => { + const languageExtension = options.language || 'ts'; + const suffix = `__specFileSuffix__.${languageExtension}`; + return !path.endsWith(suffix); + }), template({ ...strings, ...options, diff --git a/src/lib/application/application.schema.d.ts b/src/lib/application/application.schema.d.ts index d342fd72b..e5e25a896 100644 --- a/src/lib/application/application.schema.d.ts +++ b/src/lib/application/application.schema.d.ts @@ -40,4 +40,13 @@ export interface ApplicationOptions { * Nest included development dependencies (comma separated values). */ devDependencies?: string; + /** + * Specifies if a spec file is generated. + */ + spec?: boolean; + /** + * Specifies the file suffix of spec files. + * @default "spec" + */ + specFileSuffix?: string; } diff --git a/src/lib/application/files/js/src/app.controller.spec.js b/src/lib/application/files/js/src/app.controller.__specFileSuffix__.js similarity index 100% rename from src/lib/application/files/js/src/app.controller.spec.js rename to src/lib/application/files/js/src/app.controller.__specFileSuffix__.js diff --git a/src/lib/application/files/js/test/app.e2e-spec.js b/src/lib/application/files/js/test/app.e2e-__specFileSuffix__.js similarity index 100% rename from src/lib/application/files/js/test/app.e2e-spec.js rename to src/lib/application/files/js/test/app.e2e-__specFileSuffix__.js diff --git a/src/lib/application/files/ts/src/app.controller.spec.ts b/src/lib/application/files/ts/src/app.controller.__specFileSuffix__.ts similarity index 100% rename from src/lib/application/files/ts/src/app.controller.spec.ts rename to src/lib/application/files/ts/src/app.controller.__specFileSuffix__.ts diff --git a/src/lib/application/files/ts/test/app.e2e-spec.ts b/src/lib/application/files/ts/test/app.e2e-__specFileSuffix__.ts similarity index 100% rename from src/lib/application/files/ts/test/app.e2e-spec.ts rename to src/lib/application/files/ts/test/app.e2e-__specFileSuffix__.ts diff --git a/src/lib/application/schema.json b/src/lib/application/schema.json index cf29882fc..dedd49be7 100644 --- a/src/lib/application/schema.json +++ b/src/lib/application/schema.json @@ -55,6 +55,16 @@ "devDependencies": { "type": "string", "description": "Nest application development dependencies." + }, + "spec": { + "type": "boolean", + "default": true, + "description": "Specifies if a spec file is generated." + }, + "specFileSuffix": { + "type": "string", + "default": "spec", + "description": "Specifies the file suffix of spec files." } }, "required": ["name"]