Skip to content

Commit

Permalink
Merge pull request #2 from Zearin/refactor/node-test
Browse files Browse the repository at this point in the history
refactor,test: Rewrite tests using Node builtins
  • Loading branch information
zachleat authored Oct 17, 2024
2 parents 47802de + 8a3083c commit f4a9906
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 217 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ npm install @11ty/eleventy-utils
npm run test
```

- We use the [ava JavaScript test runner](https://github.com/avajs/ava) ([Assertions documentation](https://github.com/avajs/ava/blob/master/docs/03-assertions.md))
- We use the native NodeJS [test runner](https://nodejs.org/api/test.html#test-runner) and ([assertions](https://nodejs.org/api/assert.html#assert))
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
"!test/**"
],
"scripts": {
"test": "npx ava --verbose"
"test": "node --test"
},
"license": "MIT",
"engines": {
"node": ">=12"
"node": ">=18"
},
"funding": {
"type": "opencollective",
Expand All @@ -37,8 +37,5 @@
"url": "git://github.com/11ty/eleventy-utils.git"
},
"bugs": "https://github.com/11ty/eleventy-utils/issues",
"homepage": "https://github.com/11ty/eleventy-utils/",
"devDependencies": {
"ava": "^6.1.3"
}
"homepage": "https://github.com/11ty/eleventy-utils/"
}
72 changes: 37 additions & 35 deletions test/IsPlainObjectTest.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,75 @@
const test = require("ava");
const assert = require("node:assert/strict")
const test = require("node:test");
const { isPlainObject } = require("../");


test("isPlainObject", (t) => {
t.is(isPlainObject(null), false);
t.is(isPlainObject(undefined), false);
t.is(isPlainObject(1), false);
t.is(isPlainObject(true), false);
t.is(isPlainObject(false), false);
t.is(isPlainObject("string"), false);
t.is(isPlainObject([]), false);
t.is(isPlainObject(Symbol("a")), false);
t.is(
assert.equal(isPlainObject(null), false);
assert.equal(isPlainObject(undefined), false);
assert.equal(isPlainObject(1), false);
assert.equal(isPlainObject(true), false);
assert.equal(isPlainObject(false), false);
assert.equal(isPlainObject("string"), false);
assert.equal(isPlainObject([]), false);
assert.equal(isPlainObject(Symbol("a")), false);
assert.equal(
isPlainObject(function () {}),
false
);
});

// https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/test/test.js#L11447
// Notably, did not include the test for DOM Elements.
test("Test from lodash.isPlainObject", (t) => {
t.is(isPlainObject({}), true);
t.is(isPlainObject({ a: 1 }), true);
test("Test from lodash.itPlainObject", (t) => {
assert.equal(isPlainObject({}), true);
assert.equal(isPlainObject({ a: 1 }), true);

function Foo(a) {
this.a = 1;
}

t.is(isPlainObject({ constructor: Foo }), true);
t.is(isPlainObject([1, 2, 3]), false);
t.is(isPlainObject(new Foo(1)), false);
assert.equal(isPlainObject({ constructor: Foo }), true);
assert.equal(isPlainObject([1, 2, 3]), false);
assert.equal(isPlainObject(new Foo(1)), false);
});

test("Test from lodash.isPlainObject: should return `true` for objects with a `[[Prototype]]` of `null`", (t) => {
test("Test from lodash.itPlainObject: should return `true` for objects with a `[[Prototype]]` of `null`", (t) => {
let obj = Object.create(null);
t.is(isPlainObject(obj), true);
assert.equal(isPlainObject(obj), true);

obj.constructor = Object.prototype.constructor;
t.is(isPlainObject(obj), true);
assert.equal(isPlainObject(obj), true);
});

test("Test from lodash.isPlainObject: should return `true` for objects with a `valueOf` property", (t) => {
t.is(isPlainObject({ valueOf: 0 }), true);
test("Test from lodash.itPlainObject: should return `true` for objects with a `valueOf` property", (t) => {
assert.equal(isPlainObject({ valueOf: 0 }), true);
});

test("Test from lodash.isPlainObject: should return `true` for objects with a writable `Symbol.toStringTag` property", (t) => {
test("Test from lodash.itPlainObject: should return `true` for objects with a writable `Symbol.toStringTag` property", (t) => {
let obj = {};
obj[Symbol.toStringTag] = "X";

t.is(isPlainObject(obj), true);
assert.equal(isPlainObject(obj), true);
});

test("Test from lodash.isPlainObject: should return `false` for objects with a custom `[[Prototype]]`", (t) => {
test("Test from lodash.itPlainObject: should return `false` for objects with a custom `[[Prototype]]`", (t) => {
let obj = Object.create({ a: 1 });
t.is(isPlainObject(obj), false);
assert.equal(isPlainObject(obj), false);
});

test("Test from lodash.isPlainObject (modified): should return `false` for non-Object objects", (t) => {
t.is(isPlainObject(arguments), true); // WARNING: lodash was false
t.is(isPlainObject(Error), false);
t.is(isPlainObject(Math), true); // WARNING: lodash was false
test("Test from lodash.itPlainObject (modified): should return `false` for non-Object objects", (t) => {
assert.equal(isPlainObject(arguments), true); // WARNING: lodash was false
assert.equal(isPlainObject(Error), false);
assert.equal(isPlainObject(Math), true); // WARNING: lodash was false
});

test("Test from lodash.isPlainObject: should return `false` for non-objects", (t) => {
t.is(isPlainObject(true), false);
t.is(isPlainObject("a"), false);
t.is(isPlainObject(Symbol("a")), false);
test("Test from lodash.itPlainObject: should return `false` for non-objects", (t) => {
assert.equal(isPlainObject(true), false);
assert.equal(isPlainObject("a"), false);
assert.equal(isPlainObject(Symbol("a")), false);
});

test("Test from lodash.isPlainObject (modified): should return `true` for objects with a read-only `Symbol.toStringTag` property", (t) => {
test("Test from lodash.itPlainObject (modified): should return `true` for objects with a read-only `Symbol.toStringTag` property", (t) => {
var object = {};
Object.defineProperty(object, Symbol.toStringTag, {
configurable: true,
Expand All @@ -76,5 +78,5 @@ test("Test from lodash.isPlainObject (modified): should return `true` for object
value: "X",
});

t.is(isPlainObject(object), true); // WARNING: lodash was false
assert.equal(isPlainObject(object), true); // WARNING: lodash was false
});
Loading

0 comments on commit f4a9906

Please sign in to comment.