Skip to content
Snippets Groups Projects
Commit d12aac6c authored by Igor Markin's avatar Igor Markin
Browse files

Refactor testing routine

parent 8f57a3f2
Branches
Tags
1 merge request!19Add readme and improve tests usage
Showing
with 310 additions and 331 deletions
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];
};
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,
};
};
...@@ -2,16 +2,13 @@ import { EMAIL_VENDORS } from "../src"; ...@@ -2,16 +2,13 @@ import { EMAIL_VENDORS } from "../src";
const path = require("path"); const path = require("path");
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { createDescribeHtmlTestCases } from "./utils"; import { createDescribeHtmlTestCases } from "./helpers";
const TESTS_GLOBAL_PATH = "/files/gmail-gmail"; const TESTS_GLOBAL_PATH = "/files/gmail-gmail";
const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[HTML] GMail-GMail", () => { describe("[HTML] GMail-GMail", () => {
const describeFunction = createDescribeHtmlTestCases( const runTests = createDescribeHtmlTestCases(testsPath, EMAIL_VENDORS.GMAIL);
testsPath,
EMAIL_VENDORS.GMAIL
);
describe("Chrome-Chrome", describeFunction("chrome-chrome", [""])); describe("Chrome-Chrome", runTests("chrome-chrome", {}));
}); });
...@@ -2,16 +2,13 @@ import { EMAIL_VENDORS } from "../src"; ...@@ -2,16 +2,13 @@ import { EMAIL_VENDORS } from "../src";
const path = require("path"); const path = require("path");
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { createDescribeHtmlTestCases } from "./utils"; import { createDescribeHtmlTestCases } from "./helpers";
const TESTS_GLOBAL_PATH = "/files/gmail-outlook"; const TESTS_GLOBAL_PATH = "/files/gmail-outlook";
const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[HTML] GMail-Outlook", () => { describe("[HTML] GMail-Outlook", () => {
const describeFunction = createDescribeHtmlTestCases( const runTests = createDescribeHtmlTestCases(testsPath, EMAIL_VENDORS.GMAIL);
testsPath,
EMAIL_VENDORS.GMAIL
);
describe("Chrome-Chrome", describeFunction("chrome-chrome", [""])); describe("Chrome-Chrome", runTests("chrome-chrome"));
}); });
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { EMAIL_VENDORS } from "../src"; import { EMAIL_VENDORS } from "../src";
import { createDescribeHtmlTestCases } from "./utils"; import { createDescribeHtmlTestCases } from "./helpers";
const path = require("path"); const path = require("path");
const TESTS_GLOBAL_PATH = "/files/outlook-gmail"; const TESTS_GLOBAL_PATH = "/files/outlook-gmail";
const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[HTML] Outlook-GMail emails normalization", () => { describe("[HTML] Outlook-GMail emails normalization", () => {
const describeFunction = createDescribeHtmlTestCases( const runTests = createDescribeHtmlTestCases(
testsPath, testsPath,
EMAIL_VENDORS.OUTLOOK EMAIL_VENDORS.OUTLOOK
); );
describe("Chrome-Chrome", describeFunction("chrome-chrome", [])); describe("Chrome-Chrome", runTests("chrome-chrome"));
}); });
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { EMAIL_VENDORS } from "../src"; import { EMAIL_VENDORS } from "../src";
import { createDescribeHtmlTestCases } from "./utils"; import { createDescribeHtmlTestCases } from "./helpers";
const path = require("path"); const path = require("path");
const TESTS_GLOBAL_PATH = "/files/outlook-outlook"; const TESTS_GLOBAL_PATH = "/files/outlook-outlook";
const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[HTML] Outlook-Outlook emails normalization", () => { describe("[HTML] Outlook-Outlook emails normalization", () => {
const describeFunction = createDescribeHtmlTestCases( const runTests = createDescribeHtmlTestCases(
testsPath, testsPath,
EMAIL_VENDORS.OUTLOOK EMAIL_VENDORS.OUTLOOK
); );
describe("Chrome-Chrome", describeFunction("chrome-chrome", [], [])); describe("Chrome-Chrome", runTests("chrome-chrome"));
describe( describe(
"MacOS-MacOS", "MacOS-MacOS",
describeFunction("macos-macos", ["03", "04", "05", "08", "09"]) runTests("macos-macos", { ignore: ["03", "04", "05", "08", "09"] })
); );
describe( describe(
"Windows-Windows", "Windows-Windows",
describeFunction("windows-windows", ["11", "12", "13", "16"]) runTests("windows-windows", { ignore: ["11", "12", "13", "16"] })
); );
}); });
...@@ -2,16 +2,16 @@ import { EMAIL_VENDORS } from "../src"; ...@@ -2,16 +2,16 @@ import { EMAIL_VENDORS } from "../src";
const path = require("path"); const path = require("path");
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { createDescribePseudoPlainTestCases } from "./utils"; import { createDescribePseudoPlainTestCases } from "./helpers";
const TESTS_GLOBAL_PATH = "/files/gmail-gmail"; const TESTS_GLOBAL_PATH = "/files/gmail-gmail";
const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[Pseudo PLAIN] GMail-GMail", () => { describe("[Pseudo PLAIN] GMail-GMail", () => {
const describeFunction = createDescribePseudoPlainTestCases( const runTests = createDescribePseudoPlainTestCases(
testsPath, testsPath,
EMAIL_VENDORS.GMAIL EMAIL_VENDORS.GMAIL
); );
describe("Gmail-Gmail", describeFunction("chrome-chrome")); describe("Gmail-Gmail", runTests("chrome-chrome"));
}); });
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { createDescribePseudoPlainTestCases } from "./utils"; import { createDescribePseudoPlainTestCases } from "./helpers";
import { EMAIL_VENDORS } from "../src"; import { EMAIL_VENDORS } from "../src";
const path = require("path"); const path = require("path");
...@@ -8,9 +8,9 @@ const TESTS_GLOBAL_PATH = "/files/gmail-outlook"; ...@@ -8,9 +8,9 @@ const TESTS_GLOBAL_PATH = "/files/gmail-outlook";
const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[Pseudo PLAIN] Gmail-Outlook normalization", () => { describe("[Pseudo PLAIN] Gmail-Outlook normalization", () => {
const describeFunction = createDescribePseudoPlainTestCases( const runTests = createDescribePseudoPlainTestCases(
testsPath, testsPath,
EMAIL_VENDORS.GMAIL EMAIL_VENDORS.GMAIL
); );
describe("Chrome-Chrome", describeFunction("chrome-chrome")); describe("Chrome-Chrome", runTests("chrome-chrome"));
}); });
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { createDescribePseudoPlainTestCases } from "./utils"; import { createDescribePseudoPlainTestCases } from "./helpers";
import { EMAIL_VENDORS } from "../src"; import { EMAIL_VENDORS } from "../src";
const path = require("path"); const path = require("path");
...@@ -8,11 +8,11 @@ const TESTS_GLOBAL_PATH = "/files/outlook-gmail"; ...@@ -8,11 +8,11 @@ const TESTS_GLOBAL_PATH = "/files/outlook-gmail";
const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[Pseudo PLAIN] Outlook-Gmail normalization", () => { describe("[Pseudo PLAIN] Outlook-Gmail normalization", () => {
const describeFunction = createDescribePseudoPlainTestCases( const runTests = createDescribePseudoPlainTestCases(
testsPath, testsPath,
EMAIL_VENDORS.OUTLOOK EMAIL_VENDORS.OUTLOOK
); );
// ["01"] - is a filter. Pass here names of directories with test cases you want to check // ["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"));
}); });
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { createDescribePseudoPlainTestCases } from "./utils"; import { createDescribePseudoPlainTestCases } from "./helpers";
import { EMAIL_VENDORS } from "../src"; import { EMAIL_VENDORS } from "../src";
const path = require("path"); const path = require("path");
...@@ -8,18 +8,18 @@ const TESTS_GLOBAL_PATH = "/files/outlook-outlook"; ...@@ -8,18 +8,18 @@ const TESTS_GLOBAL_PATH = "/files/outlook-outlook";
const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[Pseudo PLAIN] Outlook-Outlook normalization", () => { describe("[Pseudo PLAIN] Outlook-Outlook normalization", () => {
const describeFunction = createDescribePseudoPlainTestCases( const runTests = createDescribePseudoPlainTestCases(
testsPath, testsPath,
EMAIL_VENDORS.OUTLOOK EMAIL_VENDORS.OUTLOOK
); );
describe("Chrome-Chrome", describeFunction("chrome-chrome")); describe("Chrome-Chrome", runTests("chrome-chrome"));
describe( describe(
"MacOS-MacOS", "MacOS-MacOS",
describeFunction("macos-macos", ["03", "04", "05", "08", "09"]) runTests("macos-macos", { ignore: ["03", "04", "05", "08", "09"] })
); );
describe( describe(
"Windows-Windows", "Windows-Windows",
describeFunction("windows-windows", ["11", "12", "16"]) runTests("windows-windows", { ignore: ["11", "12", "16"] })
); );
}); });
const path = require("path"); const path = require("path");
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { createDescribePlainTestCases } from "./utils"; import { createDescribePlainTestCases } from "./helpers";
const TESTS_GLOBAL_PATH = "/files/gmail-gmail"; const TESTS_GLOBAL_PATH = "/files/gmail-gmail";
const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[Plain] GMail-GMail", () => { describe("[Plain] GMail-GMail", () => {
const describeFunction = createDescribePlainTestCases(testsPath); const runTests = createDescribePlainTestCases(testsPath);
describe("Chrome-Chrome", describeFunction("chrome-chrome", [])); describe("Chrome-Chrome", runTests("chrome-chrome"));
}); });
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { createDescribePlainTestCases } from "./utils"; import { createDescribePlainTestCases } from "./helpers";
const path = require("path"); const path = require("path");
const TESTS_GLOBAL_PATH = "/files/gmail-outlook"; const TESTS_GLOBAL_PATH = "/files/gmail-outlook";
...@@ -7,6 +7,6 @@ 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}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[Plain] Gmail-Outlook normalization", () => { describe("[Plain] Gmail-Outlook normalization", () => {
const describeFunction = createDescribePlainTestCases(testsPath); const runTests = createDescribePlainTestCases(testsPath);
describe("Chrome-Chrome", describeFunction("chrome-chrome", [], [])); describe("Chrome-Chrome", runTests("chrome-chrome"));
}); });
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { createDescribePlainTestCases } from "./utils"; import { createDescribePlainTestCases } from "./helpers";
const path = require("path"); const path = require("path");
const TESTS_GLOBAL_PATH = "/files/outlook-gmail"; const TESTS_GLOBAL_PATH = "/files/outlook-gmail";
...@@ -7,6 +7,6 @@ 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}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[Plain] Outlook-Gmail normalization", () => { describe("[Plain] Outlook-Gmail normalization", () => {
const describeFunction = createDescribePlainTestCases(testsPath); const runTests = createDescribePlainTestCases(testsPath);
describe("Chrome-Chrome", describeFunction("chrome-chrome")); describe("Chrome-Chrome", runTests("chrome-chrome"));
}); });
import { describe } from "@jest/globals"; import { describe } from "@jest/globals";
import { createDescribePlainTestCases } from "./utils"; import { createDescribePlainTestCases } from "./helpers";
const path = require("path"); const path = require("path");
const TESTS_GLOBAL_PATH = "/files/outlook-outlook"; const TESTS_GLOBAL_PATH = "/files/outlook-outlook";
...@@ -7,20 +7,13 @@ 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}`); const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[Plain] Outlook-Outlook normalization", () => { describe("[Plain] Outlook-Outlook normalization", () => {
const describeFunction = createDescribePlainTestCases(testsPath); const runTests = createDescribePlainTestCases(testsPath);
describe("Chrome-Chrome", describeFunction("chrome-chrome")); describe("Chrome-Chrome", runTests("chrome-chrome"));
describe("MacOS-MacOS", describeFunction("macos-macos")); describe("MacOS-MacOS", runTests("macos-macos"));
describe( describe(
"Windows-Windows", "Windows-Windows",
describeFunction("windows-windows", [ runTests("windows-windows", {
"09", ignore: ["09", "10", "11", "12", "13", "14", "15", "16"],
"10", })
"11",
"12",
"13",
"14",
"15",
"16",
])
); );
}); });
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,
};
};
...@@ -82,7 +82,7 @@ module.exports = { ...@@ -82,7 +82,7 @@ module.exports = {
// moduleNameMapper: {}, // moduleNameMapper: {},
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader // 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 // Activates notifications for test results
// notify: false, // notify: false,
...@@ -144,10 +144,10 @@ module.exports = { ...@@ -144,10 +144,10 @@ module.exports = {
// testLocationInResults: false, // testLocationInResults: false,
// The glob patterns Jest uses to detect test files // The glob patterns Jest uses to detect test files
// testMatch: [ testMatch: [
// "**/__tests__/**/*.[jt]s?(x)", // "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)" "**/?(*.)+(spec|test).[tj]s?(x)",
// ], ],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
testPathIgnorePatterns: ["/node_modules/", "setupFiles.js"], testPathIgnorePatterns: ["/node_modules/", "setupFiles.js"],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment