diff --git a/.gitignore b/.gitignore index 635d1a75..7a746898 100644 --- a/.gitignore +++ b/.gitignore @@ -94,3 +94,4 @@ samples/todo .nx/cache +sonar-project.properties diff --git a/jest.preset.js b/jest.preset.js index f078ddce..6c8279f7 100644 --- a/jest.preset.js +++ b/jest.preset.js @@ -1,3 +1,3 @@ const nxPreset = require('@nx/jest/preset').default; -module.exports = { ...nxPreset }; +module.exports = { ...nxPreset, coverageReporters: [...nxPreset.coverageReporters, 'lcov', 'cobertura'] }; diff --git a/packages/mailer/src/lib/mailer.service.ts b/packages/mailer/src/lib/mailer.service.ts index c1c03219..e1587502 100644 --- a/packages/mailer/src/lib/mailer.service.ts +++ b/packages/mailer/src/lib/mailer.service.ts @@ -1,9 +1,9 @@ import { Inject, Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; -import { Transporter, SentMessageInfo, SendMailOptions } from 'nodemailer'; -import { MAILER_OPTIONS_PROVIDER_NAME, MAILER_TRANSPORT_PROVIDER_NAME } from './mailer.constants'; -import { MailerModuleOptions, IHandlebarsOptions } from './mailer.types'; import * as fs from 'fs-extra'; +import { SendMailOptions, SentMessageInfo, Transporter } from 'nodemailer'; import { join } from 'path'; +import { MAILER_OPTIONS_PROVIDER_NAME, MAILER_TRANSPORT_PROVIDER_NAME } from './mailer.constants'; +import { IHandlebarsOptions, MailerModuleOptions } from './mailer.types'; @Injectable() export class MailerService implements OnModuleInit, OnModuleDestroy { @@ -31,7 +31,7 @@ export class MailerService implements OnModuleInit, OnModuleDestroy { Object.assign(this.hbsOptions, this.options.hbsOptions); - if (this.hbsOptions.extension![0] !== '.') { + if (!this.hbsOptions.extension!.startsWith('.')) { this.hbsOptions.extension = '.' + this.hbsOptions.extension; } @@ -107,7 +107,7 @@ export class MailerService implements OnModuleInit, OnModuleDestroy { } if (thirdParam && typeof firstParam === 'string') { - mailOptions.html = this.templates[thirdParam!](fourthParam, fifthParam); + mailOptions.html = this.templates[thirdParam](fourthParam, fifthParam); } else { mailOptions.html = this.templates[secondParam!](thirdParam, fourthParam); } @@ -128,7 +128,7 @@ export class MailerService implements OnModuleInit, OnModuleDestroy { } private addHelpers(): void { - if (this.hbsOptions.helpers && this.hbsOptions.helpers.length) { + if (this.hbsOptions.helpers?.length) { this.hbsOptions.helpers.forEach(helper => { this.hbs.registerHelper(helper.name, helper.func); }); @@ -137,11 +137,11 @@ export class MailerService implements OnModuleInit, OnModuleDestroy { private addTemplates(): void { if (fs.existsSync(this.hbsOptions.templatesDir)) { - const templates = fs.readdirSync(this.hbsOptions!.templatesDir, { + const templates = fs.readdirSync(this.hbsOptions.templatesDir, { withFileTypes: true, }); templates - .filter(value => value.name.endsWith(this.hbsOptions!.extension!) && value.isFile()) + .filter(value => value.name.endsWith(this.hbsOptions.extension!) && value.isFile()) .forEach(element => { this.templates[element.name.substring(0, element.name.indexOf('.'))] = this.hbs.compile( fs.readFileSync(join(this.hbsOptions.templatesDir, element.name)).toString(), @@ -152,11 +152,11 @@ export class MailerService implements OnModuleInit, OnModuleDestroy { private addPartials(): void { if (this.hbsOptions.partialsDir && fs.existsSync(this.hbsOptions.partialsDir)) { - const partials = fs.readdirSync(this.hbsOptions!.partialsDir, { + const partials = fs.readdirSync(this.hbsOptions.partialsDir, { withFileTypes: true, }); partials - .filter(value => value.name.endsWith(this.hbsOptions!.extension!) && value.isFile()) + .filter(value => value.name.endsWith(this.hbsOptions.extension!) && value.isFile()) .forEach(element => { this.hbs.registerPartial( element.name.substring(0, element.name.indexOf('.')), diff --git a/packages/mailer/src/test/mailer.service.spec.ts b/packages/mailer/src/test/mailer.service.spec.ts index 774f243b..4d5db0b5 100644 --- a/packages/mailer/src/test/mailer.service.spec.ts +++ b/packages/mailer/src/test/mailer.service.spec.ts @@ -55,7 +55,7 @@ describe('MailerService', () => { const expected = { ...input, from: 'someone@whatever.com' }; service.sendPlainMail(input); - expect(transporter.sendMail).toBeCalledWith(expected); + expect(transporter.sendMail).toHaveBeenCalledWith(expected); }); it('should send a plain text email using the provided transporter providing all params', () => { @@ -68,7 +68,7 @@ describe('MailerService', () => { const expected = { ...input, from: 'someone@whatever.com' }; service.sendPlainMail(input.to, input.subject, input.html); - expect(transporter.sendMail).toBeCalledWith(expected); + expect(transporter.sendMail).toHaveBeenCalledWith(expected); }); it('should send email from a handlebars template by using the provided transporter providing emailOptions object', () => { @@ -99,7 +99,7 @@ describe('MailerService', () => { }; service.sendTemplateMail(input, 'test', { title: 'my title' }); - expect(transporter.sendMail).toBeCalledWith(expected); + expect(transporter.sendMail).toHaveBeenCalledWith(expected); }); it('should register a template and should be ready to use', () => { @@ -116,7 +116,7 @@ describe('MailerService', () => { service.addTemplate('my-view', `My view`); service.sendTemplateMail(input, 'my-view', {}); - expect(transporter.sendMail).toBeCalledWith(expected); + expect(transporter.sendMail).toHaveBeenCalledWith(expected); }); it('should register a partial and should be ready to use', () => { @@ -134,7 +134,7 @@ describe('MailerService', () => { service.addTemplate('my-view', `{{#> my-partial }}My view{{/my-partial}}`); service.registerPartial('my-partial', `this is my partial: {{> @partial-block }}`); service.sendTemplateMail(input, 'my-view', {}); - expect(transporter.sendMail).toBeCalledWith(expected); + expect(transporter.sendMail).toHaveBeenCalledWith(expected); }); it('should register a helper and should be ready to use', () => { @@ -154,6 +154,6 @@ describe('MailerService', () => { }); service.addTemplate('my-view', `{{#bold}}My view{{/bold}}`); service.sendTemplateMail(input, 'my-view', {}); - expect(transporter.sendMail).toBeCalledWith(expected); + expect(transporter.sendMail).toHaveBeenCalledWith(expected); }); }); diff --git a/packages/nx-nest/src/generators/auth-jwt/auth.spec.ts b/packages/nx-nest/src/generators/auth-jwt/auth.spec.ts index 1e10caee..fd05d7f0 100644 --- a/packages/nx-nest/src/generators/auth-jwt/auth.spec.ts +++ b/packages/nx-nest/src/generators/auth-jwt/auth.spec.ts @@ -83,24 +83,6 @@ describe('auth-jwt generator', () => { it('should add JWT configuration if convict is present', async () => { if (tree.exists(`./packages/nx-nest/apps/${options.name}/src/config.ts`)) { const fileContent = tree.read(`./packages/nx-nest/apps/${options.name}/src/config.ts`)?.toString(); - // expect(fileContent).toContain({ - // jwt: { - // secret: { - // doc: 'JWT secret', - // format: String, - // default: 'SECRET', - // env: 'JWT_SECRET', - // arg: 'jwtSecret', - // secret: true, - // }, - // expiration: { - // doc: 'Token expiration time', - // default: '24h', - // format: String, - // env: 'JWT_EXPIRATION', - // }, - // }, - // }); expect(fileContent).toContain('jwt: {'); expect(fileContent).toContain('secret: {'); expect(fileContent).toContain('expiration: {'); diff --git a/packages/nx-nest/src/utils/ast-file-builder.ts b/packages/nx-nest/src/utils/ast-file-builder.ts index 01e8419b..912c2767 100644 --- a/packages/nx-nest/src/utils/ast-file-builder.ts +++ b/packages/nx-nest/src/utils/ast-file-builder.ts @@ -3,6 +3,7 @@ import { AsExpression, ClassDeclaration, IndentationText, + ObjectLiteralElementLike, ObjectLiteralExpression, Project, PropertyAssignment, @@ -369,6 +370,25 @@ export class ASTFileBuilder { return this; } + private updatePropertyAssignment(property: ObjectLiteralElementLike, propertyInitializer: string | string[]): void { + const initializer = (property as PropertyAssignment).getInitializer(); + if (initializer?.getKind() === SyntaxKind.ArrayLiteralExpression) { + if (Array.isArray(propertyInitializer)) { + propertyInitializer.forEach(elem => { + (initializer as ArrayLiteralExpression).addElement(elem); + }); + } else { + (initializer as ArrayLiteralExpression).addElement(propertyInitializer); + } + } else { + property.set({ + initializer: Array.isArray(propertyInitializer) + ? JSON.stringify(propertyInitializer).replace(/(['"])/g, '') + : propertyInitializer, + }); + } + } + addPropertyToObjectLiteralParam( varName: string, paramIndex: number, @@ -388,27 +408,12 @@ export class ASTFileBuilder { if (arg && arg.getKind() === SyntaxKind.ObjectLiteralExpression) { const property = arg.getProperty(propertyName); if (property) { - const initializer = (property as PropertyAssignment).getInitializer(); - if (initializer?.getKind() === SyntaxKind.ArrayLiteralExpression) { - if (Array.isArray(propertyInitializer)) { - propertyInitializer.forEach(elem => { - (initializer as ArrayLiteralExpression).addElement(elem); - }); - } else { - (initializer as ArrayLiteralExpression).addElement(propertyInitializer); - } - } else { - property.set({ - initializer: Array.isArray(propertyInitializer) - ? JSON.stringify(propertyInitializer).replace(/('|")/g, '') - : propertyInitializer, - }); - } + this.updatePropertyAssignment(property, propertyInitializer); } else { arg.addPropertyAssignment({ name: propertyName, initializer: Array.isArray(propertyInitializer) - ? JSON.stringify(propertyInitializer).replace(/('|")/g, '') + ? JSON.stringify(propertyInitializer).replace(/(['"])/g, '') : propertyInitializer, }); } @@ -440,27 +445,12 @@ export class ASTFileBuilder { if (arg && arg.getKind() === SyntaxKind.ObjectLiteralExpression) { const property = (arg as ObjectLiteralExpression).getProperty(propertyName); if (property) { - const initializer = (property as PropertyAssignment).getInitializer(); - if (initializer?.getKind() === SyntaxKind.ArrayLiteralExpression) { - if (Array.isArray(propertyInitializer)) { - propertyInitializer.forEach(elem => { - (initializer as ArrayLiteralExpression).addElement(elem); - }); - } else { - (initializer as ArrayLiteralExpression).addElement(propertyInitializer); - } - } else { - property.set({ - initializer: Array.isArray(propertyInitializer) - ? JSON.stringify(propertyInitializer).replace(/('|")/g, '') - : propertyInitializer, - }); - } + this.updatePropertyAssignment(property, propertyInitializer); } else { (arg as ObjectLiteralExpression).addPropertyAssignment({ name: propertyName, initializer: Array.isArray(propertyInitializer) - ? JSON.stringify(propertyInitializer).replace(/('|")/g, '') + ? JSON.stringify(propertyInitializer).replace(/(['"])/g, '') : propertyInitializer, }); }