Skip to content

Commit

Permalink
feat: Include aliases toBeSameAs and toBe for toBeSame (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
CristhianMotoche authored Oct 31, 2023
1 parent fec555f commit c7a061a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/core/src/lib/Assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,48 @@ export class Assertion<T> {
});
}

/**
* Alias of `.toBeSame(..)` assertion.
*
* @example
* ```
* const x = { a: 1 };
* const y = x;
*
* expect(x).toBeSameAs(x);
* expect(x).toBeSameAs(y);
*
* expect(x).not.toBeSameAs({ ...x });
* ```
*
* @param expected the value to compare for referential equality
* @returns the assertion instance
*/
public toBeSameAs(expected: T): this {
return this.toBeSame(expected);
}

/**
* Another alias of `.toBeSame(..)` assertion.
*
* @example
* ```
* const x = { a: 1 };
* const y = x;
*
* expect(x).toBe(x);
* expect(x).toBe(y);
*
* expect(x).not.toBe({ ...x });
* ```
*
* @param expected the value to compare for referential equality
* @returns the assertion instance
*/
public toBe(expected: T): this {
return this.toBeSame(expected);
}

/**
* Checks if the value is of a specific data type. The supported data types
* are the same as the `typeof` operator, plus an additional `array` which
Expand Down
24 changes: 24 additions & 0 deletions packages/core/test/lib/Assertion.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Sinon from "sinon";

import { Assertion, DataType } from "../../src/lib/Assertion";
import { StringAssertion } from "../../src/lib/StringAssertion";
import { UnsupportedOperationError } from "../../src/lib/errors/UnsupportedOperationError";
Expand Down Expand Up @@ -472,6 +474,28 @@ describe("[Unit] Assertion.test.ts", () => {
});
});

describe(".toBeSameAs", () => {
it("aliases .toBeSame(..) method", () => {
const test = new StringAssertion("Foo");
const spy = Sinon.spy(test, "toBeSame");

test.toBeSameAs("Foo");

Sinon.assert.calledWithExactly(spy, "Foo");
});
});

describe(".toBe", () => {
it("aliases .toBeSame(..) method", () => {
const test = new StringAssertion("Foo");
const spy = Sinon.spy(test, "toBeSame");

test.toBe("Foo");

Sinon.assert.calledWithExactly(spy, "Foo");
});
});

describe(".toBeOfType", () => {
context("when the type of the value is of the expected type", () => {
const variants: Array<[DataType, unknown]> = [
Expand Down

0 comments on commit c7a061a

Please sign in to comment.