Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/react-native-fantom/runtime/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -144,6 +169,24 @@ class Expect {
}
}

toBeFalsy(): void {
const pass = Boolean(this.#received) === false;
if (!this.#isExpectedResult(pass)) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)}${this.#maybeNotLabel()} to be falsy`,
).blameToPreviousFrame();
}
}

toBeTruthy(): void {
const pass = Boolean(this.#received);
if (!this.#isExpectedResult(pass)) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)}${this.#maybeNotLabel()} to be truthy`,
).blameToPreviousFrame();
}
}

toThrow(expected?: string): void {
if (expected != null && typeof expected !== 'string') {
throw new ErrorWithCustomBlame(
Expand Down
71 changes: 71 additions & 0 deletions packages/react-native-fantom/src/__tests__/expect-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down Expand Up @@ -178,6 +210,45 @@ describe('expect', () => {
}).toThrow();
});

test('toBeFalsy', () => {
expect(false).toBeFalsy();
expect(0).toBeFalsy();
expect('').toBeFalsy();
expect(null).toBeFalsy();
expect(undefined).toBeFalsy();
expect(NaN).toBeFalsy();
expect([]).not.toBeFalsy();
expect(['']).not.toBeFalsy();

expect(() => {
expect(true).toBeFalsy();
}).toThrow();

expect(() => {
expect(false).not.toBeFalsy();
}).toThrow();
});

test('toBeTruthy', () => {
expect(true).toBeTruthy();
expect([]).toBeTruthy();
expect('a').toBeTruthy();
expect(false).not.toBeTruthy();
expect(0).not.toBeTruthy();
expect('').not.toBeTruthy();
expect(null).not.toBeTruthy();
expect(undefined).not.toBeTruthy();
expect(NaN).not.toBeTruthy();

expect(() => {
expect(false).toBeTruthy();
}).toThrow();

expect(() => {
expect(true).not.toBeTruthy();
}).toThrow();
});

['toBeCalled', 'toHaveBeenCalled'].map(toHaveBeenCalledAlias =>
test(toHaveBeenCalledAlias, () => {
const fn = jest.fn();
Expand Down