Skip to content

Commit

Permalink
updated: options test
Browse files Browse the repository at this point in the history
  • Loading branch information
Zen-cronic committed Jul 12, 2024
1 parent ea1fc48 commit 8c03c4b
Show file tree
Hide file tree
Showing 8 changed files with 381 additions and 110 deletions.
40 changes: 0 additions & 40 deletions dist/__tests__/unit/immutableOptions.test.js

This file was deleted.

100 changes: 100 additions & 0 deletions dist/__tests__/unit/options.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../../index");
describe("immutability and configurability of option arguments", () => {
const testArr = [1, 2, 3, 4, 5];
const defaultOptions = index_1.NodeLogger.defaultOptions;
describe("given that a NodeLogger instance is created with options", () => {
it("should NOT allow options to be set by the log func", () => {
const logger = new index_1.NodeLogger("Test", {
ignoreIterators: true,
onlyFirstElem: true,
});
const calledWith = JSON.stringify(logger._options);
// const escapedCalledWith = calledWith.replace(/\"/g, '\\"');
const expectedErrorMsg = new RegExp(`Cannot redefine _options in the instance if the constructor is called with options.\nAlready called with: ${calledWith}`);
expect(() => {
testArr.forEach((num) => {
logger.log({ num }, { ignoreIterators: false });
});
}).toThrow(expectedErrorMsg);
});
it("logger instances should retain the original option values", () => {
const logger = new index_1.NodeLogger("Test", {
ignoreIterators: true,
onlyFirstElem: true,
});
expect(() => {
testArr.forEach((num) => {
logger.log({ num });
});
}).not.toThrow();
expect(logger._options).toStrictEqual({
ignoreIterators: true,
onlyFirstElem: true,
});
});
});
describe("given that a NodeLogger instance is NOT created with options", () => {
it("should allow options to be set by the log func", () => {
const logger = new index_1.NodeLogger("Test");
expect(() => {
testArr.map((num) => {
logger.log({ num }, { ignoreIterators: true, onlyFirstElem: true });
});
}).not.toThrow();
expect(logger._options).toStrictEqual({
ignoreIterators: true,
onlyFirstElem: true,
});
});
it("should allow reconfiguration of the options by the same instance", () => {
const logger = new index_1.NodeLogger("Test");
expect(() => {
testArr.map((num) => {
logger.log({ num }, { ignoreIterators: true });
logger.log({ num }, { ignoreIterators: false, onlyFirstElem: true });
});
}).not.toThrow();
expect(logger._options).toStrictEqual({
ignoreIterators: false,
onlyFirstElem: true,
});
});
it("missing options provided to the instance must use default values", () => {
const logger = new index_1.NodeLogger("Test");
expect(logger._options).toStrictEqual(defaultOptions);
//missing one option
testArr.map((num) => {
logger.log({ num }, { ignoreIterators: true });
});
expect(logger._options).toStrictEqual({
...defaultOptions,
ignoreIterators: true,
});
//all options provided
testArr.map((num) => {
logger.log({ num }, { ignoreIterators: false, onlyFirstElem: true });
});
expect(logger._options).toStrictEqual({
...defaultOptions,
ignoreIterators: false,
onlyFirstElem: true,
});
//turn back missing to default
testArr.map((num) => {
logger.log({ num }, { ignoreIterators: true });
});
expect(logger._options).toStrictEqual({
...defaultOptions,
ignoreIterators: true,
onlyFirstElem: false,
});
//turn back all to default
testArr.map((num) => {
logger.log({ num });
});
expect(logger._options).toStrictEqual(defaultOptions);
});
});
});
97 changes: 97 additions & 0 deletions dist/__tests__/unit/optionstest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../../index");
describe("immutability and configurability of option arguments", () => {
const testArr = [1, 2, 3, 4, 5];
describe("given that a NodeLogger instance is created with options", () => {
it("should NOT allow options to be set by the log func", () => {
const logger = new index_1.NodeLogger("Test", {
ignoreIterators: true,
onlyFirstElem: true,
});
expect(() => {
testArr.forEach((num) => {
logger.log({ num }, { ignoreIterators: false });
});
}).toThrow(/^Cannot redefine _options if the instance is created with options$/);
});
it("logger instances should retain the original option values", () => {
const logger = new index_1.NodeLogger("Test", {
ignoreIterators: true,
onlyFirstElem: true,
});
expect(() => {
testArr.forEach((num) => {
logger.log({ num });
});
}).not.toThrow();
expect(logger._options).toStrictEqual({
ignoreIterators: true,
onlyFirstElem: true,
});
});
});
describe("given that a NodeLogger instance is NOT created with options", () => {
it("should allow options to be set by the log func", () => {
const logger = new index_1.NodeLogger("Test");
expect(() => {
testArr.map((num) => {
logger.log({ num }, { ignoreIterators: true, onlyFirstElem: true });
});
}).not.toThrow();
expect(logger._options).toStrictEqual({
ignoreIterators: true,
onlyFirstElem: true,
});
});
it("should allow reconfiguration of the options by the same instance", () => {
const logger = new index_1.NodeLogger("Test");
expect(() => {
testArr.map((num) => {
logger.log({ num }, { ignoreIterators: true });
logger.log({ num }, { ignoreIterators: false, onlyFirstElem: true });
});
}).not.toThrow();
expect(logger._options).toStrictEqual({
ignoreIterators: false,
onlyFirstElem: true,
});
});
it("missing options provided to the instance must use default values", () => {
const logger = new index_1.NodeLogger("Test");
const defaultOptions = index_1.NodeLogger.defaultOptions;
expect(logger._options).toStrictEqual(defaultOptions);
//missing one option
testArr.map((num) => {
logger.log({ num }, { ignoreIterators: true });
});
expect(logger._options).toStrictEqual({
...defaultOptions,
ignoreIterators: true,
});
//all options provided
testArr.map((num) => {
logger.log({ num }, { ignoreIterators: false, onlyFirstElem: true });
});
expect(logger._options).toStrictEqual({
...defaultOptions,
ignoreIterators: false,
onlyFirstElem: true,
});
//turn back missing to default
testArr.map((num) => {
logger.log({ num }, { ignoreIterators: true });
});
expect(logger._options).toStrictEqual({
...defaultOptions,
ignoreIterators: true,
onlyFirstElem: false,
});
//turn back all to default
testArr.map((num) => {
logger.log({ num });
});
expect(logger._options).toStrictEqual(defaultOptions);
});
});
});
40 changes: 29 additions & 11 deletions dist/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Logger_instances, _a, _Logger_defaultOptions, _Logger_handleOnlyFirstElem, _Logger_validateArgs, _Logger_setOptions, _Logger_selectColour;
var _Logger_instances, _a, _Logger_handleOnlyFirstElem, _Logger_validateArgs, _Logger_setOptions, _Logger_selectColour;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
class Logger {
Expand All @@ -17,7 +17,7 @@ class Logger {
_Logger_instances.add(this);
this.args = null;
this.namespace = namespace;
this._options = options || Object.assign({}, __classPrivateFieldGet(_a, _a, "f", _Logger_defaultOptions));
this._options = options || Object.assign({}, _a.defaultOptions);
this.firstElem = null;
_a.colourNum = __classPrivateFieldGet(this, _Logger_instances, "m", _Logger_selectColour).call(this);
if (options) {
Expand All @@ -28,7 +28,9 @@ class Logger {
writable: false,
});
}
//args and _options can change
//args can change
//_options may change
//namespace cannot change
Object.defineProperty(this, "namespace", {
value: this.namespace,
enumerable: true,
Expand Down Expand Up @@ -155,22 +157,38 @@ _a = Logger, _Logger_instances = new WeakSet(), _Logger_handleOnlyFirstElem = fu
if (typeof args !== "object" ||
args === null ||
Object.keys(args).length === 0) {
throw new Error("Must be a non-empty obj");
throw new TypeError("Must be a non-empty obj");
}
if (Object.keys(args).length !== 1) {
throw new Error("Only 1 property allowed");
}
}, _Logger_setOptions = function _Logger_setOptions(options) {
if (options) {
try {
this._options = Object.assign(this._options, options);
this._options = Object.assign({}, //or this._options //existing
_a.defaultOptions, //default
options //passed
);
}
catch (error) {
throw new Error("Cannot redefine _options if the instance is created with options");
const errorRe = /^Cannot assign to read only property .*/;
if (error.name === "TypeError" && errorRe.test(error.message)) {
throw new Error("Cannot redefine _options in the instance if the constructor is called with options." +
"\n" +
"Already called with: " +
JSON.stringify(this._options));
}
else {
throw error;
}
}
}
else {
this._options = Object.assign({}, __classPrivateFieldGet(_a, _a, "f", _Logger_defaultOptions));
const propDesc = Object.getOwnPropertyDescriptor(this, "_options");
//check whether already set by constructor
if (propDesc?.configurable && propDesc?.writable) {
this._options = Object.assign({}, _a.defaultOptions);
}
}
}, _Logger_selectColour = function _Logger_selectColour() {
const { namespace } = this;
Expand Down Expand Up @@ -207,8 +225,8 @@ Logger.NATIVE_ITERATORS_TYPES = [
"BigInt64Array",
"BigUint64Array",
];
_Logger_defaultOptions = { value: Object.freeze({
ignoreIterators: false,
onlyFirstElem: false,
}) };
Logger.defaultOptions = Object.freeze({
ignoreIterators: false,
onlyFirstElem: false,
});
Logger.colourNum = 7;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"dev:pg": "nodemon --watch \"pg/**\" --ext \"ts,json\" --exec \"ts-node\"",
"dev:tests": "nodemon --watch \"dev-tests/**\" --ext \"ts,json\" --exec \"ts-node\"",

"test:jest": "jest --watchAll",
"test:jest": "jest --watch",
"test:ci": "jest --ci",
"test:js": "jest --watchAll --config jest.config.dist.js",
"test:js": "jest --watch --config jest.config.dist.js",

"test": "set NODE_OPTIONS=-r ts-node/register && jest --watchAll",
"test": "set NODE_OPTIONS=-r ts-node/register && jest --watch",
"test:ts:ci":"export NODE_OPTIONS='-r ts-node/register' && jest --ci"
},
"keywords": [
Expand Down
45 changes: 0 additions & 45 deletions src/__tests__/unit/immutableOptions.test.ts

This file was deleted.

Loading

0 comments on commit 8c03c4b

Please sign in to comment.