Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-16917] Remove jest-extended dependency #12798

Merged
merged 11 commits into from
Jan 15, 2025
1 change: 1 addition & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
"jest-junit",
"jest-mock-extended",
"jest-preset-angular",
"jest-diff",
Hinton marked this conversation as resolved.
Show resolved Hide resolved
"lint-staged",
"ts-jest"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("DefaultvNextCollectionService", () => {

// Assert emitted values
expect(result.length).toBe(2);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{
id: collection1.id,
name: "DEC_NAME_" + collection1.id,
Expand Down Expand Up @@ -167,7 +167,7 @@ describe("DefaultvNextCollectionService", () => {
const result = await firstValueFrom(collectionService.encryptedCollections$(userId));

expect(result.length).toBe(2);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{
id: collection1.id,
name: makeEncString("ENC_NAME_" + collection1.id),
Expand Down Expand Up @@ -205,7 +205,7 @@ describe("DefaultvNextCollectionService", () => {

const result = await firstValueFrom(collectionService.encryptedCollections$(userId));
expect(result.length).toBe(3);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{
id: collection1.id,
name: makeEncString("UPDATED_ENC_NAME_" + collection1.id),
Expand All @@ -230,7 +230,7 @@ describe("DefaultvNextCollectionService", () => {

const result = await firstValueFrom(collectionService.encryptedCollections$(userId));
expect(result.length).toBe(1);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{
id: collection1.id,
name: makeEncString("ENC_NAME_" + collection1.id),
Expand All @@ -253,7 +253,7 @@ describe("DefaultvNextCollectionService", () => {

const result = await firstValueFrom(collectionService.encryptedCollections$(userId));
expect(result.length).toBe(1);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{
id: newCollection3.id,
name: makeEncString("ENC_NAME_" + newCollection3.id),
Expand Down
4 changes: 4 additions & 0 deletions libs/admin-console/test.setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { webcrypto } from "crypto";

import { addCustomMatchers } from "@bitwarden/common/spec";
import "jest-preset-angular/setup-jest";

addCustomMatchers();

Object.defineProperty(window, "CSS", { value: null });
Object.defineProperty(window, "getComputedStyle", {
value: () => {
Expand Down
2 changes: 1 addition & 1 deletion libs/admin-console/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../shared/tsconfig.libs",
"include": ["src", "spec"],
"include": ["src", "spec", "../../libs/common/custom-matchers.d.ts"],
Hinton marked this conversation as resolved.
Show resolved Hide resolved
"exclude": ["node_modules", "dist"]
}
12 changes: 7 additions & 5 deletions libs/common/spec/matchers/index.ts
audreyality marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import * as matchers from "jest-extended";

import { toBeFulfilled, toBeResolved, toBeRejected } from "./promise-fulfilled";
import { toAlmostEqual } from "./to-almost-equal";
import { toContainPartialObjects } from "./to-contain-partial-objects";
import { toEqualBuffer } from "./to-equal-buffer";

export * from "./to-equal-buffer";
export * from "./to-almost-equal";
export * from "./promise-fulfilled";

// add all jest-extended matchers
expect.extend(matchers);

export function addCustomMatchers() {
expect.extend({
toEqualBuffer: toEqualBuffer,
toAlmostEqual: toAlmostEqual,
toBeFulfilled: toBeFulfilled,
toBeResolved: toBeResolved,
toBeRejected: toBeRejected,
toContainPartialObjects,
});
}

Expand Down Expand Up @@ -59,4 +56,9 @@ export interface CustomMatchers<R = unknown> {
* @returns CustomMatcherResult indicating whether or not the test passed
*/
toBeRejected(withinMs?: number): Promise<R>;
/**
* Matches if the received array contains all the expected objects using partial matching (expect.objectContaining).
* @param expected An array of partial objects that should be contained in the received array.
*/
toContainPartialObjects<T>(expected: Array<T>): R;
}
77 changes: 77 additions & 0 deletions libs/common/spec/matchers/to-contain-partial-objects.spec.ts
audreyality marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
describe("toContainPartialObjects", () => {
describe("matches", () => {
it("if the array only contains the partial objects", () => {
const actual = [
{
id: 1,
name: "foo",
},
{
id: 2,
name: "bar",
},
];

const expected = [{ id: 1 }, { id: 2 }];

expect(actual).toContainPartialObjects(expected);
});

it("if the array contains the partial objects and other objects", () => {
const actual = [
{
id: 1,
name: "foo",
},
{
id: 2,
name: "bar",
},
{
id: 3,
name: "baz",
},
];

const expected = [{ id: 1 }, { id: 2 }];

expect(actual).toContainPartialObjects(expected);
});
});

describe("doesn't match", () => {
it("if the array does not contain any partial objects", () => {
const actual = [
{
id: 1,
name: "foo",
},
{
id: 2,
name: "bar",
},
];

const expected = [{ id: 1, name: "Foo" }];

expect(actual).not.toContainPartialObjects(expected);
});

it("if the array contains some but not all partial objects", () => {
const actual = [
{
id: 1,
name: "foo",
},
{
id: 2,
name: "bar",
},
];

const expected = [{ id: 2 }, { id: 3 }];

expect(actual).not.toContainPartialObjects(expected);
});
});
});
16 changes: 16 additions & 0 deletions libs/common/spec/matchers/to-contain-partial-objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { diff } from "jest-diff";

export const toContainPartialObjects: jest.CustomMatcher = function <T>(
received: Array<T>,
expected: Array<T>,
) {
const pass = this.equals(
received,
expect.arrayContaining(expected.map((e) => expect.objectContaining(e))),
audreyality marked this conversation as resolved.
Show resolved Hide resolved
);

return {
message: () => diff(expected, received),
pass: pass,
};
};
2 changes: 1 addition & 1 deletion libs/common/src/tools/rx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("errorOnChange", () => {

source$.complete();

expect(complete).toBeTrue();
expect(complete).toBe(true);
});

it("errors when the input changes", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe("Folder Service", () => {
const result = await firstValueFrom(folderService.folders$(mockUserId));

expect(result.length).toBe(2);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{ id: "1", name: makeEncString("ENC_STRING_1") },
{ id: "2", name: makeEncString("ENC_STRING_2") },
]);
Expand All @@ -96,7 +96,7 @@ describe("Folder Service", () => {
const result = await firstValueFrom(folderService.folderViews$(mockUserId));

expect(result.length).toBe(3);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{ id: "1", name: "DEC" },
{ id: "2", name: "DEC" },
{ name: "No Folder" },
Expand Down
24 changes: 1 addition & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"html-webpack-injector": "1.1.4",
"html-webpack-plugin": "5.6.3",
"husky": "9.1.4",
"jest-extended": "4.0.2",
"jest-diff": "29.7.0",
"jest-junit": "16.0.0",
"jest-mock-extended": "3.0.7",
"jest-preset-angular": "14.1.1",
Expand Down
Loading