test: add more tests for plain objects · unjs/defu@b65f603
1+import { describe } from "node:test";
12import { it, expect } from "vitest";
23import { isPlainObject } from "../src/_utils";
344-it("isPlainObject", () => {
5-expect(isPlainObject(undefined)).toBe(false);
6-expect(isPlainObject(0)).toBe(false);
7-expect(isPlainObject(0n)).toBe(false);
8-expect(isPlainObject("")).toBe(false);
9-expect(isPlainObject(true)).toBe(false);
10-expect(isPlainObject(Symbol(""))).toBe(false);
11-expect(isPlainObject(() => {})).toBe(false);
12-expect(isPlainObject(function namedFunc() {})).toBe(false);
13-expect(isPlainObject(null)).toBe(false);
14-expect(isPlainObject({})).toBe(true);
15-expect(isPlainObject(Math)).toBe(false);
16-expect(isPlainObject(new Set([]))).toBe(false);
17-expect(isPlainObject(new ArrayBuffer(0))).toBe(false);
18-expect(isPlainObject(Promise.resolve())).toBe(false);
19-expect(isPlainObject(Object.create(null))).toBe(true);
20-expect(isPlainObject(new Intl.Locale("en"))).toBe(false);
21-// eslint-disable-next-line no-new-object
22-expect(isPlainObject(new Object({ prop: true }))).toBe(true);
23-expect(isPlainObject(new (class Class {})())).toBe(false);
24-expect(isPlainObject([])).toBe(false);
25-expect(isPlainObject(/regexp/)).toBe(false);
26-expect(isPlainObject(new Error("test"))).toBe(false);
27-expect(isPlainObject(new Date())).toBe(false);
28-expect(
29-isPlainObject(
30-(function () {
31-// eslint-disable-next-line prefer-rest-params
32-return arguments;
33-})(),
34-),
35-).toBe(false);
36-// expect(isPlainObject(new Proxy({}, {}))).toBe(false); // TODO
5+describe("isPlainObject", () => {
6+it("plain objects", () => {
7+expect(isPlainObject({})).toBe(true);
8+expect(isPlainObject({ prop: true })).toBe(true);
9+expect(isPlainObject({ constructor: true })).toBe(true);
10+expect(isPlainObject({ __proto__: true })).toBe(true);
11+expect(isPlainObject(new Proxy({}, {}))).toBe(true);
12+});
13+14+it("non plain objects", () => {
15+expect(isPlainObject(undefined)).toBe(false);
16+expect(isPlainObject(0)).toBe(false);
17+expect(isPlainObject(0n)).toBe(false);
18+expect(isPlainObject("")).toBe(false);
19+expect(isPlainObject(true)).toBe(false);
20+expect(isPlainObject(Symbol(""))).toBe(false);
21+expect(isPlainObject(() => {})).toBe(false);
22+expect(isPlainObject(function namedFunc() {})).toBe(false);
23+expect(isPlainObject(null)).toBe(false);
24+expect(isPlainObject({})).toBe(true);
25+expect(isPlainObject(Math)).toBe(false);
26+expect(isPlainObject(new Set([]))).toBe(false);
27+expect(isPlainObject(new ArrayBuffer(0))).toBe(false);
28+expect(isPlainObject(Promise.resolve())).toBe(false);
29+expect(isPlainObject(Object.create(null))).toBe(true);
30+expect(isPlainObject(new Intl.Locale("en"))).toBe(false);
31+// eslint-disable-next-line no-new-object
32+expect(isPlainObject(new Object({ prop: true }))).toBe(true);
33+expect(isPlainObject(new (class Class {})())).toBe(false);
34+expect(isPlainObject([])).toBe(false);
35+expect(isPlainObject(/regexp/)).toBe(false);
36+expect(isPlainObject(new Error("test"))).toBe(false);
37+expect(isPlainObject(new Date())).toBe(false);
38+expect(
39+isPlainObject(
40+(function () {
41+// eslint-disable-next-line prefer-rest-params
42+return arguments;
43+})(),
44+),
45+).toBe(false);
46+expect(isPlainObject({ [Symbol.toStringTag]: true })).toBe(false);
47+expect(isPlainObject({ [Symbol.iterator]: true })).toBe(false);
48+});
3749});