mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
[skip ci] Add .toStrictEqual (#49176)
Summary: Changelog: [Internal] Add missing jest expect apis Reviewed By: rubennorte Differential Revision: D69123117
This commit is contained in:
committed by
Facebook GitHub Bot
parent
a702238bed
commit
61d6bca1d4
+25
@@ -89,6 +89,31 @@ class Expect {
|
||||
}
|
||||
}
|
||||
|
||||
toStrictEqual(expected: mixed): void {
|
||||
let expectedType: mixed =
|
||||
typeof expected === 'object' && expected !== null
|
||||
? Object.getPrototypeOf(expected)
|
||||
: null;
|
||||
let receivedType: mixed =
|
||||
typeof this.#received === 'object' && this.#received !== null
|
||||
? Object.getPrototypeOf(this.#received)
|
||||
: null;
|
||||
const pass =
|
||||
deepEqual(this.#received, expected, {strict: true}) &&
|
||||
expectedType === receivedType;
|
||||
if (!this.#isExpectedResult(pass)) {
|
||||
throw new ErrorWithCustomBlame(
|
||||
`Expected${this.#maybeNotLabel()} to strictly equal:\n${
|
||||
diff(expected, this.#received, {
|
||||
contextLines: 1,
|
||||
expand: false,
|
||||
omitAnnotationLines: true,
|
||||
}) ?? 'Failed to compare outputs'
|
||||
}`,
|
||||
).blameToPreviousFrame();
|
||||
}
|
||||
}
|
||||
|
||||
toBe(expected: mixed): void {
|
||||
const pass = this.#received === expected;
|
||||
if (!this.#isExpectedResult(pass)) {
|
||||
|
||||
@@ -111,6 +111,38 @@ describe('expect', () => {
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
test('toStrictEqual', () => {
|
||||
class LaCroix {
|
||||
flavor: string;
|
||||
constructor(flavor: string) {
|
||||
this.flavor = flavor;
|
||||
}
|
||||
}
|
||||
|
||||
expect({a: undefined, b: 2}).not.toStrictEqual({b: 2});
|
||||
expect([2, undefined]).not.toStrictEqual([2]);
|
||||
expect([2]).not.toStrictEqual([2, undefined]);
|
||||
// This is part of spec https://jestjs.io/docs/expect#tostrictequalvalue
|
||||
// eslint-disable-next-line no-sparse-arrays
|
||||
expect([, 2]).not.toStrictEqual([undefined, 2]);
|
||||
expect(new LaCroix('lemon')).not.toStrictEqual({flavor: 'lemon'});
|
||||
|
||||
expect({a: 1}).toStrictEqual({a: 1});
|
||||
expect([2, undefined]).toStrictEqual([2, undefined]);
|
||||
// This is part of spec https://jestjs.io/docs/expect#tostrictequalvalue
|
||||
// eslint-disable-next-line no-sparse-arrays
|
||||
expect([, 1]).toStrictEqual([, 1]);
|
||||
expect(new LaCroix('lemon')).toStrictEqual(new LaCroix('lemon'));
|
||||
|
||||
expect(() => {
|
||||
expect(new LaCroix('lemon')).toStrictEqual({flavor: 'lemon'});
|
||||
}).toThrow();
|
||||
|
||||
expect(() => {
|
||||
expect(new LaCroix('lemon')).not.toStrictEqual(new LaCroix('lemon'));
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
test('toBeInstanceOf', () => {
|
||||
class Class {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user