Skip to content

Commit

Permalink
Merge pull request #7 from Zen-cronic/migrate-tests
Browse files Browse the repository at this point in the history
Migrate tests
  • Loading branch information
Zen-cronic authored Jul 13, 2024
2 parents eb64aed + d0d79f2 commit 9ccc486
Show file tree
Hide file tree
Showing 32 changed files with 732 additions and 983 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ jobs:

strategy:
matrix:
node-version: [14.x]
node-version: [16.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js Version ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Check shell
Expand Down
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

96 changes: 96 additions & 0 deletions dist/__tests__/e2e/scope-logger-2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("jest-to-log");
const index_1 = require("../../index");
const globals_1 = require("@jest/globals");
describe("scope-logger", () => {
describe("given an array function inside a function call", () => {
it("should log: array fn -> main fn", () => {
const logger = new index_1.NodeLogger("Log tester", {
entryPoint: "Object.toLogStdoutMatcher",
});
function testFn() {
const testArr = [1, 2, 3];
testArr.forEach((number) => {
logger.log({ number });
});
}
//w/ JSON.stringify(_, _, 2) for logBody
const expectedStr = `Log tester: *Array.forEach* -> *testFn*` +
"\n" +
"{\n" +
' "number": 1\n' +
"}\n" +
"\n" +
`Log tester: *Array.forEach* -> *testFn*` +
"\n" +
"{\n" +
' "number": 2\n' +
"}\n" +
"\n" +
`Log tester: *Array.forEach* -> *testFn*` +
"\n" +
"{\n" +
' "number": 3\n' +
"}\n" +
"\n";
//Object.toLogStdoutMatcher
(0, globals_1.expect)(testFn).toLogStdout(expectedStr);
});
});
describe("given a nested function inside a function call", () => {
it("should log: inner fn -> outer fn", () => {
const logger = new index_1.NodeLogger("Log tester");
function testFn() {
function inner_testFn() {
const testVari = 123;
logger.log({ testVari }, { entryPoint: "Object.toLogStdoutMatcher" });
}
inner_testFn();
}
const expectedStr = "Log tester: *inner_testFn* -> *testFn*" +
"\n" +
"{" +
"\n" +
' "testVari": 123' +
"\n" +
"}" +
"\n" +
"\n";
(0, globals_1.expect)(testFn).toLogStdout(expectedStr);
});
});
describe("given a nested array function inside an array function call", () => {
it("should log: inner array fn -> outer array fn -> main fn", () => {
const logger = new index_1.NodeLogger("Log tester", {
entryPoint: "Object.toLogStdoutMatcher",
});
const testOuterArr = [1, 2, 3];
const testInnerArr = [1, 2, 3];
function testFn() {
testOuterArr.map((_) => {
testInnerArr.forEach((testVari) => {
logger.log({ testVari });
});
});
}
const outerRepeat = testOuterArr.length;
const innerRepeat = testInnerArr.length;
const ttlRepeat = outerRepeat * innerRepeat;
let expectedStr = "";
const expectedLogCall = "Log tester: *Array.forEach* -> *Array.map* -> *testFn*" + "\n";
const expectedLogBody = (printVal) => {
return ("{" + "\n" + ` "testVari": ${printVal}` + "\n" + "}" + "\n" + "\n");
};
let innerCount = 0;
for (let i = 0; i < ttlRepeat; i++) {
if (innerCount === innerRepeat) {
//reset
innerCount = 0;
}
expectedStr += expectedLogCall + expectedLogBody(++innerCount);
}
(0, globals_1.expect)(testFn).toLogStdout(expectedStr);
});
});
});
132 changes: 82 additions & 50 deletions dist/__tests__/e2e/scope-logger.test.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,96 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testHelper_1 = require("../../utils/testHelper");
require("jest-to-log");
const index_1 = require("../../index");
const globals_1 = require("@jest/globals");
describe("scope-logger", () => {
const { createMessagePromise, createWorkerDataPromise } = (0, testHelper_1.setupTest)("e2e",
//either js or ts works
"scope-logger.test.process.ts");
/**
*
* @param {string} expectedResult
* @param {boolean} [waitMessageFromWorker=false]
*/
async function runTestCase(expectedResult, waitMessageFromWorker = false) {
let promises = [
createWorkerDataPromise(),
];
if (waitMessageFromWorker) {
promises.push(createMessagePromise());
}
const promisesResult = await Promise.all(promises);
const workerData = promisesResult[0];
const message = promisesResult[1];
const { length = 1 } = message || {};
if (typeof length !== "number") {
throw new Error(`Invalid length from child process: ${length}`);
}
const discolouredResult = workerData.replace(/(\x1b\[\d+;\d+m)|(\x1b\[\d+m)/g, "");
expectedResult = expectedResult.repeat(length);
expect(discolouredResult).toBe(expectedResult);
}
describe("1) given an array function inside a function call", () => {
it("should log: array fn -> main fn", async () => {
try {
const expected = `Log tester: *Array.forEach* -> *fn_1*\n`;
await runTestCase(expected, true);
}
catch (error) {
throw new Error(error);
describe("given an array function inside a function call", () => {
it("should log: array fn -> main fn", () => {
const logger = new index_1.NodeLogger("Log tester", {
entryPoint: "Object.toLogStdoutMatcher",
});
function testFn() {
const testArr = [1, 2, 3];
testArr.forEach((number) => {
logger.log({ number });
});
}
//w/ JSON.stringify(_, _, 2) for logBody
const expectedStr = `Log tester: *Array.forEach* -> *testFn*` +
"\n" +
"{\n" +
' "number": 1\n' +
"}\n" +
"\n" +
`Log tester: *Array.forEach* -> *testFn*` +
"\n" +
"{\n" +
' "number": 2\n' +
"}\n" +
"\n" +
`Log tester: *Array.forEach* -> *testFn*` +
"\n" +
"{\n" +
' "number": 3\n' +
"}\n" +
"\n";
//Object.toLogStdoutMatcher
(0, globals_1.expect)(testFn).toLogStdout(expectedStr);
});
});
describe("2) given a nested function inside a function call", () => {
it("should log: inner fn -> outer fn", async () => {
try {
const expected = `Log tester: *inner_fn_2* -> *fn_2*\n`;
await runTestCase(expected);
}
catch (error) {
throw new Error(error);
describe("given a nested function inside a function call", () => {
it("should log: inner fn -> outer fn", () => {
const logger = new index_1.NodeLogger("Log tester");
function testFn() {
function inner_testFn() {
const testVari = 123;
logger.log({ testVari }, { entryPoint: "Object.toLogStdoutMatcher" });
}
inner_testFn();
}
const expectedStr = "Log tester: *inner_testFn* -> *testFn*" +
"\n" +
"{" +
"\n" +
' "testVari": 123' +
"\n" +
"}" +
"\n" +
"\n";
(0, globals_1.expect)(testFn).toLogStdout(expectedStr);
});
});
describe("3) given a nested array function inside an array function call", () => {
it("should log: inner array fn -> outer array fn -> main fn", async () => {
try {
const expected = `Log tester: *Array.forEach* -> *Array.map* -> *fn_3*\n`;
await runTestCase(expected, true);
describe("given a nested array function inside an array function call", () => {
it("should log: inner array fn -> outer array fn -> main fn", () => {
const logger = new index_1.NodeLogger("Log tester", {
entryPoint: "Object.toLogStdoutMatcher",
});
const testOuterArr = [1, 2, 3];
const testInnerArr = [1, 2, 3];
function testFn() {
testOuterArr.map((_) => {
testInnerArr.forEach((testVari) => {
logger.log({ testVari });
});
});
}
catch (error) {
throw new Error(error);
const outerRepeat = testOuterArr.length;
const innerRepeat = testInnerArr.length;
const ttlRepeat = outerRepeat * innerRepeat;
let expectedStr = "";
const expectedLogCall = "Log tester: *Array.forEach* -> *Array.map* -> *testFn*" + "\n";
const expectedLogBody = (printVal) => {
return ("{" + "\n" + ` "testVari": ${printVal}` + "\n" + "}" + "\n" + "\n");
};
let innerCount = 0;
for (let i = 0; i < ttlRepeat; i++) {
if (innerCount === innerRepeat) {
//reset
innerCount = 0;
}
expectedStr += expectedLogCall + expectedLogBody(++innerCount);
}
(0, globals_1.expect)(testFn).toLogStdout(expectedStr);
});
});
});
73 changes: 48 additions & 25 deletions dist/__tests__/unit/disableAll.test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,60 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testHelper_1 = require("../../utils/testHelper");
require("jest-to-log");
const index_1 = require("../../index");
const globals_1 = require("@jest/globals");
describe("disableAll() method", () => {
const { createWorkerDataPromise } = (0, testHelper_1.setupTest)("unit", "disableAll.test.process.ts");
/**
*
* Only createWorkerDataPromise; Nu createMessagePromise
* @returns {Promise<{discolouredResult: string}>}
*/
async function processWorkerPromises() {
const promiseResult = await createWorkerDataPromise();
const workerData = promiseResult;
const discolouredResult = workerData.replace(/(\x1b\[\d+;\d+m)|(\x1b\[\d+m)/g, "");
return { discolouredResult };
}
describe("1) given that disableAll() is called directly on an instance", () => {
it("should log nothing", async () => {
const { discolouredResult } = await processWorkerPromises();
//just \n
const expected = "\n";
expect(discolouredResult).toBe(expected);
it("should log nothing", () => {
const logger = new index_1.NodeLogger("Log tester");
let logResult = {};
function testFn() {
logger.disableAll();
const foo = "bar";
const logReturn = logger.log({ foo });
logResult = { ...logReturn };
}
(0, globals_1.expect)(testFn).toLogStdout("");
//logResult assigned only after the abv assertion
(0, globals_1.expect)(logResult).toStrictEqual({
stack: null,
logTitle: null,
logBody: null,
});
});
});
describe("2) given that disableAll() is called on the constructor (indirectly on the instance)", () => {
it("should log nothing", async () => {
const { discolouredResult } = await processWorkerPromises();
const expected = "\n";
expect(discolouredResult).toBe(expected);
it("should log nothing", () => {
const logger = new index_1.NodeLogger("Log tester").disableAll();
let logResult = {};
function testFn() {
const foo = "bar";
const logReturn = logger.log({ foo });
logResult = { ...logReturn };
}
(0, globals_1.expect)(testFn).toLogStdout("");
(0, globals_1.expect)(logResult).toStrictEqual({
stack: null,
logTitle: null,
logBody: null,
});
});
});
describe("3) given that the log method is called after invoking disableAll() on the constructor", () => {
it("should NOT throw", async () => {
expect(async () => await processWorkerPromises()).not.toThrow();
describe("3) given that the log method is called by the instance after invoking disableAll() on the constructor", () => {
it("should NOT throw", () => {
const logger = new index_1.NodeLogger("Log tester").disableAll();
let logResult = {};
function testFn() {
const foo = "bar";
const logReturn = logger.log({ foo });
logResult = { ...logReturn };
}
(0, globals_1.expect)(testFn).toLogStdout("");
(0, globals_1.expect)(logResult).toStrictEqual({
stack: null,
logTitle: null,
logBody: null,
});
});
});
});
Loading

0 comments on commit 9ccc486

Please sign in to comment.