Skip to content

Commit

Permalink
merge advancedTesting end with deployment start
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmachadorj committed Jul 14, 2024
1 parent 22a65ab commit 616b6ac
Show file tree
Hide file tree
Showing 51 changed files with 642 additions and 533 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
*/

const replacements = [
// We use only @dddforum/shared in the production code, so no need to include @dddforum/backend, @dddforum/frontend, etc.
['@dddforum/shared/src', '@dddforum/shared/dist'],
["@dddforum/shared/src", "@dddforum/shared/dist"],
["@dddforum/backend/src", "@dddforum/backend/dist"],
["@dddforum/frontend/src", "@dddforum/frontend/dist"],
];

/**
* tsc-alias only supports commonjs replacers.
*/
exports.default = function tsAliasesReplacer({ orig: originalImport, _file, _config }) {
exports.default = function tsAliasesReplacer({
orig: originalImport,
_file,
_config,
}) {
let newImport = originalImport;

replacements.forEach(([fromRule, toRule]) => {
newImport = newImport.replace(fromRule, toRule);
});

return newImport;
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
"node": ">=16.16.0"
},
"scripts": {
"test:e2e:front": "npm run test:e2e --workspace=@dddforum/frontend",
"test:e2e:back": "npm run test:e2e --workspace=@dddforum/backend",
"test:e2e:frontend": "npm run test:e2e --workspace=@dddforum/frontend",
"test:e2e:backend": "npm run test:e2e --workspace=@dddforum/backend",
"test:infra:all": "npm run test:infra --workspaces --if-present",
"test:infra:back": "npm run test:infra --workspace=@dddforum/backend",
"test:unit:back": "npm run test:unit --workspace=@dddforum/backend",
"test:infra:backend": "npm run test:infra --workspace=@dddforum/backend",
"test:unit:backend": "npm run test:unit --workspace=@dddforum/backend",
"test:unit": "npm run test:unit --workspaces --if-present",
"clean": "npm run clean --workspaces --if-present",
"build": "npm run build --workspaces --if-present",
"start:dev:backend": "npm run start:dev --workspace=@dddforum/backend",
"start:dev:frontend": "npm run start:dev --workspace=@dddforum/frontend",
"lint": "npm run lint --workspaces --if-present",
"start:dev:back": "npm run start:dev --workspace=@dddforum/backend",
"start:dev:front": "npm run start:dev --workspace=@dddforum/frontend",
"prettier-format": "run-script-os",
"prettier-format:win32": "prettier --config .prettierrc \"./**/src/**/*.ts\" --write",
"prettier-format:darwin:linux": "prettier --config .prettierrc './**/src/**/*.ts' --write",
Expand All @@ -37,7 +38,7 @@
"eslint-plugin-jest": "^27.6.0",
"eslint-plugin-prettier": "^5.0.1",
"jest": "^29.5.0",
"jest-cucumber": "^3.0.1",
"jest-cucumber": "3.0.1",
"jest-mock-extended": "^3.0.7",
"rimraf": "^3.0.2",
"run-script-os": "^1.1.6",
Expand All @@ -50,6 +51,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"nodemailer": "^6.9.13"
"nodemailer": "^6.9.13",
"tsconfig-paths": "^4.2.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
NODE_ENV=development
DATABASE_URL=
NODE_ENV=development
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
NODE_ENV=test
DATABASE_URL=
NODE_ENV=test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/dist
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
import { Spy } from "../../../../shared/testDoubles/spy";
import {
SendMailInput,
TransactionalEmailAPI,
} from "../../ports/transactionalEmailAPI";
import nodemailer from "nodemailer";
import { SendMailInput, TransactionalEmailAPI } from "../../ports/transactionalEmailAPI";

export class TransactionalEmailAPISpy
extends Spy<TransactionalEmailAPI>
implements TransactionalEmailAPI
{
constructor() {
super();
}

async sendMail(input: SendMailInput): Promise<boolean> {
this.addCall("sendMail", [input]);
return true;
const mailSettings = {
service: process.env.MAIL_SENDER_SERVICE || "gmail",
user: process.env.MAIL_SENDER_EMAIL_ADDRESS,
pass: process.env.MAIL_SENDER_PASSWORD,
};

// Create a transporter object using SMTP
const transporter = nodemailer.createTransport({
service: mailSettings.service,
auth: {
user: mailSettings.user,
pass: mailSettings.pass,
},
authMethod: "PLAIN",
});

export class MailjetTransactionalEmail implements TransactionalEmailAPI {
async sendMail(input: SendMailInput) {
// Email content
const mailOptions = {
from: mailSettings.user,
...input,
};

try {
new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error, info) => {
if (error) return reject(error);
return resolve(info);
});
});

return true;
} catch (err) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
import nodemailer from "nodemailer";

import { Spy } from "../../../../shared/testDoubles/spy";
import {
SendMailInput,
TransactionalEmailAPI,
} from "../../ports/transactionalEmailAPI";

const mailSettings = {
service: process.env.MAIL_SENDER_SERVICE || "gmail",
user: process.env.MAIL_SENDER_EMAIL_ADDRESS,
pass: process.env.MAIL_SENDER_PASSWORD,
};

// Create a transporter object using SMTP
const transporter = nodemailer.createTransport({
service: mailSettings.service,
auth: {
user: mailSettings.user,
pass: mailSettings.pass,
},
authMethod: "PLAIN",
});

export class MailjetTransactionalEmail implements TransactionalEmailAPI {
async sendMail(input: SendMailInput) {
// Email content
const mailOptions = {
from: mailSettings.user,
...input,
};

try {
new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error, info) => {
if (error) return reject(error);
return resolve(info);
});
});
export class TransactionalEmailAPISpy
extends Spy<TransactionalEmailAPI>
implements TransactionalEmailAPI
{
constructor() {
super();
}

return true;
} catch (err) {
return false;
}
async sendMail(input: SendMailInput): Promise<boolean> {
this.addCall("sendMail", [input]);
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Config } from "../../shared/config";
import { ApplicationModule } from "../../shared/modules/applicationModule";
import { TransactionalEmailAPISpy } from "./adapters/transactionalEmailAPI/mailjetTransactionalEmailAPI";
import { MailjetTransactionalEmail } from "./adapters/transactionalEmailAPI/transactionalEmailAPISpy";
import { TransactionalEmailAPISpy } from "./adapters/transactionalEmailAPI/transactionalEmailAPISpy";
import { TransactionalEmailAPI } from "./ports/transactionalEmailAPI";

export class NotificationsModule extends ApplicationModule {
Expand All @@ -22,7 +21,7 @@ export class NotificationsModule extends ApplicationModule {

private createTransactionalEmailAPI() {
if (this.getEnvironment() === "production") {
return new MailjetTransactionalEmail();
return new TransactionalEmailAPISpy();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ export type Script =
export class Config {
env: Environment;
script: Script;
apiURL: string;

constructor(script: Script) {
this.env = (process.env.NODE_ENV as Environment) || "development";
this.script = script;
this.apiURL = this.getAPIURL();
}

getEnvironment() {
Expand All @@ -23,4 +25,17 @@ export class Config {
getScript() {
return this.script;
}

getAPIURL() {
const fallback = "http://localhost:3000";
if (this.isStaging()) {
return process.env.API_URL_STAGING || fallback
}

return fallback
}

isStaging() {
return this.env === "staging";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class WebServer {
async start(): Promise<void> {
return new Promise((resolve, _reject) => {
ProcessService.killProcessOnPort(this.config.port, () => {
if (this.config.env === " test") {
if (this.config.env === "test") {
resolve();
}
console.log("Starting the server");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from 'path';
import { sharedTestRoot } from '@dddforum/shared/src/paths';
import { CreateUserCommand } from '../../../src/modules/users/usersCommand';
import { CompositionRoot } from '../../../src/shared/compositionRoot';
import { TransactionalEmailAPISpy } from '../../../src/modules/notifications/adapters/transactionalEmailAPI/mailjetTransactionalEmailAPI';
import { TransactionalEmailAPISpy } from '../../../src/modules/notifications/adapters/transactionalEmailAPI/transactionalEmailAPISpy';
import { ContactListAPISpy } from '../../../src/modules/marketing/adapters/contactListAPI/contactListSpy';
import { Application } from '../../../src/shared/application/applicationInterface';
import { InMemoryUserRepositorySpy } from '../../../src/modules/users/adapters/inMemoryUserRepositorySpy';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "@dddforum/shared",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {}
}
"name": "@dddforum/shared",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"clean": "rimraf ./dist",
"build": "npm run clean && tsc -b tsconfig.build.json"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const createMarketingAPI = (apiURL: string) => {
});
return successResponse.data as AddEmailToListResponse;
} catch (err) {
//@ts-ignore
//@ts-expect-error
return err.response.data as APIResponse;
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const createPostsAPI = (apiURL: string) => {
const successResponse = await axios.get(`${apiURL}/posts?sort=${sort}`);
return successResponse.data as GetPostsResponse;
} catch (err) {
//@ts-ignore
//@ts-expect-error
return err.response.data as GetPostsResponse;
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ export class DatabaseFixture {

async resetDatabase() {
const connection = this.composition.getDatabase().getConnection();
const deleteAllComments = connection.comment.deleteMany();
const deleteAllVotes = connection.vote.deleteMany();
const deleteAllPosts = connection.post.deleteMany();
const deleteMembers = connection.member.deleteMany();
const deleteAllUsers = connection.user.deleteMany();

try {
await connection.$transaction([
deleteAllComments,
deleteAllVotes,
deleteAllPosts,
deleteMembers,
deleteAllUsers,
connection.comment.deleteMany(),
connection.vote.deleteMany(),
connection.post.deleteMany(),
connection.member.deleteMany(),
connection.user.deleteMany(),
]);
} catch (error) {
console.error(error);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"],
"types": ["node"]
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
*/

const replacements = [
// We use only @dddforum/shared in the production code, so no need to include @dddforum/backend, @dddforum/frontend, etc.
['@dddforum/shared/src', '@dddforum/shared/dist'],
["@dddforum/shared/src", "@dddforum/shared/dist"],
["@dddforum/backend/src", "@dddforum/backend/dist"],
["@dddforum/frontend/src", "@dddforum/frontend/dist"],
];

/**
* tsc-alias only supports commonjs replacers.
*/
exports.default = function tsAliasesReplacer({ orig: originalImport, _file, _config }) {
exports.default = function tsAliasesReplacer({
orig: originalImport,
_file,
_config,
}) {
let newImport = originalImport;

replacements.forEach(([fromRule, toRule]) => {
newImport = newImport.replace(fromRule, toRule);
});

return newImport;
};
};
Loading

0 comments on commit 616b6ac

Please sign in to comment.