Skip to content

Commit 9a019ff

Browse files
authored
feat(object-assertion): Matchers use variadic arguments (#89)
1 parent 8808031 commit 9a019ff

2 files changed

Lines changed: 45 additions & 37 deletions

File tree

package/src/lib/ObjectAssertion.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class ObjectAssertion<T extends Struct> extends Assertion<T> {
3636
public toBeEmpty(): this {
3737
const error = new AssertionError({
3838
actual: this.actual,
39+
expected: { },
3940
message: "Expected the value to be an empty object",
4041
});
4142
const invertedError = new AssertionError({
@@ -83,20 +84,21 @@ export class ObjectAssertion<T extends Struct> extends Assertion<T> {
8384
*
8485
* @example
8586
* ```
86-
* expect({ a: 1, b: 2, c: 3 }).toContainAllKeys(["a", "b"]);
87+
* expect({ a: 1, b: 2, c: 3 }).toContainAllKeys("a", "b");
8788
* ```
8889
*
8990
* @param keys the keys that the object should contain
9091
* @returns the assertion instance
9192
*/
92-
public toContainAllKeys(keys: Array<keyof T>): this {
93+
public toContainAllKeys(...keys: Array<keyof T>): this {
9394
const error = new AssertionError({
94-
actual: this.actual,
95+
actual: Object.keys(this.actual),
96+
expected: keys,
9597
message: `Expected the object to contain all the provided keys <${prettify(keys)}>`,
9698
});
9799

98100
const invertedError = new AssertionError({
99-
actual: this.actual,
101+
actual: Object.keys(this.actual),
100102
message: `Expected the object NOT to contain all the provided keys <${prettify(keys)}>`,
101103
});
102104
return this.execute({
@@ -111,20 +113,21 @@ export class ObjectAssertion<T extends Struct> extends Assertion<T> {
111113
*
112114
* @example
113115
* ```
114-
* expect({ a: 1, b: 2, c: 3 }).toContainAnyKeys(["a", "b"]);
116+
* expect({ a: 1, b: 2, c: 3 }).toContainAnyKeys("a", "b");
115117
* ```
116118
*
117119
* @param keys the keys that the object may contain
118120
* @returns the assertion instance
119121
*/
120-
public toContainAnyKeys(keys: Array<keyof T>): this {
122+
public toContainAnyKeys(...keys: Array<keyof T>): this {
121123
const error = new AssertionError({
122-
actual: this.actual,
124+
actual: Object.keys(this.actual),
125+
expected: keys,
123126
message: `Expected the object to contain at least one of the provided keys <${prettify(keys)}>`,
124127
});
125128

126129
const invertedError = new AssertionError({
127-
actual: this.actual,
130+
actual: Object.keys(this.actual),
128131
message: `Expected the object NOT to contain any of the provided keys <${prettify(keys)}>`,
129132
});
130133
return this.execute({
@@ -168,20 +171,21 @@ export class ObjectAssertion<T extends Struct> extends Assertion<T> {
168171
*
169172
* @example
170173
* ```
171-
* expect({ a: 1, b: 2, c: 3 }).toContainAllValues([1, 2]);
174+
* expect({ a: 1, b: 2, c: 3 }).toContainAllValues(1, 2);
172175
* ```
173176
*
174177
* @param values the property values that the object should contain
175178
* @returns the assertion instance
176179
*/
177-
public toContainAllValues(values: Array<T[keyof T]>): this {
180+
public toContainAllValues(...values: Array<T[keyof T]>): this {
178181
const error = new AssertionError({
179-
actual: this.actual,
182+
actual: Object.values(this.actual),
183+
expected: values,
180184
message: `Expected the object to contain all the provided values <${prettify(values)}>`,
181185
});
182186

183187
const invertedError = new AssertionError({
184-
actual: this.actual,
188+
actual: Object.values(this.actual),
185189
message: `Expected the object NOT to contain all the provided values <${prettify(values)}>`,
186190
});
187191
return this.execute({
@@ -199,20 +203,21 @@ export class ObjectAssertion<T extends Struct> extends Assertion<T> {
199203
*
200204
* @example
201205
* ```
202-
* expect({ a: 1, b: 2, c: 3 }).toContainAnyValues([1, 5, 7]);
206+
* expect({ a: 1, b: 2, c: 3 }).toContainAnyValues(1, 5, 7);
203207
* ```
204208
*
205209
* @param values the property values that the object should contain
206210
* @returns the assertion instance
207211
*/
208-
public toContainAnyValues(values: Array<T[keyof T]>): this {
212+
public toContainAnyValues(...values: Array<T[keyof T]>): this {
209213
const error = new AssertionError({
210-
actual: this.actual,
214+
actual: Object.values(this.actual),
215+
expected: values,
211216
message: `Expected the object to contain at least one of the provided values <${prettify(values)}>`,
212217
});
213218

214219
const invertedError = new AssertionError({
215-
actual: this.actual,
220+
actual: Object.values(this.actual),
216221
message: `Expected the object NOT to contain any of the provided values <${prettify(values)}>`,
217222
});
218223
return this.execute({
@@ -268,12 +273,13 @@ export class ObjectAssertion<T extends Struct> extends Assertion<T> {
268273
*/
269274
public toContainAllEntries(...entries: Array<Entry<T>>): this {
270275
const error = new AssertionError({
271-
actual: this.actual,
276+
actual: Object.entries(this.actual),
277+
expected: entries,
272278
message: `Expected the object to contain all the provided entries <${prettify(entries)}>`,
273279
});
274280

275281
const invertedError = new AssertionError({
276-
actual: this.actual,
282+
actual: Object.entries(this.actual),
277283
message: `Expected the object NOT to contain all the provided entries <${prettify(entries)}>`,
278284
});
279285
return this.execute({
@@ -301,12 +307,13 @@ export class ObjectAssertion<T extends Struct> extends Assertion<T> {
301307
*/
302308
public toContainAnyEntries(...entries: Array<Entry<T>>): this {
303309
const error = new AssertionError({
304-
actual: this.actual,
310+
actual: Object.entries(this.actual),
311+
expected: entries,
305312
message: `Expected the object to contain at least one of the provided entries <${prettify(entries)}>`,
306313
});
307314

308315
const invertedError = new AssertionError({
309-
actual: this.actual,
316+
actual: Object.entries(this.actual),
310317
message: `Expected the object NOT to contain any of the provided entries <${prettify(entries)}>`,
311318
});
312319
return this.execute({
@@ -334,6 +341,7 @@ export class ObjectAssertion<T extends Struct> extends Assertion<T> {
334341
public toPartiallyMatch(other: Partial<T>): this {
335342
const error = new AssertionError({
336343
actual: this.actual,
344+
expected: other,
337345
message: "Expected the object to be a partial match",
338346
});
339347

package/test/lib/ObjectAssertion.test.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ describe("[Unit] ObjectAssertion.test.ts", () => {
7878
describe(".toContainAllKeys", () => {
7979
context("when the object contains all the provided keys", () => {
8080
it("returns the assertion instance", () => {
81-
const keys: Array<"myKey" | 2> = ["myKey", 2];
81+
const keys: Array<keyof typeof TEST_OBJ> = ["myKey", 2];
8282
const test = new ObjectAssertion(TEST_OBJ);
8383

84-
assert.deepStrictEqual(test.toContainAllKeys(["myKey", 2]), test);
85-
assert.throws(() => test.not.toContainAllKeys(keys), {
84+
assert.deepStrictEqual(test.toContainAllKeys(...keys), test);
85+
assert.throws(() => test.not.toContainAllKeys(...keys), {
8686
message: `Expected the object NOT to contain all the provided keys <${keys}>`,
8787
name: AssertionError.name,
8888
});
@@ -94,11 +94,11 @@ describe("[Unit] ObjectAssertion.test.ts", () => {
9494
const someKeys = ["truthy", "wrongKey"];
9595
const test = new ObjectAssertion(RECORD);
9696

97-
assert.throws(() => test.toContainAllKeys(someKeys), {
97+
assert.throws(() => test.toContainAllKeys(...someKeys), {
9898
message: `Expected the object to contain all the provided keys <${someKeys}>`,
9999
name: AssertionError.name,
100100
});
101-
assert.deepStrictEqual(test.not.toContainAllKeys(someKeys), test);
101+
assert.deepStrictEqual(test.not.toContainAllKeys(...someKeys), test);
102102
});
103103
});
104104
});
@@ -109,8 +109,8 @@ describe("[Unit] ObjectAssertion.test.ts", () => {
109109
const someKeys = ["truthy", "wrongKey"];
110110
const test = new ObjectAssertion(RECORD);
111111

112-
assert.deepStrictEqual(test.toContainAnyKeys(someKeys), test);
113-
assert.throws(() => test.not.toContainAnyKeys(someKeys), {
112+
assert.deepStrictEqual(test.toContainAnyKeys(...someKeys), test);
113+
assert.throws(() => test.not.toContainAnyKeys(...someKeys), {
114114
message: `Expected the object NOT to contain any of the provided keys <${someKeys}>`,
115115
name: AssertionError.name,
116116
});
@@ -122,11 +122,11 @@ describe("[Unit] ObjectAssertion.test.ts", () => {
122122
const wrongKeys = ["wrongKey", "randomKey"];
123123
const test = new ObjectAssertion(RECORD);
124124

125-
assert.throws(() => test.toContainAnyKeys(wrongKeys), {
125+
assert.throws(() => test.toContainAnyKeys(...wrongKeys), {
126126
message: `Expected the object to contain at least one of the provided keys <${wrongKeys}>`,
127127
name: AssertionError.name,
128128
});
129-
assert.deepStrictEqual(test.not.toContainAnyKeys(wrongKeys), test);
129+
assert.deepStrictEqual(test.not.toContainAnyKeys(...wrongKeys), test);
130130
});
131131
});
132132
});
@@ -165,8 +165,8 @@ describe("[Unit] ObjectAssertion.test.ts", () => {
165165
const allValues = [0, { innerObjKey: 1, message: "inner value" }];
166166
const test = new ObjectAssertion(TEST_OBJ);
167167

168-
assert.deepStrictEqual(test.toContainAllValues(allValues), test);
169-
assert.throws(() => test.not.toContainAllValues(allValues), {
168+
assert.deepStrictEqual(test.toContainAllValues(...allValues), test);
169+
assert.throws(() => test.not.toContainAllValues(...allValues), {
170170
message: `Expected the object NOT to contain all the provided values <${allValues}>`,
171171
name: AssertionError.name,
172172
});
@@ -178,11 +178,11 @@ describe("[Unit] ObjectAssertion.test.ts", () => {
178178
const someValues = [0, { innerObjKey: 1, message: "wrong inner value" }];
179179
const test = new ObjectAssertion(TEST_OBJ);
180180

181-
assert.throws(() => test.toContainAllValues(someValues), {
181+
assert.throws(() => test.toContainAllValues(...someValues), {
182182
message: `Expected the object to contain all the provided values <${someValues}>`,
183183
name: AssertionError.name,
184184
});
185-
assert.deepStrictEqual(test.not.toContainAllValues(someValues), test);
185+
assert.deepStrictEqual(test.not.toContainAllValues(...someValues), test);
186186
});
187187
});
188188
});
@@ -193,8 +193,8 @@ describe("[Unit] ObjectAssertion.test.ts", () => {
193193
const someValues = [0, { innerObjKey: 1, message: "wrong inner value" }];
194194
const test = new ObjectAssertion(TEST_OBJ);
195195

196-
assert.deepStrictEqual(test.toContainAnyValues(someValues), test);
197-
assert.throws(() => test.not.toContainAnyValues(someValues), {
196+
assert.deepStrictEqual(test.toContainAnyValues(...someValues), test);
197+
assert.throws(() => test.not.toContainAnyValues(...someValues), {
198198
message: `Expected the object NOT to contain any of the provided values <${someValues}>`,
199199
name: AssertionError.name,
200200
});
@@ -206,11 +206,11 @@ describe("[Unit] ObjectAssertion.test.ts", () => {
206206
const wrongValues = [10, { innerObjKey: 1, message: "wrong inner value" }];
207207
const test = new ObjectAssertion(TEST_OBJ);
208208

209-
assert.throws(() => test.toContainAnyValues(wrongValues), {
209+
assert.throws(() => test.toContainAnyValues(...wrongValues), {
210210
message: `Expected the object to contain at least one of the provided values <${wrongValues}>`,
211211
name: AssertionError.name,
212212
});
213-
assert.deepStrictEqual(test.not.toContainAnyValues(wrongValues), test);
213+
assert.deepStrictEqual(test.not.toContainAnyValues(...wrongValues), test);
214214
});
215215
});
216216
});

0 commit comments

Comments
 (0)