Skip to content

Commit c7a061a

Browse files
feat: Include aliases toBeSameAs and toBe for toBeSame (#109)
1 parent fec555f commit c7a061a

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

packages/core/src/lib/Assertion.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,48 @@ export class Assertion<T> {
440440
});
441441
}
442442

443+
/**
444+
* Alias of `.toBeSame(..)` assertion.
445+
*
446+
* @example
447+
* ```
448+
* const x = { a: 1 };
449+
* const y = x;
450+
*
451+
* expect(x).toBeSameAs(x);
452+
* expect(x).toBeSameAs(y);
453+
*
454+
* expect(x).not.toBeSameAs({ ...x });
455+
* ```
456+
*
457+
* @param expected the value to compare for referential equality
458+
* @returns the assertion instance
459+
*/
460+
public toBeSameAs(expected: T): this {
461+
return this.toBeSame(expected);
462+
}
463+
464+
/**
465+
* Another alias of `.toBeSame(..)` assertion.
466+
*
467+
* @example
468+
* ```
469+
* const x = { a: 1 };
470+
* const y = x;
471+
*
472+
* expect(x).toBe(x);
473+
* expect(x).toBe(y);
474+
*
475+
* expect(x).not.toBe({ ...x });
476+
* ```
477+
*
478+
* @param expected the value to compare for referential equality
479+
* @returns the assertion instance
480+
*/
481+
public toBe(expected: T): this {
482+
return this.toBeSame(expected);
483+
}
484+
443485
/**
444486
* Checks if the value is of a specific data type. The supported data types
445487
* are the same as the `typeof` operator, plus an additional `array` which

packages/core/test/lib/Assertion.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Sinon from "sinon";
2+
13
import { Assertion, DataType } from "../../src/lib/Assertion";
24
import { StringAssertion } from "../../src/lib/StringAssertion";
35
import { UnsupportedOperationError } from "../../src/lib/errors/UnsupportedOperationError";
@@ -472,6 +474,28 @@ describe("[Unit] Assertion.test.ts", () => {
472474
});
473475
});
474476

477+
describe(".toBeSameAs", () => {
478+
it("aliases .toBeSame(..) method", () => {
479+
const test = new StringAssertion("Foo");
480+
const spy = Sinon.spy(test, "toBeSame");
481+
482+
test.toBeSameAs("Foo");
483+
484+
Sinon.assert.calledWithExactly(spy, "Foo");
485+
});
486+
});
487+
488+
describe(".toBe", () => {
489+
it("aliases .toBeSame(..) method", () => {
490+
const test = new StringAssertion("Foo");
491+
const spy = Sinon.spy(test, "toBeSame");
492+
493+
test.toBe("Foo");
494+
495+
Sinon.assert.calledWithExactly(spy, "Foo");
496+
});
497+
});
498+
475499
describe(".toBeOfType", () => {
476500
context("when the type of the value is of the expected type", () => {
477501
const variants: Array<[DataType, unknown]> = [

0 commit comments

Comments
 (0)