diff --git a/__tests__/helpers/fileReader.ts b/__tests__/helpers/fileReader.ts new file mode 100644 index 0000000000000000000000000000000000000000..65c2653f1c6bffd1480ef5fbab0f6e28cac892f6 --- /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 0000000000000000000000000000000000000000..4b52c948578d04983f18fecaf4953c63378e6262 --- /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 bb4fa7a3e58a07ee640965e9f0c6a3799d6f4690..f12591d843558467866627d13c06068dec7ebe2f 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 0f214527ac0011f1f9b422a5368dad452c407dff..884c87b53ad8b8421074eba9def4576a53b08f8b 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 c0e8ad88283b0d459321ec0c67fd1e4a39d6f048..ebb0feba821d267b7e7edec955fb388bc7175d96 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 684848daa2439887e34b77253611cda61f16b632..f27a85e6d84420c60423610a170269f8752136ca 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 a23c2de1c1eed2c585f0b6dff6c47df53c75cb8a..b6d98b16baa311a39332dd9af819cb1150d9b177 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 47bc081233251df2045ced8a3927b207609adb92..c05314d428087fa7f28402a8acc00fcf010a0f1e 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 08384831c2eaa9571988e89db1464f8e450aa73a..ce451f25193b5a0fe3e8e8209ccc682a5c970af8 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 0e2a9ad002316a7451cb1e5e5dd3187e3c0023ba..6d8bc5c31e70ee044882cd5d9e9c610f27b69f3e 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 33b9954f25323ea55bff2114a8b2b44c9c48b309..7846313571fbb69aac70a7f9c52f89c277f1ab05 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 d15505aafc861bc0a380af8b1e99c941507abcee..47295e16cca772ff7a28e54b439493b4da7c289c 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 f74daacf5c16a94c018e0e2f7c751addbecf185d..ea3c7c98cbce9e84e6dc1d92107b5cde88c1a9c8 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 335a692e80ef15688f444b37deea6837ecbb54ae..fa6cd546461ca9a795f119cca8f1abba370423a5 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 1ec1507b68cd23acd294d495c67a363b959de6df..0000000000000000000000000000000000000000 --- 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 216e02ed6b0bfb29264c3d559ea89249c600c2df..66ae80cdd4bba7aaec809bee515613485768a34c 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"],