2021-07-05 19:31:51 +00:00
|
|
|
var describe;
|
|
|
|
var test;
|
|
|
|
var expect;
|
2022-12-02 16:25:40 +00:00
|
|
|
var withinSameSecond;
|
2020-04-13 17:31:13 +00:00
|
|
|
|
2020-07-03 21:36:58 +00:00
|
|
|
// Stores the results of each test and suite. Has a terrible
|
|
|
|
// name to avoid name collision.
|
2021-07-05 19:31:51 +00:00
|
|
|
var __TestResults__ = {};
|
2020-04-19 22:01:45 +00:00
|
|
|
|
2020-07-05 03:23:46 +00:00
|
|
|
// So test names like "toString" don't automatically produce an error
|
|
|
|
Object.setPrototypeOf(__TestResults__, null);
|
|
|
|
|
2020-07-03 21:36:58 +00:00
|
|
|
// This array is used to communicate with the C++ program. It treats
|
|
|
|
// each message in this array as a separate message. Has a terrible
|
|
|
|
// name to avoid name collision.
|
2021-07-05 19:31:51 +00:00
|
|
|
var __UserOutput__ = [];
|
2020-05-26 18:31:22 +00:00
|
|
|
|
2020-07-03 21:36:58 +00:00
|
|
|
// We also rebind console.log here to use the array above
|
|
|
|
console.log = (...args) => {
|
2020-07-06 14:37:45 +00:00
|
|
|
__UserOutput__.push(args.join(" "));
|
2020-04-21 18:21:26 +00:00
|
|
|
};
|
|
|
|
|
2020-07-04 01:09:35 +00:00
|
|
|
class ExpectationError extends Error {
|
2021-04-23 18:29:21 +00:00
|
|
|
constructor(message) {
|
|
|
|
super(message);
|
2020-07-06 14:37:45 +00:00
|
|
|
this.name = "ExpectationError";
|
|
|
|
}
|
2020-07-04 01:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 21:36:58 +00:00
|
|
|
// Use an IIFE to avoid polluting the global namespace as much as possible
|
|
|
|
(() => {
|
2020-07-06 14:37:45 +00:00
|
|
|
const deepEquals = (a, b) => {
|
2024-06-02 04:43:35 +00:00
|
|
|
if (Object.is(a, b)) return true; // Handles identical references and primitives
|
|
|
|
if ((a !== null && b === null) || (a === null && b !== null)) return false;
|
2020-07-06 14:37:45 +00:00
|
|
|
if (Array.isArray(a)) return Array.isArray(b) && deepArrayEquals(a, b);
|
|
|
|
if (typeof a === "object") return typeof b === "object" && deepObjectEquals(a, b);
|
|
|
|
};
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
const deepArrayEquals = (a, b) => {
|
|
|
|
if (a.length !== b.length) return false;
|
|
|
|
for (let i = 0; i < a.length; ++i) {
|
|
|
|
if (!deepEquals(a[i], b[i])) return false;
|
|
|
|
}
|
2024-06-02 04:43:35 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
return true;
|
|
|
|
};
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
const deepObjectEquals = (a, b) => {
|
2024-06-02 04:43:35 +00:00
|
|
|
const keysA = Reflect.ownKeys(a);
|
|
|
|
const keysB = Reflect.ownKeys(b);
|
|
|
|
|
|
|
|
if (keysA.length !== keysB.length) return false;
|
|
|
|
for (let key of keysA) {
|
2020-07-06 14:37:45 +00:00
|
|
|
if (!deepEquals(a[key], b[key])) return false;
|
|
|
|
}
|
2024-06-02 04:43:35 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
return true;
|
|
|
|
};
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2021-06-27 21:17:05 +00:00
|
|
|
const valueToString = value => {
|
|
|
|
try {
|
2022-02-16 10:14:57 +00:00
|
|
|
if (value === 0 && 1 / value < 0) {
|
|
|
|
return "-0";
|
|
|
|
}
|
2021-06-27 21:17:05 +00:00
|
|
|
return String(value);
|
|
|
|
} catch {
|
|
|
|
// e.g for objects without a prototype, the above throws.
|
|
|
|
return Object.prototype.toString.call(value);
|
|
|
|
}
|
|
|
|
};
|
2021-06-16 19:44:53 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
class Expector {
|
|
|
|
constructor(target, inverted) {
|
|
|
|
this.target = target;
|
|
|
|
this.inverted = !!inverted;
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
get not() {
|
|
|
|
return new Expector(this.target, !this.inverted);
|
|
|
|
}
|
2020-07-04 17:09:48 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toBe(value) {
|
|
|
|
this.__doMatcher(() => {
|
2020-11-11 22:01:33 +00:00
|
|
|
this.__expect(
|
|
|
|
Object.is(this.target, value),
|
2021-06-16 19:44:53 +00:00
|
|
|
() =>
|
|
|
|
`toBe: expected _${valueToString(value)}_, got _${valueToString(
|
|
|
|
this.target
|
|
|
|
)}_`
|
2020-11-11 22:01:33 +00:00
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
|
|
|
}
|
2020-07-04 17:09:48 +00:00
|
|
|
|
2023-08-02 23:22:54 +00:00
|
|
|
toBeCloseTo(value, precision = 5) {
|
2020-11-11 22:01:33 +00:00
|
|
|
this.__expect(
|
|
|
|
typeof this.target === "number",
|
2023-08-02 22:19:10 +00:00
|
|
|
() => `toBeCloseTo: expected target of type number, got ${typeof this.target}`
|
2020-11-11 22:01:33 +00:00
|
|
|
);
|
|
|
|
this.__expect(
|
|
|
|
typeof value === "number",
|
2021-04-23 18:12:10 +00:00
|
|
|
() => `toBeCloseTo: expected argument of type number, got ${typeof value}`
|
2020-11-11 22:01:33 +00:00
|
|
|
);
|
2023-08-02 23:22:54 +00:00
|
|
|
this.__expect(
|
|
|
|
typeof precision === "number",
|
|
|
|
() => `toBeCloseTo: expected precision of type number, got ${typeof precision}`
|
|
|
|
);
|
2020-07-04 01:09:35 +00:00
|
|
|
|
2023-08-02 23:22:54 +00:00
|
|
|
const epsilon = 10 ** -precision / 2;
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
2023-08-02 23:22:54 +00:00
|
|
|
this.__expect(Math.abs(this.target - value) < epsilon);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toHaveLength(length) {
|
2020-11-11 22:01:33 +00:00
|
|
|
this.__expect(
|
|
|
|
typeof this.target.length === "number",
|
|
|
|
() => "toHaveLength: target.length not of type number"
|
|
|
|
);
|
2020-07-04 01:09:35 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(Object.is(this.target.length, length));
|
|
|
|
});
|
|
|
|
}
|
2020-07-04 01:09:35 +00:00
|
|
|
|
2021-06-08 21:08:47 +00:00
|
|
|
toHaveSize(size) {
|
|
|
|
this.__expect(
|
|
|
|
typeof this.target.size === "number",
|
|
|
|
() => "toHaveSize: target.size not of type number"
|
|
|
|
);
|
|
|
|
|
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(Object.is(this.target.size, size));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toHaveProperty(property, value) {
|
|
|
|
this.__doMatcher(() => {
|
|
|
|
let object = this.target;
|
|
|
|
|
|
|
|
if (typeof property === "string" && property.includes(".")) {
|
|
|
|
let propertyArray = [];
|
|
|
|
|
|
|
|
while (property.includes(".")) {
|
|
|
|
let index = property.indexOf(".");
|
|
|
|
propertyArray.push(property.substring(0, index));
|
|
|
|
if (index + 1 >= property.length) break;
|
|
|
|
property = property.substring(index + 1, property.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
propertyArray.push(property);
|
|
|
|
|
|
|
|
property = propertyArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(property)) {
|
|
|
|
for (let key of property) {
|
2022-12-20 18:38:42 +00:00
|
|
|
this.__expect(
|
|
|
|
object !== undefined && object !== null,
|
|
|
|
"got undefined or null as array key"
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
object = object[key];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
object = object[property];
|
|
|
|
}
|
|
|
|
|
2022-12-20 18:38:42 +00:00
|
|
|
this.__expect(object !== undefined, "should not be undefined");
|
|
|
|
if (value !== undefined)
|
|
|
|
this.__expect(
|
|
|
|
deepEquals(object, value),
|
|
|
|
`value does not equal property ${valueToString(object)} vs ${valueToString(
|
|
|
|
value
|
|
|
|
)}`
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
|
|
|
}
|
2020-07-04 01:09:35 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toBeDefined() {
|
|
|
|
this.__doMatcher(() => {
|
2021-04-23 18:12:10 +00:00
|
|
|
this.__expect(
|
|
|
|
this.target !== undefined,
|
|
|
|
() => "toBeDefined: expected target to be defined, got undefined"
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
|
|
|
}
|
2020-07-04 01:09:35 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toBeInstanceOf(class_) {
|
|
|
|
this.__doMatcher(() => {
|
2022-12-20 18:38:42 +00:00
|
|
|
this.__expect(
|
|
|
|
this.target instanceof class_,
|
|
|
|
`Expected ${valueToString(this.target)} to be instance of ${class_.name}`
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
2020-07-05 16:27:00 +00:00
|
|
|
}
|
2020-07-04 01:09:35 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toBeNull() {
|
|
|
|
this.__doMatcher(() => {
|
2022-12-20 18:38:42 +00:00
|
|
|
this.__expect(
|
|
|
|
this.target === null,
|
|
|
|
`Expected target to be null got ${valueToString(this.target)}`
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
2020-07-05 16:27:00 +00:00
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toBeUndefined() {
|
|
|
|
this.__doMatcher(() => {
|
2020-11-11 22:01:33 +00:00
|
|
|
this.__expect(
|
|
|
|
this.target === undefined,
|
2021-04-23 18:12:10 +00:00
|
|
|
() =>
|
2021-06-16 19:44:53 +00:00
|
|
|
`toBeUndefined: expected target to be undefined, got _${valueToString(
|
2021-04-23 18:12:10 +00:00
|
|
|
this.target
|
|
|
|
)}_`
|
2020-11-11 22:01:33 +00:00
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toBeNaN() {
|
|
|
|
this.__doMatcher(() => {
|
2020-11-11 22:01:33 +00:00
|
|
|
this.__expect(
|
|
|
|
isNaN(this.target),
|
2021-06-16 19:44:53 +00:00
|
|
|
() => `toBeNaN: expected target to be NaN, got _${valueToString(this.target)}_`
|
2020-11-11 22:01:33 +00:00
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2022-12-20 18:41:23 +00:00
|
|
|
toBeTrue(customDetails = undefined) {
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
2021-05-13 22:34:03 +00:00
|
|
|
this.__expect(
|
|
|
|
this.target === true,
|
2021-06-16 19:44:53 +00:00
|
|
|
() =>
|
2022-12-20 18:41:23 +00:00
|
|
|
`toBeTrue: expected target to be true, got _${valueToString(this.target)}_${
|
|
|
|
customDetails ? ` (${customDetails})` : ""
|
|
|
|
}`
|
2021-05-13 22:34:03 +00:00
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2022-12-20 18:41:23 +00:00
|
|
|
toBeFalse(customDetails = undefined) {
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
2021-05-13 22:34:03 +00:00
|
|
|
this.__expect(
|
|
|
|
this.target === false,
|
2021-06-16 19:44:53 +00:00
|
|
|
() =>
|
2021-06-28 17:22:42 +00:00
|
|
|
`toBeFalse: expected target to be false, got _${valueToString(
|
|
|
|
this.target
|
2022-12-20 18:41:23 +00:00
|
|
|
)}_${customDetails ?? ""}`
|
2021-05-13 22:34:03 +00:00
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
__validateNumericComparisonTypes(value) {
|
|
|
|
this.__expect(typeof this.target === "number" || typeof this.target === "bigint");
|
|
|
|
this.__expect(typeof value === "number" || typeof value === "bigint");
|
|
|
|
this.__expect(typeof this.target === typeof value);
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toBeLessThan(value) {
|
|
|
|
this.__validateNumericComparisonTypes(value);
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(this.target < value);
|
|
|
|
});
|
|
|
|
}
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toBeLessThanOrEqual(value) {
|
|
|
|
this.__validateNumericComparisonTypes(value);
|
2020-07-04 17:09:48 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(this.target <= value);
|
|
|
|
});
|
|
|
|
}
|
2020-07-04 17:09:48 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toBeGreaterThan(value) {
|
|
|
|
this.__validateNumericComparisonTypes(value);
|
2020-07-04 17:09:48 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(this.target > value);
|
|
|
|
});
|
|
|
|
}
|
2020-07-04 17:09:48 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toBeGreaterThanOrEqual(value) {
|
|
|
|
this.__validateNumericComparisonTypes(value);
|
2020-07-04 17:09:48 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(this.target >= value);
|
|
|
|
});
|
|
|
|
}
|
2020-07-04 17:09:48 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toContain(item) {
|
|
|
|
this.__doMatcher(() => {
|
|
|
|
for (let element of this.target) {
|
|
|
|
if (item === element) return;
|
|
|
|
}
|
2020-07-04 17:09:48 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
throw new ExpectationError();
|
|
|
|
});
|
|
|
|
}
|
2020-07-04 17:09:48 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toContainEqual(item) {
|
|
|
|
this.__doMatcher(() => {
|
|
|
|
for (let element of this.target) {
|
|
|
|
if (deepEquals(item, element)) return;
|
|
|
|
}
|
2020-07-04 17:09:48 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
throw new ExpectationError();
|
|
|
|
});
|
|
|
|
}
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toEqual(value) {
|
|
|
|
this.__doMatcher(() => {
|
2022-02-22 04:17:48 +00:00
|
|
|
this.__expect(
|
|
|
|
deepEquals(this.target, value),
|
|
|
|
() =>
|
|
|
|
`Expected _${valueToString(value)}_, but got _${valueToString(
|
|
|
|
this.target
|
|
|
|
)}_`
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
2020-07-05 16:27:00 +00:00
|
|
|
}
|
2020-07-04 01:09:35 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toThrow(value) {
|
|
|
|
this.__expect(typeof this.target === "function");
|
|
|
|
this.__expect(
|
|
|
|
typeof value === "string" ||
|
|
|
|
typeof value === "function" ||
|
|
|
|
typeof value === "object" ||
|
|
|
|
value === undefined
|
|
|
|
);
|
|
|
|
|
|
|
|
this.__doMatcher(() => {
|
|
|
|
let threw = true;
|
|
|
|
try {
|
|
|
|
this.target();
|
|
|
|
threw = false;
|
|
|
|
} catch (e) {
|
|
|
|
if (typeof value === "string") {
|
2022-10-11 23:34:31 +00:00
|
|
|
this.__expect(
|
|
|
|
e.message.includes(value),
|
|
|
|
`Expected ${this.target.toString()} to throw and message to include "${value}" but message "${
|
|
|
|
e.message
|
|
|
|
}" did not contain it`
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
} else if (typeof value === "function") {
|
2022-10-11 23:34:31 +00:00
|
|
|
this.__expect(
|
|
|
|
e instanceof value,
|
|
|
|
`Expected ${this.target.toString()} to throw and be of type ${value} but it threw ${e}`
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
} else if (typeof value === "object") {
|
2022-10-11 23:34:31 +00:00
|
|
|
this.__expect(
|
|
|
|
e.message === value.message,
|
|
|
|
`Expected ${this.target.toString()} to throw and message to be ${value} but it threw with message ${
|
|
|
|
e.message
|
|
|
|
}`
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-11 23:34:31 +00:00
|
|
|
this.__expect(
|
|
|
|
threw,
|
|
|
|
`Expected ${this.target.toString()} to throw but it didn't throw anything`
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
pass(message) {
|
|
|
|
// FIXME: This does nothing. If we want to implement things
|
|
|
|
// like assertion count, this will have to do something
|
2020-07-05 16:27:00 +00:00
|
|
|
}
|
2020-07-04 01:09:35 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
// jest-extended
|
|
|
|
fail(message) {
|
|
|
|
this.__doMatcher(() => {
|
2020-08-22 01:53:54 +00:00
|
|
|
this.__expect(false, message);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
// jest-extended
|
|
|
|
toThrowWithMessage(class_, message) {
|
|
|
|
this.__expect(typeof this.target === "function");
|
|
|
|
this.__expect(class_ !== undefined);
|
|
|
|
this.__expect(message !== undefined);
|
|
|
|
|
|
|
|
this.__doMatcher(() => {
|
|
|
|
try {
|
|
|
|
this.target();
|
2021-05-13 22:33:15 +00:00
|
|
|
this.__expect(false, () => "toThrowWithMessage: target function did not throw");
|
2020-07-06 14:37:45 +00:00
|
|
|
} catch (e) {
|
2021-05-11 21:41:35 +00:00
|
|
|
this.__expect(
|
|
|
|
e instanceof class_,
|
|
|
|
() =>
|
2021-06-16 19:44:53 +00:00
|
|
|
`toThrowWithMessage: expected error to be instance of ${valueToString(
|
2021-05-13 22:30:50 +00:00
|
|
|
class_.name
|
2021-06-16 19:44:53 +00:00
|
|
|
)}, got ${valueToString(e.name)}`
|
2021-05-11 21:41:35 +00:00
|
|
|
);
|
|
|
|
this.__expect(
|
|
|
|
e.message.includes(message),
|
|
|
|
() =>
|
2021-06-16 19:44:53 +00:00
|
|
|
`toThrowWithMessage: expected error message to include _${valueToString(
|
2021-05-13 22:30:50 +00:00
|
|
|
message
|
2021-06-16 19:44:53 +00:00
|
|
|
)}_, got _${valueToString(e.message)}_`
|
2021-05-11 21:41:35 +00:00
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
// Test for syntax errors; target must be a string
|
|
|
|
toEval() {
|
|
|
|
this.__expect(typeof this.target === "string");
|
2020-12-26 15:24:24 +00:00
|
|
|
const success = canParseSource(this.target);
|
2021-11-30 00:00:06 +00:00
|
|
|
this.__expect(
|
|
|
|
this.inverted ? !success : success,
|
|
|
|
() =>
|
2022-11-17 09:21:45 +00:00
|
|
|
`Expected _${valueToString(this.target)}_ ` +
|
2021-11-30 00:00:06 +00:00
|
|
|
(this.inverted ? "not to eval but it did" : "to eval but it didn't")
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
// Must compile regardless of inverted-ness
|
|
|
|
toEvalTo(value) {
|
|
|
|
this.__expect(typeof this.target === "string");
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
let result;
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
try {
|
2021-06-18 18:09:17 +00:00
|
|
|
result = eval(this.target);
|
2020-07-06 14:37:45 +00:00
|
|
|
} catch (e) {
|
2021-11-30 00:00:06 +00:00
|
|
|
throw new ExpectationError(
|
|
|
|
`Expected _${valueToString(this.target)}_ to eval but it failed with ${e}`
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
2021-11-30 00:00:06 +00:00
|
|
|
this.__expect(
|
|
|
|
deepEquals(value, result),
|
|
|
|
() =>
|
|
|
|
`Expected _${valueToString(this.target)}_ to eval to ` +
|
|
|
|
`_${valueToString(value)}_ but got _${valueToString(result)}_`
|
|
|
|
);
|
2020-07-06 14:37:45 +00:00
|
|
|
});
|
2020-07-05 16:27:00 +00:00
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toHaveConfigurableProperty(property) {
|
|
|
|
this.__expect(this.target !== undefined && this.target !== null);
|
|
|
|
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
|
|
|
this.__expect(d !== undefined);
|
2020-07-05 16:27:00 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(d.configurable);
|
|
|
|
});
|
2020-07-05 16:27:00 +00:00
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toHaveEnumerableProperty(property) {
|
|
|
|
this.__expect(this.target !== undefined && this.target !== null);
|
|
|
|
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
|
|
|
this.__expect(d !== undefined);
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(d.enumerable);
|
|
|
|
});
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toHaveWritableProperty(property) {
|
|
|
|
this.__expect(this.target !== undefined && this.target !== null);
|
|
|
|
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
|
|
|
this.__expect(d !== undefined);
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(d.writable);
|
|
|
|
});
|
|
|
|
}
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toHaveValueProperty(property, value) {
|
|
|
|
this.__expect(this.target !== undefined && this.target !== null);
|
|
|
|
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
|
|
|
this.__expect(d !== undefined);
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(d.value !== undefined);
|
|
|
|
if (value !== undefined) this.__expect(deepEquals(value, d.value));
|
|
|
|
});
|
|
|
|
}
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toHaveGetterProperty(property) {
|
|
|
|
this.__expect(this.target !== undefined && this.target !== null);
|
|
|
|
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
|
|
|
this.__expect(d !== undefined);
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(d.get !== undefined);
|
|
|
|
});
|
|
|
|
}
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
toHaveSetterProperty(property) {
|
|
|
|
this.__expect(this.target !== undefined && this.target !== null);
|
|
|
|
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
|
|
|
this.__expect(d !== undefined);
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(d.set !== undefined);
|
|
|
|
});
|
|
|
|
}
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2022-02-10 10:13:53 +00:00
|
|
|
toBeIteratorResultWithValue(value) {
|
|
|
|
this.__expect(this.target !== undefined && this.target !== null);
|
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(
|
|
|
|
this.target.done === false,
|
|
|
|
() =>
|
|
|
|
`toGiveIteratorResultWithValue: expected 'done' to be _false_ got ${valueToString(
|
|
|
|
this.target.done
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
this.__expect(
|
|
|
|
deepEquals(value, this.target.value),
|
|
|
|
() =>
|
|
|
|
`toGiveIteratorResultWithValue: expected 'value' to be _${valueToString(
|
|
|
|
value
|
|
|
|
)}_ got ${valueToString(this.target.value)}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
toBeIteratorResultDone() {
|
|
|
|
this.__expect(this.target !== undefined && this.target !== null);
|
|
|
|
this.__doMatcher(() => {
|
|
|
|
this.__expect(
|
|
|
|
this.target.done === true,
|
|
|
|
() =>
|
|
|
|
`toGiveIteratorResultDone: expected 'done' to be _true_ got ${valueToString(
|
|
|
|
this.target.done
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
this.__expect(
|
|
|
|
this.target.value === undefined,
|
|
|
|
() =>
|
|
|
|
`toGiveIteratorResultDone: expected 'value' to be _undefined_ got ${valueToString(
|
|
|
|
this.target.value
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
__doMatcher(matcher) {
|
|
|
|
if (!this.inverted) {
|
|
|
|
matcher();
|
|
|
|
} else {
|
|
|
|
let threw = false;
|
|
|
|
try {
|
|
|
|
matcher();
|
|
|
|
} catch (e) {
|
|
|
|
if (e.name === "ExpectationError") threw = true;
|
|
|
|
}
|
2020-08-22 01:53:54 +00:00
|
|
|
if (!threw) throw new ExpectationError("not: test didn't fail");
|
2020-07-06 14:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2020-08-22 01:53:54 +00:00
|
|
|
__expect(value, details) {
|
|
|
|
if (value !== true) {
|
2020-11-11 22:01:33 +00:00
|
|
|
if (details !== undefined) {
|
2022-03-19 18:54:21 +00:00
|
|
|
if (details instanceof Function) throw new ExpectationError(details());
|
|
|
|
else throw new ExpectationError(details);
|
2020-11-11 22:01:33 +00:00
|
|
|
} else {
|
|
|
|
throw new ExpectationError();
|
|
|
|
}
|
2020-08-22 01:53:54 +00:00
|
|
|
}
|
2020-07-06 14:37:45 +00:00
|
|
|
}
|
2020-07-04 06:13:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
expect = value => new Expector(value);
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
// describe is able to lump test results inside of it by using this context
|
|
|
|
// variable. Top level tests have the default suite message
|
|
|
|
const defaultSuiteMessage = "__$$TOP_LEVEL$$__";
|
|
|
|
let suiteMessage = defaultSuiteMessage;
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
describe = (message, callback) => {
|
|
|
|
suiteMessage = message;
|
2021-06-03 22:53:33 +00:00
|
|
|
if (!__TestResults__[suiteMessage]) __TestResults__[suiteMessage] = {};
|
|
|
|
try {
|
|
|
|
callback();
|
|
|
|
} catch (e) {
|
|
|
|
__TestResults__[suiteMessage][defaultSuiteMessage] = {
|
|
|
|
result: "fail",
|
|
|
|
details: String(e),
|
2022-03-08 03:39:41 +00:00
|
|
|
duration: 0,
|
2021-06-03 22:53:33 +00:00
|
|
|
};
|
|
|
|
}
|
2020-07-06 14:37:45 +00:00
|
|
|
suiteMessage = defaultSuiteMessage;
|
|
|
|
};
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
test = (message, callback) => {
|
|
|
|
if (!__TestResults__[suiteMessage]) __TestResults__[suiteMessage] = {};
|
|
|
|
|
|
|
|
const suite = __TestResults__[suiteMessage];
|
2021-05-05 14:54:39 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(suite, message)) {
|
2020-07-06 14:37:45 +00:00
|
|
|
suite[message] = {
|
|
|
|
result: "fail",
|
2021-03-31 20:21:36 +00:00
|
|
|
details: "Another test with the same message did already run",
|
2022-03-08 03:39:41 +00:00
|
|
|
duration: 0,
|
2020-07-06 14:37:45 +00:00
|
|
|
};
|
|
|
|
return;
|
|
|
|
}
|
2020-07-04 06:13:06 +00:00
|
|
|
|
2024-11-16 17:26:09 +00:00
|
|
|
const start = Date.now();
|
|
|
|
const time_ms = () => Date.now() - start;
|
|
|
|
|
2020-07-05 16:27:00 +00:00
|
|
|
try {
|
2020-07-06 14:37:45 +00:00
|
|
|
callback();
|
|
|
|
suite[message] = {
|
|
|
|
result: "pass",
|
2024-11-16 17:26:09 +00:00
|
|
|
duration: time_ms(),
|
2020-07-06 14:37:45 +00:00
|
|
|
};
|
2020-07-05 16:27:00 +00:00
|
|
|
} catch (e) {
|
2020-07-06 14:37:45 +00:00
|
|
|
suite[message] = {
|
|
|
|
result: "fail",
|
2020-08-22 01:53:54 +00:00
|
|
|
details: String(e),
|
2024-11-16 17:26:09 +00:00
|
|
|
duration: time_ms(),
|
2020-07-06 14:37:45 +00:00
|
|
|
};
|
2020-07-03 21:36:58 +00:00
|
|
|
}
|
2020-07-06 14:37:45 +00:00
|
|
|
};
|
2020-07-03 21:36:58 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
test.skip = (message, callback) => {
|
|
|
|
if (typeof callback !== "function")
|
|
|
|
throw new Error("test.skip has invalid second argument (must be a function)");
|
2020-07-04 19:57:12 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
if (!__TestResults__[suiteMessage]) __TestResults__[suiteMessage] = {};
|
2020-07-04 19:57:12 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
const suite = __TestResults__[suiteMessage];
|
2021-05-05 14:54:39 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(suite, message)) {
|
|
|
|
suite[message] = {
|
|
|
|
result: "fail",
|
|
|
|
details: "Another test with the same message did already run",
|
2022-03-08 03:39:41 +00:00
|
|
|
duration: 0,
|
2021-05-05 14:54:39 +00:00
|
|
|
};
|
|
|
|
return;
|
|
|
|
}
|
2020-07-04 19:57:12 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
suite[message] = {
|
|
|
|
result: "skip",
|
2022-03-08 03:39:41 +00:00
|
|
|
duration: 0,
|
2020-07-06 14:37:45 +00:00
|
|
|
};
|
2020-07-05 16:27:00 +00:00
|
|
|
};
|
2022-12-02 16:25:40 +00:00
|
|
|
|
2023-07-22 04:22:47 +00:00
|
|
|
test.xfail = (message, callback) => {
|
|
|
|
if (!__TestResults__[suiteMessage]) __TestResults__[suiteMessage] = {};
|
|
|
|
|
|
|
|
const suite = __TestResults__[suiteMessage];
|
|
|
|
if (Object.prototype.hasOwnProperty.call(suite, message)) {
|
|
|
|
suite[message] = {
|
|
|
|
result: "fail",
|
|
|
|
details: "Another test with the same message did already run",
|
|
|
|
duration: 0,
|
|
|
|
};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-16 17:26:09 +00:00
|
|
|
const start = Date.now();
|
|
|
|
const time_ms = () => Date.now() - start;
|
|
|
|
|
2023-07-22 04:22:47 +00:00
|
|
|
try {
|
|
|
|
callback();
|
|
|
|
suite[message] = {
|
|
|
|
result: "fail",
|
|
|
|
details: "Expected test to fail, but it passed",
|
2024-11-16 17:26:09 +00:00
|
|
|
duration: time_ms(),
|
2023-07-22 04:22:47 +00:00
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
suite[message] = {
|
|
|
|
result: "xfail",
|
2024-11-16 17:26:09 +00:00
|
|
|
duration: time_ms(),
|
2023-07-22 04:22:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
test.xfailIf = (condition, message, callback) => {
|
|
|
|
condition ? test.xfail(message, callback) : test(message, callback);
|
|
|
|
};
|
|
|
|
|
2022-12-02 16:25:40 +00:00
|
|
|
withinSameSecond = callback => {
|
|
|
|
let callbackDuration;
|
|
|
|
for (let tries = 0; tries < 5; tries++) {
|
2024-11-16 17:26:09 +00:00
|
|
|
const start = Date.now();
|
2022-12-02 16:25:40 +00:00
|
|
|
const result = callback();
|
2024-11-16 17:26:09 +00:00
|
|
|
const end = Date.now();
|
|
|
|
|
|
|
|
if (start / 1000 != end / 1000) {
|
|
|
|
callbackDuration = end - start;
|
2022-12-02 16:25:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-11-16 17:26:09 +00:00
|
|
|
|
2022-12-02 16:25:40 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
throw new ExpectationError(
|
|
|
|
`Tried to execute callback '${callback}' 5 times within the same second but ` +
|
|
|
|
`failed. Make sure the callback does as little work as possible (the last run ` +
|
2024-11-16 17:26:09 +00:00
|
|
|
`took ${callbackDuration}ms) and the machine is not overloaded. If you see this ` +
|
2022-12-02 16:25:40 +00:00
|
|
|
`error appearing in the CI it is most likely a flaky failure!`
|
|
|
|
);
|
|
|
|
};
|
2020-07-03 21:36:58 +00:00
|
|
|
})();
|