vitest 0.21.1 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +61 -33
- package/dist/browser.d.ts +5 -5
- package/dist/browser.mjs +12 -10
- package/dist/{chunk-api-setup.7a6ba7fb.mjs → chunk-api-setup.5fc06d1d.mjs} +94 -91
- package/dist/chunk-constants.6196597b.mjs +284 -0
- package/dist/chunk-env-node.ceb43f1c.mjs +403 -0
- package/dist/{chunk-install-pkg.6c6dc0c2.mjs → chunk-install-pkg.e081fc1b.mjs} +2 -1
- package/dist/chunk-integrations-coverage.99c020eb.mjs +166 -0
- package/dist/{chunk-integrations-globals.44a8f047.mjs → chunk-integrations-globals.ef598c23.mjs} +8 -9
- package/dist/{chunk-magic-string.efe26975.mjs → chunk-magic-string.56b2b543.mjs} +30 -10
- package/dist/chunk-mock-date.0d86eaa5.mjs +332 -0
- package/dist/chunk-node-git.6f289b0a.mjs +84 -0
- package/dist/{chunk-runtime-chain.98d42d89.mjs → chunk-runtime-chain.2af36ddf.mjs} +507 -172
- package/dist/{chunk-runtime-error.87a2b5a2.mjs → chunk-runtime-error.ed9b4f70.mjs} +208 -76
- package/dist/{chunk-runtime-hooks.453f8858.mjs → chunk-runtime-hooks.75ce0575.mjs} +18 -12
- package/dist/{chunk-runtime-mocker.23b62bfa.mjs → chunk-runtime-mocker.fc76f21d.mjs} +18 -11
- package/dist/{chunk-runtime-rpc.b50ab560.mjs → chunk-runtime-rpc.3fe371e9.mjs} +1 -2
- package/dist/{chunk-utils-source-map.94107ee8.mjs → chunk-utils-source-map.70ee97e1.mjs} +11 -4
- package/dist/{chunk-vite-node-client.fdd9592c.mjs → chunk-vite-node-client.74ebe3d5.mjs} +97 -31
- package/dist/{chunk-vite-node-debug.09afb76f.mjs → chunk-vite-node-debug.2d8a1dc3.mjs} +3 -3
- package/dist/{chunk-vite-node-externalize.27aee038.mjs → chunk-vite-node-externalize.41bf722e.mjs} +644 -222
- package/dist/{chunk-vite-node-utils.f34df9d3.mjs → chunk-vite-node-utils.68573626.mjs} +60 -42
- package/dist/cli-wrapper.mjs +128 -0
- package/dist/cli.mjs +29 -20
- package/dist/config.cjs +5 -2
- package/dist/config.d.ts +8 -4
- package/dist/config.mjs +4 -3
- package/dist/entry.mjs +20 -15
- package/dist/environments.d.ts +23 -0
- package/dist/environments.mjs +3 -0
- package/dist/{global-60f880c6.d.ts → global-ea084c9f.d.ts} +627 -178
- package/dist/{index-4a906fa4.d.ts → index-5f09f4d0.d.ts} +3 -50
- package/dist/index.d.ts +6 -6
- package/dist/index.mjs +7 -6
- package/dist/loader.mjs +3 -3
- package/dist/node.d.ts +5 -4
- package/dist/node.mjs +19 -16
- package/dist/suite.mjs +6 -5
- package/dist/vendor-index.0557b03a.mjs +147 -0
- package/dist/vendor-index.13e3bda3.mjs +61 -0
- package/dist/{chunk-node-git.c2be9c49.mjs → vendor-index.4aeeb598.mjs} +4 -72
- package/dist/{vendor-index.61438b77.mjs → vendor-index.731a22f2.mjs} +1 -61
- package/dist/worker.mjs +20 -18
- package/package.json +19 -16
- package/vitest.mjs +1 -1
- package/dist/chunk-constants.26dc9f85.mjs +0 -38
- package/dist/chunk-defaults.02abff90.mjs +0 -680
- package/dist/chunk-mock-date.bc81a3ac.mjs +0 -555
- package/dist/chunk-utils-global.fa20c2f6.mjs +0 -5
- package/dist/mocker-5e2a8e41.d.ts +0 -3
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import util$1 from 'util';
|
|
2
|
-
import { i as isObject,
|
|
2
|
+
import { i as isObject, b as getCallLastIndex, s as slash, g as getWorkerState, c as getNames, d as assertTypes, e as getFullName, f as isRunningInTest, n as noop, h as isRunningInBenchmark } from './chunk-mock-date.0d86eaa5.mjs';
|
|
3
3
|
import * as chai$2 from 'chai';
|
|
4
4
|
import { expect, AssertionError, util } from 'chai';
|
|
5
5
|
import { c as commonjsGlobal } from './vendor-_commonjsHelpers.4da45ef5.mjs';
|
|
6
|
-
import { r as rpc } from './chunk-runtime-rpc.
|
|
6
|
+
import { r as rpc } from './chunk-runtime-rpc.3fe371e9.mjs';
|
|
7
7
|
import fs, { promises } from 'fs';
|
|
8
|
-
import {
|
|
9
|
-
import { g as
|
|
8
|
+
import { j as join, d as dirname, p as picocolors } from './chunk-constants.6196597b.mjs';
|
|
9
|
+
import { a as plugins_1, f as format_1, g as getOriginalPos, b as posToNumber, n as numberToPos, l as lineSplitRE, p as parseStacktrace, u as unifiedDiff, s as stringify, m as matcherUtils } from './chunk-utils-source-map.70ee97e1.mjs';
|
|
10
10
|
import { isMockFunction } from './spy.mjs';
|
|
11
11
|
import { s as safeSetTimeout, a as safeClearTimeout } from './chunk-utils-timers.b48455ed.mjs';
|
|
12
12
|
|
|
@@ -208,7 +208,11 @@ function keys(obj, hasKey2) {
|
|
|
208
208
|
if (hasKey2(obj, key))
|
|
209
209
|
keys2.push(key);
|
|
210
210
|
}
|
|
211
|
-
return keys2.concat(
|
|
211
|
+
return keys2.concat(
|
|
212
|
+
Object.getOwnPropertySymbols(obj).filter(
|
|
213
|
+
(symbol) => Object.getOwnPropertyDescriptor(obj, symbol).enumerable
|
|
214
|
+
)
|
|
215
|
+
);
|
|
212
216
|
}
|
|
213
217
|
function hasDefinedKey(obj, key) {
|
|
214
218
|
return hasKey(obj, key) && obj[key] !== void 0;
|
|
@@ -498,13 +502,15 @@ const removeExtraLineBreaks = (string) => string.length > 2 && string.startsWith
|
|
|
498
502
|
const escapeRegex = true;
|
|
499
503
|
const printFunctionName = false;
|
|
500
504
|
function serialize(val, indent = 2, formatOverrides = {}) {
|
|
501
|
-
return normalizeNewlines(
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
505
|
+
return normalizeNewlines(
|
|
506
|
+
format_1(val, {
|
|
507
|
+
escapeRegex,
|
|
508
|
+
indent,
|
|
509
|
+
plugins: getSerializers(),
|
|
510
|
+
printFunctionName,
|
|
511
|
+
...formatOverrides
|
|
512
|
+
})
|
|
513
|
+
);
|
|
508
514
|
}
|
|
509
515
|
function escapeBacktickString(str) {
|
|
510
516
|
return str.replace(/`|\\|\${/g, "\\$&");
|
|
@@ -523,7 +529,9 @@ function normalizeNewlines(string) {
|
|
|
523
529
|
}
|
|
524
530
|
async function saveSnapshotFile(snapshotData, snapshotPath) {
|
|
525
531
|
var _a, _b;
|
|
526
|
-
const snapshots = Object.keys(snapshotData).sort(naturalCompare$1.exports).map(
|
|
532
|
+
const snapshots = Object.keys(snapshotData).sort(naturalCompare$1.exports).map(
|
|
533
|
+
(key) => `exports[${printBacktickString(key)}] = ${printBacktickString(normalizeNewlines(snapshotData[key]))};`
|
|
534
|
+
);
|
|
527
535
|
const content = `${writeSnapshotVersion()}
|
|
528
536
|
|
|
529
537
|
${snapshots.join("\n\n")}
|
|
@@ -532,7 +540,11 @@ ${snapshots.join("\n\n")}
|
|
|
532
540
|
if (skipWriting)
|
|
533
541
|
return;
|
|
534
542
|
ensureDirectoryExists(snapshotPath);
|
|
535
|
-
await ((_b = fs) == null ? void 0 : _b.promises.writeFile(
|
|
543
|
+
await ((_b = fs) == null ? void 0 : _b.promises.writeFile(
|
|
544
|
+
snapshotPath,
|
|
545
|
+
content,
|
|
546
|
+
"utf-8"
|
|
547
|
+
));
|
|
536
548
|
}
|
|
537
549
|
function prepareExpected(expected) {
|
|
538
550
|
function findStartIndent() {
|
|
@@ -588,7 +600,7 @@ function deepMergeSnapshot(target, source) {
|
|
|
588
600
|
}
|
|
589
601
|
|
|
590
602
|
async function saveInlineSnapshots(snapshots) {
|
|
591
|
-
const MagicString = (await import('./chunk-magic-string.
|
|
603
|
+
const MagicString = (await import('./chunk-magic-string.56b2b543.mjs')).default;
|
|
592
604
|
const files = new Set(snapshots.map((i) => i.file));
|
|
593
605
|
await Promise.all(Array.from(files).map(async (file) => {
|
|
594
606
|
const map = await rpc().getSourceMap(file);
|
|
@@ -684,7 +696,10 @@ class SnapshotState {
|
|
|
684
696
|
constructor(testFilePath, snapshotPath, options) {
|
|
685
697
|
this.testFilePath = testFilePath;
|
|
686
698
|
this.snapshotPath = snapshotPath;
|
|
687
|
-
const { data, dirty } = getSnapshotData(
|
|
699
|
+
const { data, dirty } = getSnapshotData(
|
|
700
|
+
this.snapshotPath,
|
|
701
|
+
options.updateSnapshot
|
|
702
|
+
);
|
|
688
703
|
this._initialData = data;
|
|
689
704
|
this._snapshotData = data;
|
|
690
705
|
this._dirty = dirty;
|
|
@@ -723,8 +738,10 @@ class SnapshotState {
|
|
|
723
738
|
stacks.forEach((i) => i.file = slash(i.file));
|
|
724
739
|
const stack = this._inferInlineSnapshotStack(stacks);
|
|
725
740
|
if (!stack) {
|
|
726
|
-
throw new Error(
|
|
727
|
-
|
|
741
|
+
throw new Error(
|
|
742
|
+
`Vitest: Couldn't infer stack frame for inline snapshot.
|
|
743
|
+
${JSON.stringify(stacks)}`
|
|
744
|
+
);
|
|
728
745
|
}
|
|
729
746
|
stack.column--;
|
|
730
747
|
this._inlineSnapshots.push({
|
|
@@ -883,7 +900,14 @@ class SnapshotClient {
|
|
|
883
900
|
this.saveCurrent();
|
|
884
901
|
const filePath = this.test.file.filepath;
|
|
885
902
|
if (!this.getSnapshotState(test)) {
|
|
886
|
-
this.snapshotStateMap.set(
|
|
903
|
+
this.snapshotStateMap.set(
|
|
904
|
+
filePath,
|
|
905
|
+
new SnapshotState(
|
|
906
|
+
filePath,
|
|
907
|
+
await rpc().resolveSnapshotPath(filePath),
|
|
908
|
+
getWorkerState().config.snapshotOptions
|
|
909
|
+
)
|
|
910
|
+
);
|
|
887
911
|
}
|
|
888
912
|
this.snapshotState = this.getSnapshotState(test);
|
|
889
913
|
}
|
|
@@ -980,79 +1004,95 @@ const getErrorString = (expected, promise) => {
|
|
|
980
1004
|
} catch (e) {
|
|
981
1005
|
return getErrorMessage(e);
|
|
982
1006
|
}
|
|
983
|
-
throw new Error("snapshot function didn't
|
|
1007
|
+
throw new Error("snapshot function didn't throw");
|
|
984
1008
|
};
|
|
985
1009
|
const SnapshotPlugin = (chai, utils) => {
|
|
986
1010
|
for (const key of ["matchSnapshot", "toMatchSnapshot"]) {
|
|
987
|
-
utils.addMethod(
|
|
1011
|
+
utils.addMethod(
|
|
1012
|
+
chai.Assertion.prototype,
|
|
1013
|
+
key,
|
|
1014
|
+
function(properties, message) {
|
|
1015
|
+
const expected = utils.flag(this, "object");
|
|
1016
|
+
const test = utils.flag(this, "vitest-test");
|
|
1017
|
+
if (typeof properties === "string" && typeof message === "undefined") {
|
|
1018
|
+
message = properties;
|
|
1019
|
+
properties = void 0;
|
|
1020
|
+
}
|
|
1021
|
+
const errorMessage = utils.flag(this, "message");
|
|
1022
|
+
getSnapshotClient().assert({
|
|
1023
|
+
received: expected,
|
|
1024
|
+
test,
|
|
1025
|
+
message,
|
|
1026
|
+
isInline: false,
|
|
1027
|
+
properties,
|
|
1028
|
+
errorMessage
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
);
|
|
1032
|
+
}
|
|
1033
|
+
utils.addMethod(
|
|
1034
|
+
chai.Assertion.prototype,
|
|
1035
|
+
"toMatchInlineSnapshot",
|
|
1036
|
+
function __VITEST_INLINE_SNAPSHOT__(properties, inlineSnapshot, message) {
|
|
988
1037
|
const expected = utils.flag(this, "object");
|
|
1038
|
+
const error = utils.flag(this, "error");
|
|
989
1039
|
const test = utils.flag(this, "vitest-test");
|
|
990
|
-
if (typeof properties === "string"
|
|
991
|
-
message =
|
|
1040
|
+
if (typeof properties === "string") {
|
|
1041
|
+
message = inlineSnapshot;
|
|
1042
|
+
inlineSnapshot = properties;
|
|
992
1043
|
properties = void 0;
|
|
993
1044
|
}
|
|
1045
|
+
if (inlineSnapshot)
|
|
1046
|
+
inlineSnapshot = stripSnapshotIndentation(inlineSnapshot);
|
|
994
1047
|
const errorMessage = utils.flag(this, "message");
|
|
995
1048
|
getSnapshotClient().assert({
|
|
996
1049
|
received: expected,
|
|
997
1050
|
test,
|
|
998
1051
|
message,
|
|
999
|
-
isInline:
|
|
1052
|
+
isInline: true,
|
|
1000
1053
|
properties,
|
|
1054
|
+
inlineSnapshot,
|
|
1055
|
+
error,
|
|
1001
1056
|
errorMessage
|
|
1002
1057
|
});
|
|
1003
|
-
});
|
|
1004
|
-
}
|
|
1005
|
-
utils.addMethod(chai.Assertion.prototype, "toMatchInlineSnapshot", function __VITEST_INLINE_SNAPSHOT__(properties, inlineSnapshot, message) {
|
|
1006
|
-
const expected = utils.flag(this, "object");
|
|
1007
|
-
const error = utils.flag(this, "error");
|
|
1008
|
-
const test = utils.flag(this, "vitest-test");
|
|
1009
|
-
if (typeof properties === "string") {
|
|
1010
|
-
message = inlineSnapshot;
|
|
1011
|
-
inlineSnapshot = properties;
|
|
1012
|
-
properties = void 0;
|
|
1013
1058
|
}
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
isInline: true,
|
|
1052
|
-
error,
|
|
1053
|
-
errorMessage
|
|
1054
|
-
});
|
|
1055
|
-
});
|
|
1059
|
+
);
|
|
1060
|
+
utils.addMethod(
|
|
1061
|
+
chai.Assertion.prototype,
|
|
1062
|
+
"toThrowErrorMatchingSnapshot",
|
|
1063
|
+
function(message) {
|
|
1064
|
+
const expected = utils.flag(this, "object");
|
|
1065
|
+
const test = utils.flag(this, "vitest-test");
|
|
1066
|
+
const promise = utils.flag(this, "promise");
|
|
1067
|
+
const errorMessage = utils.flag(this, "message");
|
|
1068
|
+
getSnapshotClient().assert({
|
|
1069
|
+
received: getErrorString(expected, promise),
|
|
1070
|
+
test,
|
|
1071
|
+
message,
|
|
1072
|
+
errorMessage
|
|
1073
|
+
});
|
|
1074
|
+
}
|
|
1075
|
+
);
|
|
1076
|
+
utils.addMethod(
|
|
1077
|
+
chai.Assertion.prototype,
|
|
1078
|
+
"toThrowErrorMatchingInlineSnapshot",
|
|
1079
|
+
function __VITEST_INLINE_SNAPSHOT__(inlineSnapshot, message) {
|
|
1080
|
+
const expected = utils.flag(this, "object");
|
|
1081
|
+
const error = utils.flag(this, "error");
|
|
1082
|
+
const test = utils.flag(this, "vitest-test");
|
|
1083
|
+
const promise = utils.flag(this, "promise");
|
|
1084
|
+
const errorMessage = utils.flag(this, "message");
|
|
1085
|
+
getSnapshotClient().assert({
|
|
1086
|
+
received: getErrorString(expected, promise),
|
|
1087
|
+
test,
|
|
1088
|
+
message,
|
|
1089
|
+
inlineSnapshot,
|
|
1090
|
+
isInline: true,
|
|
1091
|
+
error,
|
|
1092
|
+
errorMessage
|
|
1093
|
+
});
|
|
1094
|
+
}
|
|
1095
|
+
);
|
|
1056
1096
|
};
|
|
1057
1097
|
|
|
1058
1098
|
var chai$1 = /*#__PURE__*/Object.freeze({
|
|
@@ -1063,10 +1103,20 @@ var chai$1 = /*#__PURE__*/Object.freeze({
|
|
|
1063
1103
|
|
|
1064
1104
|
const GLOBAL_EXPECT = Symbol.for("expect-global");
|
|
1065
1105
|
const MATCHERS_OBJECT = Symbol.for("matchers-object");
|
|
1106
|
+
const JEST_MATCHERS_OBJECT = Symbol.for("$$jest-matchers-object");
|
|
1066
1107
|
|
|
1067
1108
|
if (!Object.prototype.hasOwnProperty.call(globalThis, MATCHERS_OBJECT)) {
|
|
1109
|
+
const globalState = /* @__PURE__ */ new WeakMap();
|
|
1110
|
+
const matchers = /* @__PURE__ */ Object.create(null);
|
|
1068
1111
|
Object.defineProperty(globalThis, MATCHERS_OBJECT, {
|
|
1069
|
-
|
|
1112
|
+
get: () => globalState
|
|
1113
|
+
});
|
|
1114
|
+
Object.defineProperty(globalThis, JEST_MATCHERS_OBJECT, {
|
|
1115
|
+
configurable: true,
|
|
1116
|
+
get: () => ({
|
|
1117
|
+
state: globalState.get(globalThis[GLOBAL_EXPECT]),
|
|
1118
|
+
matchers
|
|
1119
|
+
})
|
|
1070
1120
|
});
|
|
1071
1121
|
}
|
|
1072
1122
|
const getState = (expect) => globalThis[MATCHERS_OBJECT].get(expect);
|
|
@@ -1080,6 +1130,7 @@ const JestChaiExpect = (chai, utils) => {
|
|
|
1080
1130
|
function def(name, fn) {
|
|
1081
1131
|
const addMethod = (n) => {
|
|
1082
1132
|
utils.addMethod(chai.Assertion.prototype, n, fn);
|
|
1133
|
+
utils.addMethod(globalThis[JEST_MATCHERS_OBJECT].matchers, n, fn);
|
|
1083
1134
|
};
|
|
1084
1135
|
if (Array.isArray(name))
|
|
1085
1136
|
name.forEach((n) => addMethod(n));
|
|
@@ -1117,43 +1168,85 @@ const JestChaiExpect = (chai, utils) => {
|
|
|
1117
1168
|
});
|
|
1118
1169
|
def("toEqual", function(expected) {
|
|
1119
1170
|
const actual = utils.flag(this, "object");
|
|
1120
|
-
const equal = equals(
|
|
1121
|
-
|
|
1171
|
+
const equal = equals(
|
|
1172
|
+
actual,
|
|
1173
|
+
expected,
|
|
1174
|
+
[iterableEquality]
|
|
1175
|
+
);
|
|
1176
|
+
return this.assert(
|
|
1177
|
+
equal,
|
|
1178
|
+
"expected #{this} to deeply equal #{exp}",
|
|
1179
|
+
"expected #{this} to not deeply equal #{exp}",
|
|
1180
|
+
expected,
|
|
1181
|
+
actual
|
|
1182
|
+
);
|
|
1122
1183
|
});
|
|
1123
1184
|
def("toStrictEqual", function(expected) {
|
|
1124
1185
|
const obj = utils.flag(this, "object");
|
|
1125
|
-
const equal = equals(
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1186
|
+
const equal = equals(
|
|
1187
|
+
obj,
|
|
1188
|
+
expected,
|
|
1189
|
+
[
|
|
1190
|
+
iterableEquality,
|
|
1191
|
+
typeEquality,
|
|
1192
|
+
sparseArrayEquality,
|
|
1193
|
+
arrayBufferEquality
|
|
1194
|
+
],
|
|
1195
|
+
true
|
|
1196
|
+
);
|
|
1197
|
+
return this.assert(
|
|
1198
|
+
equal,
|
|
1199
|
+
"expected #{this} to strictly equal #{exp}",
|
|
1200
|
+
"expected #{this} to not strictly equal #{exp}",
|
|
1201
|
+
expected,
|
|
1202
|
+
obj
|
|
1203
|
+
);
|
|
1132
1204
|
});
|
|
1133
1205
|
def("toBe", function(expected) {
|
|
1134
1206
|
const actual = this._obj;
|
|
1135
1207
|
const pass = Object.is(actual, expected);
|
|
1136
1208
|
let deepEqualityName = "";
|
|
1137
1209
|
if (!pass) {
|
|
1138
|
-
const toStrictEqualPass = equals(
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1210
|
+
const toStrictEqualPass = equals(
|
|
1211
|
+
actual,
|
|
1212
|
+
expected,
|
|
1213
|
+
[
|
|
1214
|
+
iterableEquality,
|
|
1215
|
+
typeEquality,
|
|
1216
|
+
sparseArrayEquality,
|
|
1217
|
+
arrayBufferEquality
|
|
1218
|
+
],
|
|
1219
|
+
true
|
|
1220
|
+
);
|
|
1144
1221
|
if (toStrictEqualPass) {
|
|
1145
1222
|
deepEqualityName = "toStrictEqual";
|
|
1146
1223
|
} else {
|
|
1147
|
-
const toEqualPass = equals(
|
|
1224
|
+
const toEqualPass = equals(
|
|
1225
|
+
actual,
|
|
1226
|
+
expected,
|
|
1227
|
+
[iterableEquality]
|
|
1228
|
+
);
|
|
1148
1229
|
if (toEqualPass)
|
|
1149
1230
|
deepEqualityName = "toEqual";
|
|
1150
1231
|
}
|
|
1151
1232
|
}
|
|
1152
|
-
return this.assert(
|
|
1233
|
+
return this.assert(
|
|
1234
|
+
pass,
|
|
1235
|
+
generateToBeMessage(deepEqualityName),
|
|
1236
|
+
"expected #{this} not to be #{exp} // Object.is equality",
|
|
1237
|
+
expected,
|
|
1238
|
+
actual
|
|
1239
|
+
);
|
|
1153
1240
|
});
|
|
1154
1241
|
def("toMatchObject", function(expected) {
|
|
1155
1242
|
const actual = this._obj;
|
|
1156
|
-
return this.assert(
|
|
1243
|
+
return this.assert(
|
|
1244
|
+
equals(actual, expected, [iterableEquality, subsetEquality]),
|
|
1245
|
+
"expected #{this} to match object #{exp}",
|
|
1246
|
+
"expected #{this} to not match object #{exp}",
|
|
1247
|
+
expected,
|
|
1248
|
+
actual
|
|
1249
|
+
);
|
|
1157
1250
|
});
|
|
1158
1251
|
def("toMatch", function(expected) {
|
|
1159
1252
|
if (typeof expected === "string")
|
|
@@ -1169,39 +1262,78 @@ const JestChaiExpect = (chai, utils) => {
|
|
|
1169
1262
|
const index = Array.from(obj).findIndex((item) => {
|
|
1170
1263
|
return equals(item, expected);
|
|
1171
1264
|
});
|
|
1172
|
-
this.assert(
|
|
1265
|
+
this.assert(
|
|
1266
|
+
index !== -1,
|
|
1267
|
+
"expected #{this} to deep equally contain #{exp}",
|
|
1268
|
+
"expected #{this} to not deep equally contain #{exp}",
|
|
1269
|
+
expected
|
|
1270
|
+
);
|
|
1173
1271
|
});
|
|
1174
1272
|
def("toBeTruthy", function() {
|
|
1175
1273
|
const obj = utils.flag(this, "object");
|
|
1176
|
-
this.assert(
|
|
1274
|
+
this.assert(
|
|
1275
|
+
Boolean(obj),
|
|
1276
|
+
"expected #{this} to be truthy",
|
|
1277
|
+
"expected #{this} to not be truthy",
|
|
1278
|
+
obj
|
|
1279
|
+
);
|
|
1177
1280
|
});
|
|
1178
1281
|
def("toBeFalsy", function() {
|
|
1179
1282
|
const obj = utils.flag(this, "object");
|
|
1180
|
-
this.assert(
|
|
1283
|
+
this.assert(
|
|
1284
|
+
!obj,
|
|
1285
|
+
"expected #{this} to be falsy",
|
|
1286
|
+
"expected #{this} to not be falsy",
|
|
1287
|
+
obj
|
|
1288
|
+
);
|
|
1181
1289
|
});
|
|
1182
1290
|
def("toBeGreaterThan", function(expected) {
|
|
1183
1291
|
const actual = this._obj;
|
|
1184
1292
|
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
1185
1293
|
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
1186
|
-
return this.assert(
|
|
1294
|
+
return this.assert(
|
|
1295
|
+
actual > expected,
|
|
1296
|
+
`expected ${actual} to be greater than ${expected}`,
|
|
1297
|
+
`expected ${actual} to be not greater than ${expected}`,
|
|
1298
|
+
actual,
|
|
1299
|
+
expected
|
|
1300
|
+
);
|
|
1187
1301
|
});
|
|
1188
1302
|
def("toBeGreaterThanOrEqual", function(expected) {
|
|
1189
1303
|
const actual = this._obj;
|
|
1190
1304
|
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
1191
1305
|
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
1192
|
-
return this.assert(
|
|
1306
|
+
return this.assert(
|
|
1307
|
+
actual >= expected,
|
|
1308
|
+
`expected ${actual} to be greater than or equal to ${expected}`,
|
|
1309
|
+
`expected ${actual} to be not greater than or equal to ${expected}`,
|
|
1310
|
+
actual,
|
|
1311
|
+
expected
|
|
1312
|
+
);
|
|
1193
1313
|
});
|
|
1194
1314
|
def("toBeLessThan", function(expected) {
|
|
1195
1315
|
const actual = this._obj;
|
|
1196
1316
|
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
1197
1317
|
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
1198
|
-
return this.assert(
|
|
1318
|
+
return this.assert(
|
|
1319
|
+
actual < expected,
|
|
1320
|
+
`expected ${actual} to be less than ${expected}`,
|
|
1321
|
+
`expected ${actual} to be not less than ${expected}`,
|
|
1322
|
+
actual,
|
|
1323
|
+
expected
|
|
1324
|
+
);
|
|
1199
1325
|
});
|
|
1200
1326
|
def("toBeLessThanOrEqual", function(expected) {
|
|
1201
1327
|
const actual = this._obj;
|
|
1202
1328
|
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
1203
1329
|
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
1204
|
-
return this.assert(
|
|
1330
|
+
return this.assert(
|
|
1331
|
+
actual <= expected,
|
|
1332
|
+
`expected ${actual} to be less than or equal to ${expected}`,
|
|
1333
|
+
`expected ${actual} to be not less than or equal to ${expected}`,
|
|
1334
|
+
actual,
|
|
1335
|
+
expected
|
|
1336
|
+
);
|
|
1205
1337
|
});
|
|
1206
1338
|
def("toBeNaN", function() {
|
|
1207
1339
|
return this.be.NaN;
|
|
@@ -1222,7 +1354,13 @@ const JestChaiExpect = (chai, utils) => {
|
|
|
1222
1354
|
def("toBeTypeOf", function(expected) {
|
|
1223
1355
|
const actual = typeof this._obj;
|
|
1224
1356
|
const equal = expected === actual;
|
|
1225
|
-
return this.assert(
|
|
1357
|
+
return this.assert(
|
|
1358
|
+
equal,
|
|
1359
|
+
"expected #{this} to be type of #{exp}",
|
|
1360
|
+
"expected #{this} not to be type of #{exp}",
|
|
1361
|
+
expected,
|
|
1362
|
+
actual
|
|
1363
|
+
);
|
|
1226
1364
|
});
|
|
1227
1365
|
def("toBeInstanceOf", function(obj) {
|
|
1228
1366
|
return this.instanceOf(obj);
|
|
@@ -1235,14 +1373,21 @@ const JestChaiExpect = (chai, utils) => {
|
|
|
1235
1373
|
args[0] = args[0].map((key) => key.replace(/([.[\]])/g, "\\$1")).join(".");
|
|
1236
1374
|
const actual = this._obj;
|
|
1237
1375
|
const [propertyName, expected] = args;
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1376
|
+
const getValue = () => {
|
|
1377
|
+
const hasOwn = Object.prototype.hasOwnProperty.call(actual, propertyName);
|
|
1378
|
+
if (hasOwn)
|
|
1379
|
+
return { value: actual[propertyName], exists: true };
|
|
1380
|
+
return utils.getPathInfo(actual, propertyName);
|
|
1381
|
+
};
|
|
1382
|
+
const { value, exists } = getValue();
|
|
1383
|
+
const pass = exists && (args.length === 1 || equals(expected, value));
|
|
1384
|
+
const valueString = args.length === 1 ? "" : ` with value ${utils.objDisplay(expected)}`;
|
|
1385
|
+
return this.assert(
|
|
1386
|
+
pass,
|
|
1387
|
+
`expected #{this} to have property "${propertyName}"${valueString}`,
|
|
1388
|
+
`expected #{this} to not have property "${propertyName}"${valueString}`,
|
|
1389
|
+
actual
|
|
1390
|
+
);
|
|
1246
1391
|
});
|
|
1247
1392
|
def("toBeCloseTo", function(received, precision = 2) {
|
|
1248
1393
|
const expected = this._obj;
|
|
@@ -1258,7 +1403,13 @@ const JestChaiExpect = (chai, utils) => {
|
|
|
1258
1403
|
receivedDiff = Math.abs(expected - received);
|
|
1259
1404
|
pass = receivedDiff < expectedDiff;
|
|
1260
1405
|
}
|
|
1261
|
-
return this.assert(
|
|
1406
|
+
return this.assert(
|
|
1407
|
+
pass,
|
|
1408
|
+
`expected #{this} to be close to #{exp}, received difference is ${receivedDiff}, but expected ${expectedDiff}`,
|
|
1409
|
+
`expected #{this} to not be close to #{exp}, received difference is ${receivedDiff}, but expected ${expectedDiff}`,
|
|
1410
|
+
received,
|
|
1411
|
+
expected
|
|
1412
|
+
);
|
|
1262
1413
|
});
|
|
1263
1414
|
const assertIsMock = (assertion) => {
|
|
1264
1415
|
if (!isMockFunction(assertion._obj))
|
|
@@ -1304,26 +1455,41 @@ Number of calls: ${picocolors.exports.bold(spy.mock.calls.length)}
|
|
|
1304
1455
|
const spy = getSpy(this);
|
|
1305
1456
|
const spyName = spy.getMockName();
|
|
1306
1457
|
const callCount = spy.mock.calls.length;
|
|
1307
|
-
return this.assert(
|
|
1458
|
+
return this.assert(
|
|
1459
|
+
callCount === number,
|
|
1460
|
+
`expected "${spyName}" to be called #{exp} times`,
|
|
1461
|
+
`expected "${spyName}" to not be called #{exp} times`,
|
|
1462
|
+
number,
|
|
1463
|
+
callCount
|
|
1464
|
+
);
|
|
1308
1465
|
});
|
|
1309
1466
|
def("toHaveBeenCalledOnce", function() {
|
|
1310
1467
|
const spy = getSpy(this);
|
|
1311
1468
|
const spyName = spy.getMockName();
|
|
1312
1469
|
const callCount = spy.mock.calls.length;
|
|
1313
|
-
return this.assert(
|
|
1470
|
+
return this.assert(
|
|
1471
|
+
callCount === 1,
|
|
1472
|
+
`expected "${spyName}" to be called once`,
|
|
1473
|
+
`expected "${spyName}" to not be called once`,
|
|
1474
|
+
1,
|
|
1475
|
+
callCount
|
|
1476
|
+
);
|
|
1314
1477
|
});
|
|
1315
1478
|
def(["toHaveBeenCalled", "toBeCalled"], function() {
|
|
1316
1479
|
const spy = getSpy(this);
|
|
1317
1480
|
const spyName = spy.getMockName();
|
|
1318
1481
|
const called = spy.mock.calls.length > 0;
|
|
1319
1482
|
const isNot = utils.flag(this, "negate");
|
|
1320
|
-
let msg = utils.getMessage(
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1483
|
+
let msg = utils.getMessage(
|
|
1484
|
+
this,
|
|
1485
|
+
[
|
|
1486
|
+
called,
|
|
1487
|
+
`expected "${spyName}" to be called at least once`,
|
|
1488
|
+
`expected "${spyName}" to not be called at all`,
|
|
1489
|
+
true,
|
|
1490
|
+
called
|
|
1491
|
+
]
|
|
1492
|
+
);
|
|
1327
1493
|
if (called && isNot)
|
|
1328
1494
|
msg += formatCalls(spy, msg);
|
|
1329
1495
|
if (called && isNot || !called && !isNot) {
|
|
@@ -1337,12 +1503,15 @@ Number of calls: ${picocolors.exports.bold(spy.mock.calls.length)}
|
|
|
1337
1503
|
const spyName = spy.getMockName();
|
|
1338
1504
|
const pass = spy.mock.calls.some((callArg) => equals(callArg, args, [iterableEquality]));
|
|
1339
1505
|
const isNot = utils.flag(this, "negate");
|
|
1340
|
-
let msg = utils.getMessage(
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1506
|
+
let msg = utils.getMessage(
|
|
1507
|
+
this,
|
|
1508
|
+
[
|
|
1509
|
+
pass,
|
|
1510
|
+
`expected "${spyName}" to be called with arguments: #{exp}`,
|
|
1511
|
+
`expected "${spyName}" to not be called with arguments: #{exp}`,
|
|
1512
|
+
args
|
|
1513
|
+
]
|
|
1514
|
+
);
|
|
1346
1515
|
if (pass && isNot || !pass && !isNot) {
|
|
1347
1516
|
msg += formatCalls(spy, msg, args);
|
|
1348
1517
|
const err = new Error(msg);
|
|
@@ -1354,13 +1523,25 @@ Number of calls: ${picocolors.exports.bold(spy.mock.calls.length)}
|
|
|
1354
1523
|
const spy = getSpy(this);
|
|
1355
1524
|
const spyName = spy.getMockName();
|
|
1356
1525
|
const nthCall = spy.mock.calls[times - 1];
|
|
1357
|
-
this.assert(
|
|
1526
|
+
this.assert(
|
|
1527
|
+
equals(nthCall, args, [iterableEquality]),
|
|
1528
|
+
`expected ${ordinalOf(times)} "${spyName}" call to have been called with #{exp}`,
|
|
1529
|
+
`expected ${ordinalOf(times)} "${spyName}" call to not have been called with #{exp}`,
|
|
1530
|
+
args,
|
|
1531
|
+
nthCall
|
|
1532
|
+
);
|
|
1358
1533
|
});
|
|
1359
1534
|
def(["toHaveBeenLastCalledWith", "lastCalledWith"], function(...args) {
|
|
1360
1535
|
const spy = getSpy(this);
|
|
1361
1536
|
const spyName = spy.getMockName();
|
|
1362
1537
|
const lastCall = spy.mock.calls[spy.calls.length - 1];
|
|
1363
|
-
this.assert(
|
|
1538
|
+
this.assert(
|
|
1539
|
+
equals(lastCall, args, [iterableEquality]),
|
|
1540
|
+
`expected last "${spyName}" call to have been called with #{exp}`,
|
|
1541
|
+
`expected last "${spyName}" call to not have been called with #{exp}`,
|
|
1542
|
+
args,
|
|
1543
|
+
lastCall
|
|
1544
|
+
);
|
|
1364
1545
|
});
|
|
1365
1546
|
def(["toThrow", "toThrowError"], function(expected) {
|
|
1366
1547
|
if (typeof expected === "string" || typeof expected === "undefined" || expected instanceof RegExp)
|
|
@@ -1390,14 +1571,32 @@ Number of calls: ${picocolors.exports.bold(spy.mock.calls.length)}
|
|
|
1390
1571
|
}
|
|
1391
1572
|
if (typeof expected === "function") {
|
|
1392
1573
|
const name = expected.name || expected.prototype.constructor.name;
|
|
1393
|
-
return this.assert(
|
|
1574
|
+
return this.assert(
|
|
1575
|
+
thrown && thrown instanceof expected,
|
|
1576
|
+
`expected error to be instance of ${name}`,
|
|
1577
|
+
`expected error not to be instance of ${name}`,
|
|
1578
|
+
expected,
|
|
1579
|
+
thrown
|
|
1580
|
+
);
|
|
1394
1581
|
}
|
|
1395
1582
|
if (expected instanceof Error) {
|
|
1396
|
-
return this.assert(
|
|
1583
|
+
return this.assert(
|
|
1584
|
+
thrown && expected.message === thrown.message,
|
|
1585
|
+
`expected error to have message: ${expected.message}`,
|
|
1586
|
+
`expected error not to have message: ${expected.message}`,
|
|
1587
|
+
expected.message,
|
|
1588
|
+
thrown && thrown.message
|
|
1589
|
+
);
|
|
1397
1590
|
}
|
|
1398
1591
|
if (typeof expected.asymmetricMatch === "function") {
|
|
1399
1592
|
const matcher = expected;
|
|
1400
|
-
return this.assert(
|
|
1593
|
+
return this.assert(
|
|
1594
|
+
thrown && matcher.asymmetricMatch(thrown),
|
|
1595
|
+
"expected error to match asymmetric matcher",
|
|
1596
|
+
"expected error not to match asymmetric matcher",
|
|
1597
|
+
matcher.toString(),
|
|
1598
|
+
thrown
|
|
1599
|
+
);
|
|
1401
1600
|
}
|
|
1402
1601
|
throw new Error(`"toThrow" expects string, RegExp, function, Error instance or asymmetric matcher, got "${typeof expected}"`);
|
|
1403
1602
|
});
|
|
@@ -1405,26 +1604,49 @@ Number of calls: ${picocolors.exports.bold(spy.mock.calls.length)}
|
|
|
1405
1604
|
const spy = getSpy(this);
|
|
1406
1605
|
const spyName = spy.getMockName();
|
|
1407
1606
|
const calledAndNotThrew = spy.mock.calls.length > 0 && !spy.mock.results.some(({ type }) => type === "throw");
|
|
1408
|
-
this.assert(
|
|
1607
|
+
this.assert(
|
|
1608
|
+
calledAndNotThrew,
|
|
1609
|
+
`expected "${spyName}" to be successfully called at least once`,
|
|
1610
|
+
`expected "${spyName}" to not be successfully called`,
|
|
1611
|
+
calledAndNotThrew,
|
|
1612
|
+
!calledAndNotThrew
|
|
1613
|
+
);
|
|
1409
1614
|
});
|
|
1410
1615
|
def(["toHaveReturnedTimes", "toReturnTimes"], function(times) {
|
|
1411
1616
|
const spy = getSpy(this);
|
|
1412
1617
|
const spyName = spy.getMockName();
|
|
1413
1618
|
const successfulReturns = spy.mock.results.reduce((success, { type }) => type === "throw" ? success : ++success, 0);
|
|
1414
|
-
this.assert(
|
|
1619
|
+
this.assert(
|
|
1620
|
+
successfulReturns === times,
|
|
1621
|
+
`expected "${spyName}" to be successfully called ${times} times`,
|
|
1622
|
+
`expected "${spyName}" to not be successfully called ${times} times`,
|
|
1623
|
+
`expected number of returns: ${times}`,
|
|
1624
|
+
`received number of returns: ${successfulReturns}`
|
|
1625
|
+
);
|
|
1415
1626
|
});
|
|
1416
1627
|
def(["toHaveReturnedWith", "toReturnWith"], function(value) {
|
|
1417
1628
|
const spy = getSpy(this);
|
|
1418
1629
|
const spyName = spy.getMockName();
|
|
1419
1630
|
const pass = spy.mock.results.some(({ type, value: result }) => type === "return" && equals(value, result));
|
|
1420
|
-
this.assert(
|
|
1631
|
+
this.assert(
|
|
1632
|
+
pass,
|
|
1633
|
+
`expected "${spyName}" to be successfully called with #{exp}`,
|
|
1634
|
+
`expected "${spyName}" to not be successfully called with #{exp}`,
|
|
1635
|
+
value
|
|
1636
|
+
);
|
|
1421
1637
|
});
|
|
1422
1638
|
def(["toHaveLastReturnedWith", "lastReturnedWith"], function(value) {
|
|
1423
1639
|
const spy = getSpy(this);
|
|
1424
1640
|
const spyName = spy.getMockName();
|
|
1425
1641
|
const { value: lastResult } = spy.mock.results[spy.returns.length - 1];
|
|
1426
1642
|
const pass = equals(lastResult, value);
|
|
1427
|
-
this.assert(
|
|
1643
|
+
this.assert(
|
|
1644
|
+
pass,
|
|
1645
|
+
`expected last "${spyName}" call to return #{exp}`,
|
|
1646
|
+
`expected last "${spyName}" call to not return #{exp}`,
|
|
1647
|
+
value,
|
|
1648
|
+
lastResult
|
|
1649
|
+
);
|
|
1428
1650
|
});
|
|
1429
1651
|
def(["toHaveNthReturnedWith", "nthReturnedWith"], function(nthCall, value) {
|
|
1430
1652
|
const spy = getSpy(this);
|
|
@@ -1435,7 +1657,13 @@ Number of calls: ${picocolors.exports.bold(spy.mock.calls.length)}
|
|
|
1435
1657
|
if (!isNot && callType === "throw")
|
|
1436
1658
|
chai.assert.fail(`expected ${ordinalCall} to return #{exp}, but instead it threw an error`);
|
|
1437
1659
|
const nthCallReturn = equals(callResult, value);
|
|
1438
|
-
this.assert(
|
|
1660
|
+
this.assert(
|
|
1661
|
+
nthCallReturn,
|
|
1662
|
+
`expected ${ordinalCall} "${spyName}" call to return #{exp}`,
|
|
1663
|
+
`expected ${ordinalCall} "${spyName}" call to not return #{exp}`,
|
|
1664
|
+
value,
|
|
1665
|
+
callResult
|
|
1666
|
+
);
|
|
1439
1667
|
});
|
|
1440
1668
|
def("toSatisfy", function(matcher, message) {
|
|
1441
1669
|
return this.be.satisfy(matcher, message);
|
|
@@ -1452,12 +1680,15 @@ Number of calls: ${picocolors.exports.bold(spy.mock.calls.length)}
|
|
|
1452
1680
|
if (typeof result !== "function")
|
|
1453
1681
|
return result instanceof chai.Assertion ? proxy : result;
|
|
1454
1682
|
return async (...args) => {
|
|
1455
|
-
return obj.then(
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1683
|
+
return obj.then(
|
|
1684
|
+
(value) => {
|
|
1685
|
+
utils.flag(this, "object", value);
|
|
1686
|
+
return result.call(this, ...args);
|
|
1687
|
+
},
|
|
1688
|
+
(err) => {
|
|
1689
|
+
throw new Error(`promise rejected "${toString(err)}" instead of resolving`);
|
|
1690
|
+
}
|
|
1691
|
+
);
|
|
1461
1692
|
};
|
|
1462
1693
|
}
|
|
1463
1694
|
});
|
|
@@ -1476,18 +1707,25 @@ Number of calls: ${picocolors.exports.bold(spy.mock.calls.length)}
|
|
|
1476
1707
|
if (typeof result !== "function")
|
|
1477
1708
|
return result instanceof chai.Assertion ? proxy : result;
|
|
1478
1709
|
return async (...args) => {
|
|
1479
|
-
return wrapper.then(
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1710
|
+
return wrapper.then(
|
|
1711
|
+
(value) => {
|
|
1712
|
+
throw new Error(`promise resolved "${toString(value)}" instead of rejecting`);
|
|
1713
|
+
},
|
|
1714
|
+
(err) => {
|
|
1715
|
+
utils.flag(this, "object", err);
|
|
1716
|
+
return result.call(this, ...args);
|
|
1717
|
+
}
|
|
1718
|
+
);
|
|
1485
1719
|
};
|
|
1486
1720
|
}
|
|
1487
1721
|
});
|
|
1488
1722
|
return proxy;
|
|
1489
1723
|
});
|
|
1490
|
-
utils.addMethod(
|
|
1724
|
+
utils.addMethod(
|
|
1725
|
+
chai.expect,
|
|
1726
|
+
"addSnapshotSerializer",
|
|
1727
|
+
addSerializer
|
|
1728
|
+
);
|
|
1491
1729
|
};
|
|
1492
1730
|
function toString(value) {
|
|
1493
1731
|
try {
|
|
@@ -1560,7 +1798,9 @@ class ObjectContaining extends AsymmetricMatcher {
|
|
|
1560
1798
|
}
|
|
1561
1799
|
asymmetricMatch(other) {
|
|
1562
1800
|
if (typeof this.sample !== "object") {
|
|
1563
|
-
throw new TypeError(
|
|
1801
|
+
throw new TypeError(
|
|
1802
|
+
`You must provide an object to ${this.toString()}, not '${typeof this.sample}'.`
|
|
1803
|
+
);
|
|
1564
1804
|
}
|
|
1565
1805
|
let result = true;
|
|
1566
1806
|
for (const property in this.sample) {
|
|
@@ -1584,9 +1824,13 @@ class ArrayContaining extends AsymmetricMatcher {
|
|
|
1584
1824
|
}
|
|
1585
1825
|
asymmetricMatch(other) {
|
|
1586
1826
|
if (!Array.isArray(this.sample)) {
|
|
1587
|
-
throw new TypeError(
|
|
1827
|
+
throw new TypeError(
|
|
1828
|
+
`You must provide an array to ${this.toString()}, not '${typeof this.sample}'.`
|
|
1829
|
+
);
|
|
1588
1830
|
}
|
|
1589
|
-
const result = this.sample.length === 0 || Array.isArray(other) && this.sample.every(
|
|
1831
|
+
const result = this.sample.length === 0 || Array.isArray(other) && this.sample.every(
|
|
1832
|
+
(item) => other.some((another) => equals(item, another))
|
|
1833
|
+
);
|
|
1590
1834
|
return this.inverse ? !result : result;
|
|
1591
1835
|
}
|
|
1592
1836
|
toString() {
|
|
@@ -1599,7 +1843,9 @@ class ArrayContaining extends AsymmetricMatcher {
|
|
|
1599
1843
|
class Any extends AsymmetricMatcher {
|
|
1600
1844
|
constructor(sample) {
|
|
1601
1845
|
if (typeof sample === "undefined") {
|
|
1602
|
-
throw new TypeError(
|
|
1846
|
+
throw new TypeError(
|
|
1847
|
+
"any() expects to be passed a constructor function. Please pass one or use anything() to match any object."
|
|
1848
|
+
);
|
|
1603
1849
|
}
|
|
1604
1850
|
super(sample);
|
|
1605
1851
|
}
|
|
@@ -1665,12 +1911,36 @@ class StringMatching extends AsymmetricMatcher {
|
|
|
1665
1911
|
}
|
|
1666
1912
|
}
|
|
1667
1913
|
const JestAsymmetricMatchers = (chai, utils) => {
|
|
1668
|
-
utils.addMethod(
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
utils.addMethod(
|
|
1914
|
+
utils.addMethod(
|
|
1915
|
+
chai.expect,
|
|
1916
|
+
"anything",
|
|
1917
|
+
() => new Anything()
|
|
1918
|
+
);
|
|
1919
|
+
utils.addMethod(
|
|
1920
|
+
chai.expect,
|
|
1921
|
+
"any",
|
|
1922
|
+
(expected) => new Any(expected)
|
|
1923
|
+
);
|
|
1924
|
+
utils.addMethod(
|
|
1925
|
+
chai.expect,
|
|
1926
|
+
"stringContaining",
|
|
1927
|
+
(expected) => new StringContaining(expected)
|
|
1928
|
+
);
|
|
1929
|
+
utils.addMethod(
|
|
1930
|
+
chai.expect,
|
|
1931
|
+
"objectContaining",
|
|
1932
|
+
(expected) => new ObjectContaining(expected)
|
|
1933
|
+
);
|
|
1934
|
+
utils.addMethod(
|
|
1935
|
+
chai.expect,
|
|
1936
|
+
"arrayContaining",
|
|
1937
|
+
(expected) => new ArrayContaining(expected)
|
|
1938
|
+
);
|
|
1939
|
+
utils.addMethod(
|
|
1940
|
+
chai.expect,
|
|
1941
|
+
"stringMatching",
|
|
1942
|
+
(expected) => new StringMatching(expected)
|
|
1943
|
+
);
|
|
1674
1944
|
chai.expect.not = {
|
|
1675
1945
|
stringContaining: (expected) => new StringContaining(expected, true),
|
|
1676
1946
|
objectContaining: (expected) => new ObjectContaining(expected, true),
|
|
@@ -1727,13 +1997,18 @@ function JestExtendPlugin(expect, matchers) {
|
|
|
1727
1997
|
throw new JestExtendError(message(), actual, expected);
|
|
1728
1998
|
}
|
|
1729
1999
|
const expectAssertionWrapper = isAsyncFunction(expectAssertion) ? expectAsyncWrapper : expectSyncWrapper;
|
|
2000
|
+
utils.addMethod(globalThis[JEST_MATCHERS_OBJECT].matchers, expectAssertionName, expectAssertionWrapper);
|
|
1730
2001
|
utils.addMethod(c.Assertion.prototype, expectAssertionName, expectAssertionWrapper);
|
|
1731
2002
|
class CustomMatcher extends AsymmetricMatcher {
|
|
1732
2003
|
constructor(inverse = false, ...sample) {
|
|
1733
2004
|
super(sample, inverse);
|
|
1734
2005
|
}
|
|
1735
2006
|
asymmetricMatch(other) {
|
|
1736
|
-
const { pass } = expectAssertion.call(
|
|
2007
|
+
const { pass } = expectAssertion.call(
|
|
2008
|
+
this.getMatcherContext(expect),
|
|
2009
|
+
other,
|
|
2010
|
+
...this.sample
|
|
2011
|
+
);
|
|
1737
2012
|
return this.inverse ? !pass : pass;
|
|
1738
2013
|
}
|
|
1739
2014
|
toString() {
|
|
@@ -1773,6 +2048,7 @@ chai$2.use(Subset);
|
|
|
1773
2048
|
chai$2.use(SnapshotPlugin);
|
|
1774
2049
|
chai$2.use(JestAsymmetricMatchers);
|
|
1775
2050
|
|
|
2051
|
+
const workerState$1 = getWorkerState();
|
|
1776
2052
|
function createExpect(test) {
|
|
1777
2053
|
var _a;
|
|
1778
2054
|
const expect = (value, message) => {
|
|
@@ -1793,6 +2069,7 @@ function createExpect(test) {
|
|
|
1793
2069
|
isExpectingAssertionsError: null,
|
|
1794
2070
|
expectedAssertionsNumber: null,
|
|
1795
2071
|
expectedAssertionsNumberErrorGen: null,
|
|
2072
|
+
environment: workerState$1.config.environment,
|
|
1796
2073
|
testPath: (_a = test == null ? void 0 : test.suite.file) == null ? void 0 : _a.filepath,
|
|
1797
2074
|
currentTestName: test ? getFullName(test) : void 0
|
|
1798
2075
|
}, expect);
|
|
@@ -1887,6 +2164,7 @@ If this is a long-running test, pass a timeout value as the last argument or con
|
|
|
1887
2164
|
|
|
1888
2165
|
const fnMap = /* @__PURE__ */ new WeakMap();
|
|
1889
2166
|
const hooksMap = /* @__PURE__ */ new WeakMap();
|
|
2167
|
+
const benchmarkMap = /* @__PURE__ */ new WeakMap();
|
|
1890
2168
|
function setFn(key, fn) {
|
|
1891
2169
|
fnMap.set(key, fn);
|
|
1892
2170
|
}
|
|
@@ -1899,11 +2177,29 @@ function setHooks(key, hooks) {
|
|
|
1899
2177
|
function getHooks(key) {
|
|
1900
2178
|
return hooksMap.get(key);
|
|
1901
2179
|
}
|
|
2180
|
+
async function getBenchmarkFactory(key) {
|
|
2181
|
+
let benchmark = benchmarkMap.get(key);
|
|
2182
|
+
if (!benchmark) {
|
|
2183
|
+
if (!globalThis.EventTarget)
|
|
2184
|
+
await import('./vendor-index.0557b03a.mjs').then(function (n) { return n.i; });
|
|
2185
|
+
const Bench = (await import('tinybench')).default;
|
|
2186
|
+
benchmark = new Bench({});
|
|
2187
|
+
benchmarkMap.set(key, benchmark);
|
|
2188
|
+
}
|
|
2189
|
+
return benchmark;
|
|
2190
|
+
}
|
|
1902
2191
|
|
|
1903
2192
|
const suite = createSuite();
|
|
1904
|
-
const test = createTest(
|
|
1905
|
-
|
|
1906
|
-
|
|
2193
|
+
const test = createTest(
|
|
2194
|
+
function(name, fn, options) {
|
|
2195
|
+
getCurrentSuite().test.fn.call(this, name, fn, options);
|
|
2196
|
+
}
|
|
2197
|
+
);
|
|
2198
|
+
const bench = createBenchmark(
|
|
2199
|
+
function(name, fn, options) {
|
|
2200
|
+
getCurrentSuite().benchmark.fn.call(this, name, fn, options);
|
|
2201
|
+
}
|
|
2202
|
+
);
|
|
1907
2203
|
function formatTitle(template, items, idx) {
|
|
1908
2204
|
if (template.includes("%#")) {
|
|
1909
2205
|
template = template.replace(/%%/g, "__vitest_escaped_%__").replace(/%#/g, `${idx}`).replace(/__vitest_escaped_%__/g, "%%");
|
|
@@ -1943,15 +2239,20 @@ function createSuiteCollector(name, factory = () => {
|
|
|
1943
2239
|
const factoryQueue = [];
|
|
1944
2240
|
let suite2;
|
|
1945
2241
|
initSuite();
|
|
1946
|
-
const test2 = createTest(function(name2, fn = noop,
|
|
2242
|
+
const test2 = createTest(function(name2, fn = noop, options) {
|
|
2243
|
+
if (!isRunningInTest())
|
|
2244
|
+
throw new Error("`test()` and `it()` is only available in test mode.");
|
|
1947
2245
|
const mode2 = this.only ? "only" : this.skip ? "skip" : this.todo ? "todo" : "run";
|
|
2246
|
+
if (typeof options === "number")
|
|
2247
|
+
options = { timeout: options };
|
|
1948
2248
|
const test3 = {
|
|
1949
2249
|
id: "",
|
|
1950
2250
|
type: "test",
|
|
1951
2251
|
name: name2,
|
|
1952
2252
|
mode: mode2,
|
|
1953
2253
|
suite: void 0,
|
|
1954
|
-
fails: this.fails
|
|
2254
|
+
fails: this.fails,
|
|
2255
|
+
retry: options == null ? void 0 : options.retry
|
|
1955
2256
|
};
|
|
1956
2257
|
if (this.concurrent || concurrent)
|
|
1957
2258
|
test3.concurrent = true;
|
|
@@ -1962,15 +2263,34 @@ function createSuiteCollector(name, factory = () => {
|
|
|
1962
2263
|
value: context,
|
|
1963
2264
|
enumerable: false
|
|
1964
2265
|
});
|
|
1965
|
-
setFn(test3, withTimeout(
|
|
2266
|
+
setFn(test3, withTimeout(
|
|
2267
|
+
() => fn(context),
|
|
2268
|
+
options == null ? void 0 : options.timeout
|
|
2269
|
+
));
|
|
1966
2270
|
tasks.push(test3);
|
|
1967
2271
|
});
|
|
2272
|
+
const benchmark = createBenchmark(function(name2, fn = noop, options) {
|
|
2273
|
+
const mode2 = this.skip ? "skip" : "run";
|
|
2274
|
+
if (!isRunningInBenchmark())
|
|
2275
|
+
throw new Error("`bench()` is only available in benchmark mode. Run with `vitest bench` instead.");
|
|
2276
|
+
const benchmark2 = {
|
|
2277
|
+
type: "benchmark",
|
|
2278
|
+
id: "",
|
|
2279
|
+
name: name2,
|
|
2280
|
+
mode: mode2,
|
|
2281
|
+
options,
|
|
2282
|
+
suite: void 0
|
|
2283
|
+
};
|
|
2284
|
+
setFn(benchmark2, fn);
|
|
2285
|
+
tasks.push(benchmark2);
|
|
2286
|
+
});
|
|
1968
2287
|
const collector = {
|
|
1969
2288
|
type: "collector",
|
|
1970
2289
|
name,
|
|
1971
2290
|
mode,
|
|
1972
2291
|
test: test2,
|
|
1973
2292
|
tasks,
|
|
2293
|
+
benchmark,
|
|
1974
2294
|
collect,
|
|
1975
2295
|
clear,
|
|
1976
2296
|
on: addHook
|
|
@@ -2029,22 +2349,37 @@ function createSuite() {
|
|
|
2029
2349
|
};
|
|
2030
2350
|
suiteFn.skipIf = (condition) => condition ? suite.skip : suite;
|
|
2031
2351
|
suiteFn.runIf = (condition) => condition ? suite : suite.skip;
|
|
2032
|
-
return createChainable(
|
|
2352
|
+
return createChainable(
|
|
2353
|
+
["concurrent", "shuffle", "skip", "only", "todo"],
|
|
2354
|
+
suiteFn
|
|
2355
|
+
);
|
|
2033
2356
|
}
|
|
2034
2357
|
function createTest(fn) {
|
|
2035
2358
|
const testFn = fn;
|
|
2036
2359
|
testFn.each = function(cases) {
|
|
2037
2360
|
const test2 = this.withContext();
|
|
2038
|
-
return (name, fn2,
|
|
2361
|
+
return (name, fn2, options) => {
|
|
2039
2362
|
cases.forEach((i, idx) => {
|
|
2040
2363
|
const items = Array.isArray(i) ? i : [i];
|
|
2041
|
-
test2(formatTitle(name, items, idx), () => fn2(...items),
|
|
2364
|
+
test2(formatTitle(name, items, idx), () => fn2(...items), options);
|
|
2042
2365
|
});
|
|
2043
2366
|
};
|
|
2044
2367
|
};
|
|
2045
2368
|
testFn.skipIf = (condition) => condition ? test.skip : test;
|
|
2046
2369
|
testFn.runIf = (condition) => condition ? test : test.skip;
|
|
2047
|
-
return createChainable(
|
|
2370
|
+
return createChainable(
|
|
2371
|
+
["concurrent", "skip", "only", "todo", "fails"],
|
|
2372
|
+
testFn
|
|
2373
|
+
);
|
|
2374
|
+
}
|
|
2375
|
+
function createBenchmark(fn) {
|
|
2376
|
+
const benchmark = createChainable(
|
|
2377
|
+
["skip"],
|
|
2378
|
+
fn
|
|
2379
|
+
);
|
|
2380
|
+
benchmark.skipIf = (condition) => condition ? benchmark.skip : benchmark;
|
|
2381
|
+
benchmark.runIf = (condition) => condition ? benchmark : benchmark.skip;
|
|
2382
|
+
return benchmark;
|
|
2048
2383
|
}
|
|
2049
2384
|
|
|
2050
|
-
export { GLOBAL_EXPECT as G, getDefaultHookTimeout as a,
|
|
2385
|
+
export { GLOBAL_EXPECT as G, getDefaultHookTimeout as a, bench as b, createExpect as c, describe as d, globalExpect as e, clearCollectorContext as f, getCurrentSuite as g, defaultSuite as h, it as i, setHooks as j, getHooks as k, collectorContext as l, getBenchmarkFactory as m, getFn as n, setState as o, getState as p, createSuiteHooks as q, chai$1 as r, suite as s, test as t, withTimeout as w };
|