-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Zen-cronic/migrate-tests
Migrate tests
- Loading branch information
Showing
32 changed files
with
732 additions
and
983 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.