diff --git a/src/__tests__/null-guards.mjs b/src/__tests__/null-guards.mjs new file mode 100644 index 0000000..90010c6 --- /dev/null +++ b/src/__tests__/null-guards.mjs @@ -0,0 +1,37 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import parser from "../../dist/index.js"; + +const guards = [ + "isNode", + "isAttribute", + "isClassName", + "isCombinator", + "isComment", + "isIdentifier", + "isNesting", + "isPseudo", + "isRoot", + "isSelector", + "isString", + "isTag", + "isUniversal", + "isPseudoElement", + "isPseudoClass", + "isContainer", + "isNamespace", +]; + +for (const name of guards) { + test(`${name} returns false for absent values and non-nodes`, () => { + for (const value of [null, undefined, false, 0, "tag", {}, [], { type: "unknown" }]) { + assert.equal(parser[name](value), false); + } + }); +} + +test("isNode does not recognize unrelated Object member names as node types", () => { + for (const type of ["constructor", "toString", "hasOwnProperty"]) { + assert.equal(parser.isNode({ type }), false); + } +}); diff --git a/src/selectors/guards.js b/src/selectors/guards.js index 23845d8..ecad6b0 100644 --- a/src/selectors/guards.js +++ b/src/selectors/guards.js @@ -29,7 +29,7 @@ const IS_TYPE = { }; export function isNode(node) { - return typeof node === "object" && IS_TYPE[node.type]; + return typeof node === "object" && node !== null && IS_TYPE[node.type] === true; } function isNodeType(type, node) {