mirror of
https://github.com/pezkuwichain/pezkuwi-dev.git
synced 2026-10-30 06:29:48 +00:00
dev-test: asymmetric matchers in .toEqual; arrayContaining checks entries
.toEqual compared with assert.deepEqual and never looked at matchers, so expect(x).toEqual(expect.arrayContaining(...)) or .toEqual(expect.stringContaining(...)) always failed; matchers only worked inside toHaveBeenCalledWith. arrayContaining was also typed to take a string and checked value.includes(theWholeArray). .toEqual (and .not.toEqual) now apply matchers wherever they appear in the expected value, with the previous deepEqual unchanged when there are none; arrayContaining asserts that every expected entry equals (or matches) one of the array's entries. Seven new cases; with the old .toEqual six of them fail.
This commit is contained in:
1 parent
28566c7d5a
commit
71c6f83ba0
2 files changed
+99
-8
No files matched your search
+36
@@ -219,4 +219,40 @@ describe('expect', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('asymmetric matchers in .toEqual', () => {
|
||||
it('matches an array containing all expected entries, in any order', () => {
|
||||
expect([1, 2, 3]).toEqual(expect.arrayContaining([3, 1]));
|
||||
expect(['a', 'b']).toEqual(expect.arrayContaining(['b', 'a']));
|
||||
});
|
||||
|
||||
it('fails when an expected entry is missing', () => {
|
||||
expect(() => expect([1, 2]).toEqual(expect.arrayContaining([3]))).toThrow();
|
||||
});
|
||||
|
||||
it('compares array entries deeply', () => {
|
||||
expect([{ a: 1 }, { b: 2 }]).toEqual(expect.arrayContaining([{ b: 2 }]));
|
||||
expect(() => expect([{ a: 1 }]).toEqual(expect.arrayContaining([{ a: 2 }]))).toThrow();
|
||||
});
|
||||
|
||||
it('matches a string containing the expected part', () => {
|
||||
expect('hello world').toEqual(expect.stringContaining('lo w'));
|
||||
expect(() => expect('hello').toEqual(expect.stringContaining('xyz'))).toThrow();
|
||||
});
|
||||
|
||||
it('applies matchers nested in objects and arrays', () => {
|
||||
expect({ a: [1, 2], b: 'x' }).toEqual({ a: expect.arrayContaining([2]), b: 'x' });
|
||||
expect([{ a: 'xyz' }]).toEqual([{ a: expect.stringContaining('y') }]);
|
||||
expect(() => expect({ a: [1], b: 'x' }).toEqual({ a: expect.arrayContaining([2]), b: 'x' })).toThrow();
|
||||
});
|
||||
|
||||
it('still requires equal keys around a nested matcher', () => {
|
||||
expect(() => expect({ a: [1], b: 'x', c: 1 }).toEqual({ a: expect.arrayContaining([1]), b: 'x' })).toThrow();
|
||||
});
|
||||
|
||||
it('supports .not with a matcher', () => {
|
||||
expect([1, 2]).not.toEqual(expect.arrayContaining([3]));
|
||||
expect(() => expect([1, 2]).not.toEqual(expect.arrayContaining([2]))).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user