From d12aac6c2d4e05d5462c49c75c528ac94f47f91c Mon Sep 17 00:00:00 2001 From: igor <igor.markin@vereign.com> Date: Tue, 29 Dec 2020 14:14:28 +0300 Subject: [PATCH] Refactor testing routine --- __tests__/helpers/fileReader.ts | 16 ++ __tests__/helpers/index.ts | 245 +++++++++++++++++++ __tests__/html-gmail-gmail.test.ts | 9 +- __tests__/html-gmail-outlook.test.ts | 9 +- __tests__/html-outlook-gmail.test.ts | 6 +- __tests__/html-outlook-outlook.test.ts | 10 +- __tests__/htmltext-gmail-gmail.test.ts | 6 +- __tests__/htmltext-gmail-outlook.test.ts | 6 +- __tests__/htmltext-outlook-gmail.test.ts | 6 +- __tests__/htmltext-outlook-outlook.test.ts | 10 +- __tests__/plain-gmail-gmail.test.ts | 6 +- __tests__/plain-gmail-outlook.test.ts | 6 +- __tests__/plain-outlook-gmail.test.ts | 6 +- __tests__/plain-outlook-outlook.test.ts | 21 +- __tests__/utils.ts | 269 --------------------- jest.config.js | 10 +- 16 files changed, 310 insertions(+), 331 deletions(-) create mode 100644 __tests__/helpers/fileReader.ts create mode 100644 __tests__/helpers/index.ts delete mode 100644 __tests__/utils.ts diff --git a/__tests__/helpers/fileReader.ts b/__tests__/helpers/fileReader.ts new file mode 100644 index 0000000..65c2653 --- /dev/null +++ b/__tests__/helpers/fileReader.ts @@ -0,0 +1,16 @@ +const fs = require("fs"); + +export const listDirectories = (testCasesPath: string): Array<string> => { + return fs.readdirSync(testCasesPath).filter(function (file) { + return fs.statSync(testCasesPath + "/" + file).isDirectory(); + }); +}; + +const filesCache: { [key: string]: string } = {}; +export const readFile = (path: string): string => { + if (!filesCache[path]) { + filesCache[path] = fs.readFileSync(path).toString(); + } + + return filesCache[path]; +}; diff --git a/__tests__/helpers/index.ts b/__tests__/helpers/index.ts new file mode 100644 index 0000000..4b52c94 --- /dev/null +++ b/__tests__/helpers/index.ts @@ -0,0 +1,245 @@ +import { JSDOM } from "jsdom"; +const crypto = require("crypto"); +import MIMEParser from "@vereign/mime-parser"; +import { expect, test } from "@jest/globals"; +import { DOM } from "@vereign/dom"; + +import { PlainNormalizer, HTMLNormalizer } from "../../src"; +import { listDirectories, readFile } from "./fileReader"; +import { diffStringsUnified } from "jest-diff"; + +const SENT_EML_NAME = "sent.eml"; +const RECEIVED_EML_NAME = "received.eml"; + +/** + * Substitutes cid:* with base64 strings of the corresponding attachments + */ +const POPULATE_ATTACHMENTS = true; + +interface CasesFilteringOptions { + ignore?: string[]; + checkExclusively?: string[]; +} + +/** + * Hashes base64 of the attachments to improve performance and readability + * @param base64 + */ +const hashAttachment = (base64: string) => { + return crypto + .createHash("sha256") + .update(Buffer.from(base64, "base64")) + .digest() + .toString("base64"); +}; + +expect.extend({ + /** + * Extends "toEqual" with diff functionality + */ + toEqualWithDiff(target, source) { + let pass = true; + try { + expect(target).toEqual(source); + } catch (e) { + pass = false; + } + + return { + pass, + message: pass ? () => "Pass" : () => diffStringsUnified(source, target), + }; + }, +}); + +export const createDescribeHtmlTestCases = ( + testsPath: string, + vendor: string +) => ( + casesGroupName: string, + filteringOptions?: CasesFilteringOptions +) => (): void => { + const testCases = prepareTestCases( + testsPath, + casesGroupName, + filteringOptions + ); + + test.concurrent.each(testCases)( + "Case %s", + async (dirName, { sentMime, receivedMime }) => { + const { sentHtmlDocument, receivedHtmlDocument } = await getDOMDocuments( + sentMime, + receivedMime + ); + + const sentHtml = HTMLNormalizer.normalizeVendorHtml( + sentHtmlDocument, + vendor + ); + const receivedHtml = HTMLNormalizer.normalizeVendorHtml( + receivedHtmlDocument, + vendor + ); + + // eslint-disable-next-line + // @ts-ignore + expect(receivedHtml).toEqualWithDiff(sentHtml); + } + ); +}; + +export const createDescribePlainTestCases = (testsPath: string) => ( + casesGroupName: string, + filteringOptions?: CasesFilteringOptions +) => (): void => { + const testCases = prepareTestCases( + testsPath, + casesGroupName, + filteringOptions + ); + + test.concurrent.each(testCases)( + "Case %s", + async (dirName, { sentMime, receivedMime }) => { + const sentNormalizedPlain = PlainNormalizer.normalizePlain( + await sentMime.getPlain({ + populateAttachments: POPULATE_ATTACHMENTS, + hashAttachment, + }) + ); + const receivedNormalizedPlain = PlainNormalizer.normalizePlain( + await receivedMime.getPlain({ + populateAttachments: POPULATE_ATTACHMENTS, + hashAttachment, + }) + ); + + // eslint-disable-next-line + // @ts-ignore + expect(receivedNormalizedPlain).toEqualWithDiff(sentNormalizedPlain); + } + ); +}; + +export const createDescribePseudoPlainTestCases = ( + testsPath: string, + vendor: string +) => ( + casesGroupName: string, + filteringOptions?: CasesFilteringOptions +) => (): void => { + const testCases = prepareTestCases( + testsPath, + casesGroupName, + filteringOptions + ); + + test.concurrent.each(testCases)( + "Case %s", + async (dirName, { sentMime, receivedMime }) => { + const { sentHtmlDocument, receivedHtmlDocument } = await getDOMDocuments( + sentMime, + receivedMime + ); + + HTMLNormalizer.normalizeVendorHtml(sentHtmlDocument, vendor); + HTMLNormalizer.normalizeVendorHtml(receivedHtmlDocument, vendor); + + const normalizedSentPseudoPlainText = HTMLNormalizer.extractPseudoPlainPart( + sentHtmlDocument + ); + + const normalizedReceivedPseudoPlainText = HTMLNormalizer.extractPseudoPlainPart( + receivedHtmlDocument + ); + + // eslint-disable-next-line + // @ts-ignore + expect(normalizedReceivedPseudoPlainText).toEqualWithDiff( + normalizedSentPseudoPlainText + ); + } + ); +}; + +const prepareTestCases = ( + path: string, + casesGroup: string, + filteringOptions: CasesFilteringOptions +): Array<[string, { sentMime: MIMEParser; receivedMime: MIMEParser }]> => { + const testsCasesPath = path + "/" + casesGroup; + const casesDirs = listDirectories(testsCasesPath); + const casesDirsFiltered = filterTestCases(casesDirs, filteringOptions); + + return casesDirsFiltered.map((caseDir) => { + const sentMimeString = readFile( + `${testsCasesPath}/${caseDir}/${SENT_EML_NAME}` + ); + const receivedMimeString = readFile( + `${testsCasesPath}/${caseDir}/${RECEIVED_EML_NAME}` + ); + + return [ + caseDir, + { + sentMime: new MIMEParser(sentMimeString), + receivedMime: new MIMEParser(receivedMimeString), + }, + ]; + }); +}; + +/** + * + * @param {Array<string>} casesNames + * @param {Object} options + * @param {Array<string>} options.ignore - ignores specific test cases + * @param {Array<string>} options.checkExclusively - focuses on specific test cases (disregards toIgnore) + */ +const filterTestCases = ( + casesNames: string[], + options: CasesFilteringOptions +): string[] => { + const ignore = options?.ignore; + const checkExclusively = options?.checkExclusively; + + if (checkExclusively?.length) { + return casesNames.filter((caseName) => checkExclusively.includes(caseName)); + } + + if (ignore?.length) { + return casesNames.filter((caseName) => !ignore.includes(caseName)); + } + + return casesNames; +}; + +export const getDOMDocuments = async ( + sentMime: MIMEParser, + receivedMime: MIMEParser +): Promise<{ + sentHtmlDocument: HTMLDocument; + receivedHtmlDocument: HTMLDocument; +}> => { + /** + * Usage of the @vereign/dom is a must, because of Google Add-on. + */ + const sentDOM = new DOM( + await sentMime.getHTML({ + populateAttachments: POPULATE_ATTACHMENTS, + hashAttachment, + }) + ); + const receivedDOM = new JSDOM( + await receivedMime.getHTML({ + populateAttachments: POPULATE_ATTACHMENTS, + hashAttachment, + }) + ); + + return { + sentHtmlDocument: sentDOM.window.document, + receivedHtmlDocument: receivedDOM.window.document, + }; +}; diff --git a/__tests__/html-gmail-gmail.test.ts b/__tests__/html-gmail-gmail.test.ts index bb4fa7a..f12591d 100644 --- a/__tests__/html-gmail-gmail.test.ts +++ b/__tests__/html-gmail-gmail.test.ts @@ -2,16 +2,13 @@ import { EMAIL_VENDORS } from "../src"; const path = require("path"); import { describe } from "@jest/globals"; -import { createDescribeHtmlTestCases } from "./utils"; +import { createDescribeHtmlTestCases } from "./helpers"; const TESTS_GLOBAL_PATH = "/files/gmail-gmail"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[HTML] GMail-GMail", () => { - const describeFunction = createDescribeHtmlTestCases( - testsPath, - EMAIL_VENDORS.GMAIL - ); + const runTests = createDescribeHtmlTestCases(testsPath, EMAIL_VENDORS.GMAIL); - describe("Chrome-Chrome", describeFunction("chrome-chrome", [""])); + describe("Chrome-Chrome", runTests("chrome-chrome", {})); }); diff --git a/__tests__/html-gmail-outlook.test.ts b/__tests__/html-gmail-outlook.test.ts index 0f21452..884c87b 100644 --- a/__tests__/html-gmail-outlook.test.ts +++ b/__tests__/html-gmail-outlook.test.ts @@ -2,16 +2,13 @@ import { EMAIL_VENDORS } from "../src"; const path = require("path"); import { describe } from "@jest/globals"; -import { createDescribeHtmlTestCases } from "./utils"; +import { createDescribeHtmlTestCases } from "./helpers"; const TESTS_GLOBAL_PATH = "/files/gmail-outlook"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[HTML] GMail-Outlook", () => { - const describeFunction = createDescribeHtmlTestCases( - testsPath, - EMAIL_VENDORS.GMAIL - ); + const runTests = createDescribeHtmlTestCases(testsPath, EMAIL_VENDORS.GMAIL); - describe("Chrome-Chrome", describeFunction("chrome-chrome", [""])); + describe("Chrome-Chrome", runTests("chrome-chrome")); }); diff --git a/__tests__/html-outlook-gmail.test.ts b/__tests__/html-outlook-gmail.test.ts index c0e8ad8..ebb0feb 100644 --- a/__tests__/html-outlook-gmail.test.ts +++ b/__tests__/html-outlook-gmail.test.ts @@ -1,16 +1,16 @@ import { describe } from "@jest/globals"; import { EMAIL_VENDORS } from "../src"; -import { createDescribeHtmlTestCases } from "./utils"; +import { createDescribeHtmlTestCases } from "./helpers"; const path = require("path"); const TESTS_GLOBAL_PATH = "/files/outlook-gmail"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[HTML] Outlook-GMail emails normalization", () => { - const describeFunction = createDescribeHtmlTestCases( + const runTests = createDescribeHtmlTestCases( testsPath, EMAIL_VENDORS.OUTLOOK ); - describe("Chrome-Chrome", describeFunction("chrome-chrome", [])); + describe("Chrome-Chrome", runTests("chrome-chrome")); }); diff --git a/__tests__/html-outlook-outlook.test.ts b/__tests__/html-outlook-outlook.test.ts index 684848d..f27a85e 100644 --- a/__tests__/html-outlook-outlook.test.ts +++ b/__tests__/html-outlook-outlook.test.ts @@ -1,24 +1,24 @@ import { describe } from "@jest/globals"; import { EMAIL_VENDORS } from "../src"; -import { createDescribeHtmlTestCases } from "./utils"; +import { createDescribeHtmlTestCases } from "./helpers"; const path = require("path"); const TESTS_GLOBAL_PATH = "/files/outlook-outlook"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[HTML] Outlook-Outlook emails normalization", () => { - const describeFunction = createDescribeHtmlTestCases( + const runTests = createDescribeHtmlTestCases( testsPath, EMAIL_VENDORS.OUTLOOK ); - describe("Chrome-Chrome", describeFunction("chrome-chrome", [], [])); + describe("Chrome-Chrome", runTests("chrome-chrome")); describe( "MacOS-MacOS", - describeFunction("macos-macos", ["03", "04", "05", "08", "09"]) + runTests("macos-macos", { ignore: ["03", "04", "05", "08", "09"] }) ); describe( "Windows-Windows", - describeFunction("windows-windows", ["11", "12", "13", "16"]) + runTests("windows-windows", { ignore: ["11", "12", "13", "16"] }) ); }); diff --git a/__tests__/htmltext-gmail-gmail.test.ts b/__tests__/htmltext-gmail-gmail.test.ts index a23c2de..b6d98b1 100644 --- a/__tests__/htmltext-gmail-gmail.test.ts +++ b/__tests__/htmltext-gmail-gmail.test.ts @@ -2,16 +2,16 @@ import { EMAIL_VENDORS } from "../src"; const path = require("path"); import { describe } from "@jest/globals"; -import { createDescribePseudoPlainTestCases } from "./utils"; +import { createDescribePseudoPlainTestCases } from "./helpers"; const TESTS_GLOBAL_PATH = "/files/gmail-gmail"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Pseudo PLAIN] GMail-GMail", () => { - const describeFunction = createDescribePseudoPlainTestCases( + const runTests = createDescribePseudoPlainTestCases( testsPath, EMAIL_VENDORS.GMAIL ); - describe("Gmail-Gmail", describeFunction("chrome-chrome")); + describe("Gmail-Gmail", runTests("chrome-chrome")); }); diff --git a/__tests__/htmltext-gmail-outlook.test.ts b/__tests__/htmltext-gmail-outlook.test.ts index 47bc081..c05314d 100644 --- a/__tests__/htmltext-gmail-outlook.test.ts +++ b/__tests__/htmltext-gmail-outlook.test.ts @@ -1,5 +1,5 @@ import { describe } from "@jest/globals"; -import { createDescribePseudoPlainTestCases } from "./utils"; +import { createDescribePseudoPlainTestCases } from "./helpers"; import { EMAIL_VENDORS } from "../src"; const path = require("path"); @@ -8,9 +8,9 @@ const TESTS_GLOBAL_PATH = "/files/gmail-outlook"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Pseudo PLAIN] Gmail-Outlook normalization", () => { - const describeFunction = createDescribePseudoPlainTestCases( + const runTests = createDescribePseudoPlainTestCases( testsPath, EMAIL_VENDORS.GMAIL ); - describe("Chrome-Chrome", describeFunction("chrome-chrome")); + describe("Chrome-Chrome", runTests("chrome-chrome")); }); diff --git a/__tests__/htmltext-outlook-gmail.test.ts b/__tests__/htmltext-outlook-gmail.test.ts index 0838483..ce451f2 100644 --- a/__tests__/htmltext-outlook-gmail.test.ts +++ b/__tests__/htmltext-outlook-gmail.test.ts @@ -1,5 +1,5 @@ import { describe } from "@jest/globals"; -import { createDescribePseudoPlainTestCases } from "./utils"; +import { createDescribePseudoPlainTestCases } from "./helpers"; import { EMAIL_VENDORS } from "../src"; const path = require("path"); @@ -8,11 +8,11 @@ const TESTS_GLOBAL_PATH = "/files/outlook-gmail"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Pseudo PLAIN] Outlook-Gmail normalization", () => { - const describeFunction = createDescribePseudoPlainTestCases( + const runTests = createDescribePseudoPlainTestCases( testsPath, EMAIL_VENDORS.OUTLOOK ); // ["01"] - is a filter. Pass here names of directories with test cases you want to check - describe("Chrome-Chrome", describeFunction("chrome-chrome")); + describe("Chrome-Chrome", runTests("chrome-chrome")); }); diff --git a/__tests__/htmltext-outlook-outlook.test.ts b/__tests__/htmltext-outlook-outlook.test.ts index 0e2a9ad..6d8bc5c 100644 --- a/__tests__/htmltext-outlook-outlook.test.ts +++ b/__tests__/htmltext-outlook-outlook.test.ts @@ -1,5 +1,5 @@ import { describe } from "@jest/globals"; -import { createDescribePseudoPlainTestCases } from "./utils"; +import { createDescribePseudoPlainTestCases } from "./helpers"; import { EMAIL_VENDORS } from "../src"; const path = require("path"); @@ -8,18 +8,18 @@ const TESTS_GLOBAL_PATH = "/files/outlook-outlook"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Pseudo PLAIN] Outlook-Outlook normalization", () => { - const describeFunction = createDescribePseudoPlainTestCases( + const runTests = createDescribePseudoPlainTestCases( testsPath, EMAIL_VENDORS.OUTLOOK ); - describe("Chrome-Chrome", describeFunction("chrome-chrome")); + describe("Chrome-Chrome", runTests("chrome-chrome")); describe( "MacOS-MacOS", - describeFunction("macos-macos", ["03", "04", "05", "08", "09"]) + runTests("macos-macos", { ignore: ["03", "04", "05", "08", "09"] }) ); describe( "Windows-Windows", - describeFunction("windows-windows", ["11", "12", "16"]) + runTests("windows-windows", { ignore: ["11", "12", "16"] }) ); }); diff --git a/__tests__/plain-gmail-gmail.test.ts b/__tests__/plain-gmail-gmail.test.ts index 33b9954..7846313 100644 --- a/__tests__/plain-gmail-gmail.test.ts +++ b/__tests__/plain-gmail-gmail.test.ts @@ -1,12 +1,12 @@ const path = require("path"); import { describe } from "@jest/globals"; -import { createDescribePlainTestCases } from "./utils"; +import { createDescribePlainTestCases } from "./helpers"; const TESTS_GLOBAL_PATH = "/files/gmail-gmail"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Plain] GMail-GMail", () => { - const describeFunction = createDescribePlainTestCases(testsPath); + const runTests = createDescribePlainTestCases(testsPath); - describe("Chrome-Chrome", describeFunction("chrome-chrome", [])); + describe("Chrome-Chrome", runTests("chrome-chrome")); }); diff --git a/__tests__/plain-gmail-outlook.test.ts b/__tests__/plain-gmail-outlook.test.ts index d15505a..47295e1 100644 --- a/__tests__/plain-gmail-outlook.test.ts +++ b/__tests__/plain-gmail-outlook.test.ts @@ -1,5 +1,5 @@ import { describe } from "@jest/globals"; -import { createDescribePlainTestCases } from "./utils"; +import { createDescribePlainTestCases } from "./helpers"; const path = require("path"); const TESTS_GLOBAL_PATH = "/files/gmail-outlook"; @@ -7,6 +7,6 @@ const TESTS_GLOBAL_PATH = "/files/gmail-outlook"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Plain] Gmail-Outlook normalization", () => { - const describeFunction = createDescribePlainTestCases(testsPath); - describe("Chrome-Chrome", describeFunction("chrome-chrome", [], [])); + const runTests = createDescribePlainTestCases(testsPath); + describe("Chrome-Chrome", runTests("chrome-chrome")); }); diff --git a/__tests__/plain-outlook-gmail.test.ts b/__tests__/plain-outlook-gmail.test.ts index f74daac..ea3c7c9 100644 --- a/__tests__/plain-outlook-gmail.test.ts +++ b/__tests__/plain-outlook-gmail.test.ts @@ -1,5 +1,5 @@ import { describe } from "@jest/globals"; -import { createDescribePlainTestCases } from "./utils"; +import { createDescribePlainTestCases } from "./helpers"; const path = require("path"); const TESTS_GLOBAL_PATH = "/files/outlook-gmail"; @@ -7,6 +7,6 @@ const TESTS_GLOBAL_PATH = "/files/outlook-gmail"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Plain] Outlook-Gmail normalization", () => { - const describeFunction = createDescribePlainTestCases(testsPath); - describe("Chrome-Chrome", describeFunction("chrome-chrome")); + const runTests = createDescribePlainTestCases(testsPath); + describe("Chrome-Chrome", runTests("chrome-chrome")); }); diff --git a/__tests__/plain-outlook-outlook.test.ts b/__tests__/plain-outlook-outlook.test.ts index 335a692..fa6cd54 100644 --- a/__tests__/plain-outlook-outlook.test.ts +++ b/__tests__/plain-outlook-outlook.test.ts @@ -1,5 +1,5 @@ import { describe } from "@jest/globals"; -import { createDescribePlainTestCases } from "./utils"; +import { createDescribePlainTestCases } from "./helpers"; const path = require("path"); const TESTS_GLOBAL_PATH = "/files/outlook-outlook"; @@ -7,20 +7,13 @@ const TESTS_GLOBAL_PATH = "/files/outlook-outlook"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Plain] Outlook-Outlook normalization", () => { - const describeFunction = createDescribePlainTestCases(testsPath); - describe("Chrome-Chrome", describeFunction("chrome-chrome")); - describe("MacOS-MacOS", describeFunction("macos-macos")); + const runTests = createDescribePlainTestCases(testsPath); + describe("Chrome-Chrome", runTests("chrome-chrome")); + describe("MacOS-MacOS", runTests("macos-macos")); describe( "Windows-Windows", - describeFunction("windows-windows", [ - "09", - "10", - "11", - "12", - "13", - "14", - "15", - "16", - ]) + runTests("windows-windows", { + ignore: ["09", "10", "11", "12", "13", "14", "15", "16"], + }) ); }); diff --git a/__tests__/utils.ts b/__tests__/utils.ts deleted file mode 100644 index 1ec1507..0000000 --- a/__tests__/utils.ts +++ /dev/null @@ -1,269 +0,0 @@ -import { JSDOM } from "jsdom"; -const fs = require("fs"); -const crypto = require("crypto"); -import MIMEParser from "@vereign/mime-parser"; -import { expect, test } from "@jest/globals"; -import { DOM } from "@vereign/dom"; -import { diffStringsUnified } from "jest-diff"; - -import { PlainNormalizer, HTMLNormalizer } from "../src"; - -const SENT_EML_NAME = "sent.eml"; -const RECEIVED_EML_NAME = "received.eml"; - -const populateAttachments = true; -const hashAttachment = (base64: string) => { - return crypto - .createHash("sha256") - .update(Buffer.from(base64, "base64")) - .digest() - .toString("base64"); -}; - -expect.extend({ - toEqualWithDiff(target, source) { - let pass = true; - try { - expect(target).toEqual(source); - } catch (e) { - pass = false; - } - - return { - pass, - message: pass ? () => "Pass" : () => diffStringsUnified(source, target), - }; - }, -}); - -const mimeCache: { [key: string]: MIMEParser } = {}; -const getMime = (path: string): MIMEParser => { - if (!mimeCache[path]) { - const mimeString = fs.readFileSync(path).toString(); - mimeCache[path] = new MIMEParser(mimeString); - } - - return mimeCache[path]; -}; - -export const getNormalizedPlain = async ( - testCasePath: string -): Promise<{ - sentPlain: string; - receivedPlain: string; -}> => { - const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`); - const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`); - - const sentNormalizedPlain = PlainNormalizer.normalizePlain( - await sentMime.getPlain({ - populateAttachments, - hashAttachment, - }) - ); - const receivedNormalizedPlain = PlainNormalizer.normalizePlain( - await receivedMime.getPlain({ - populateAttachments, - hashAttachment, - }) - ); - - return { - sentPlain: sentNormalizedPlain, - receivedPlain: receivedNormalizedPlain, - }; -}; - -export const getTestCasesDirs = (testCasesPath: string): Array<string> => { - return fs.readdirSync(testCasesPath).filter(function (file) { - return fs.statSync(testCasesPath + "/" + file).isDirectory(); - }); -}; - -export const getNormalizedHtml = async ( - testCasePath: string, - vendor: string -): Promise<{ - sentHtml: string; - receivedHtml: string; -}> => { - const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`); - const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`); - - const sentHtml = await sentMime.getHTML({ - populateAttachments, - hashAttachment, - }); - const receivedHtml = await receivedMime.getHTML({ - populateAttachments, - hashAttachment, - }); - - const sentDOM = new DOM(sentHtml); - const receivedDOM = new JSDOM(receivedHtml); - - const sentNormalizedHtml = HTMLNormalizer.normalizeVendorHtml( - sentDOM.window.document, - vendor - ); - const receivedNormalizedHtml = HTMLNormalizer.normalizeVendorHtml( - receivedDOM.window.document, - vendor - ); - - return { - sentHtml: sentNormalizedHtml, - receivedHtml: receivedNormalizedHtml, - }; -}; - -export const createDescribeHtmlTestCases = ( - testsPath: string, - vendor: string -) => - /** - * @param casesGroupName - name of the folder with cases - * @param failingCases - a list of cases that are failing and ignored. Pending to be fixed - * @param casesToCheckOnly - a filter to use if you want to check specific cases - */ - ( - casesGroupName: string, - failingCases?: Array<string>, - casesToCheckOnly?: Array<string> - ) => (): void => { - const testsCasesPath = testsPath + "/" + casesGroupName; - let testCasesDirs = getTestCasesDirs(testsCasesPath); - - if (casesToCheckOnly && casesToCheckOnly.length) { - testCasesDirs = testCasesDirs.filter((dir) => - casesToCheckOnly.includes(dir) - ); - } - - if (failingCases && failingCases.length) { - testCasesDirs = testCasesDirs.filter( - (dir) => !failingCases.includes(dir) - ); - } - - test.each(testCasesDirs)("Case %s", async (dirName: string) => { - const testCasePath = testsCasesPath + "/" + dirName; - const normalizedHtmls = await getNormalizedHtml(testCasePath, vendor); - const { sentHtml, receivedHtml } = normalizedHtmls; - - // expect(receivedHtml.length).toBeGreaterThan(0); - // expect(sentHtml.length).toBeGreaterThan(0); - - // eslint-disable-next-line - // @ts-ignore - // console.log(receivedHtml); - expect(receivedHtml).toEqualWithDiff(sentHtml); - }); - }; - -export const createDescribePlainTestCases = (testsPath: string) => ( - casesName: string, - failingCases: Array<string> = [], - casesToCheckOnly?: Array<string> -) => (): void => { - const testsCasesPath = testsPath + "/" + casesName; - let testCasesDirs = getTestCasesDirs(testsCasesPath); - - if (casesToCheckOnly && casesToCheckOnly.length) { - testCasesDirs = testCasesDirs.filter((dir) => - casesToCheckOnly.includes(dir) - ); - } - - if (failingCases && failingCases.length) { - testCasesDirs = testCasesDirs.filter((dir) => !failingCases.includes(dir)); - } - - test.each(testCasesDirs)("Case %s", async (dirName: string) => { - const testCasePath = testsCasesPath + "/" + dirName; - const normalizedPlain = await getNormalizedPlain(testCasePath); - - const { sentPlain, receivedPlain } = normalizedPlain; - // expect(sentPlain.length).toBeGreaterThan(0); - // expect(receivedPlain.length).toBeGreaterThan(0); - // eslint-disable-next-line - // @ts-ignore - expect(receivedPlain).toEqualWithDiff(sentPlain); - }); -}; - -export const createDescribePseudoPlainTestCases = ( - testsPath: string, - vendor: string -) => - /** - * @param casesGroupName - name of the folder with cases - * @param failingCases - a list of cases that are failing and ignored. Pending to be fixed - * @param casesToCheckOnly - a filter to use if you want to check specific cases - */ - ( - casesGroupName: string, - failingCases?: Array<string>, - casesToCheckOnly?: Array<string> - ) => (): void => { - const testsCasesPath = testsPath + "/" + casesGroupName; - let testCasesDirs = getTestCasesDirs(testsCasesPath); - - if (casesToCheckOnly && casesToCheckOnly.length) { - testCasesDirs = testCasesDirs.filter((dir) => - casesToCheckOnly.includes(dir) - ); - } - - if (failingCases && failingCases.length) { - testCasesDirs = testCasesDirs.filter( - (dir) => !failingCases.includes(dir) - ); - } - - test.each(testCasesDirs)("Case %s", async (dirName: string) => { - const testCasePath = testsCasesPath + "/" + dirName; - const { sentHtmlDocument, receivedHtmlDocument } = await getDOMDocuments( - testCasePath - ); - - HTMLNormalizer.normalizeVendorHtml(sentHtmlDocument, vendor); - HTMLNormalizer.normalizeVendorHtml(receivedHtmlDocument, vendor); - - const normalizedSentPseudoPlainText = HTMLNormalizer.extractPseudoPlainPart( - sentHtmlDocument - ); - - const normalizedReceivedPseudoPlainText = HTMLNormalizer.extractPseudoPlainPart( - receivedHtmlDocument - ); - - // eslint-disable-next-line - // @ts-ignore - expect(normalizedReceivedPseudoPlainText).toEqualWithDiff( - normalizedSentPseudoPlainText - ); - }); - }; - -export const getDOMDocuments = async ( - testCasePath: string -): Promise<{ - sentHtmlDocument: HTMLDocument; - receivedHtmlDocument: HTMLDocument; -}> => { - const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`); - const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`); - - const sentDOM = new DOM( - await sentMime.getHTML({ populateAttachments, hashAttachment }) - ); - const receivedDOM = new JSDOM( - await receivedMime.getHTML({ populateAttachments, hashAttachment }) - ); - - return { - sentHtmlDocument: sentDOM.window.document, - receivedHtmlDocument: receivedDOM.window.document, - }; -}; diff --git a/jest.config.js b/jest.config.js index 216e02e..66ae80c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -82,7 +82,7 @@ module.exports = { // moduleNameMapper: {}, // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader - modulePathIgnorePatterns: ["__tests__/utils.ts"], + // modulePathIgnorePatterns: ["__tests__/utils.ts"], // Activates notifications for test results // notify: false, @@ -144,10 +144,10 @@ module.exports = { // testLocationInResults: false, // The glob patterns Jest uses to detect test files - // testMatch: [ - // "**/__tests__/**/*.[jt]s?(x)", - // "**/?(*.)+(spec|test).[tj]s?(x)" - // ], + testMatch: [ + // "**/__tests__/**/*.[jt]s?(x)", + "**/?(*.)+(spec|test).[tj]s?(x)", + ], // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped testPathIgnorePatterns: ["/node_modules/", "setupFiles.js"], -- GitLab