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

Hackathon: msal-browser - Replaced sinon with jest #7337

Merged
merged 11 commits into from
Sep 27, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Replaced sinon with jest in all the msal-browser tests #7337",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "none"
}
2 changes: 0 additions & 2 deletions lib/msal-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
"@rollup/plugin-typescript": "^11.0.0",
"@types/jest": "^29.5.0",
"@types/node": "^20.3.1",
"@types/sinon": "^7.5.0",
"dotenv": "^8.2.0",
"eslint-config-msal": "file:../../shared-configs/eslint-config-msal",
"fake-indexeddb": "^3.1.3",
Expand All @@ -97,7 +96,6 @@
"rollup": "^3.29.5",
"rollup-msal": "file:../../shared-configs/rollup-msal",
"shx": "^0.3.2",
"sinon": "^7.5.0",
"ssri": "^8.0.1",
"ts-jest": "^29.2.5",
"ts-jest-resolver": "^2.0.1",
Expand Down
1,669 changes: 797 additions & 872 deletions lib/msal-browser/test/app/PublicClientApplication.spec.ts

Large diffs are not rendered by default.

37 changes: 19 additions & 18 deletions lib/msal-browser/test/broker/NativeMessageHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,33 @@ import {
AuthErrorMessage,
IPerformanceClient,
} from "@azure/msal-common";
import sinon from "sinon";
import { NativeMessageHandler } from "../../src/broker/nativeBroker/NativeMessageHandler";
import { BrowserAuthError, BrowserAuthErrorMessage } from "../../src";
import { NativeExtensionMethod } from "../../src/utils/BrowserConstants";
import { NativeAuthError } from "../../src/error/NativeAuthError";
import { getDefaultPerformanceClient } from "../utils/TelemetryUtils";
import { CryptoOps } from "../../src/crypto/CryptoOps";
import { NativeMessageHandler } from "../../src/broker/nativeBroker/NativeMessageHandler.js";
import { BrowserAuthError, BrowserAuthErrorMessage } from "../../src/index.js";
import { NativeExtensionMethod } from "../../src/utils/BrowserConstants.js";
import { NativeAuthError } from "../../src/error/NativeAuthError.js";
import { getDefaultPerformanceClient } from "../utils/TelemetryUtils.js";
import { CryptoOps } from "../../src/crypto/CryptoOps.js";

let performanceClient: IPerformanceClient;

describe("NativeMessageHandler Tests", () => {
let postMessageSpy: sinon.SinonSpy;
let postMessageSpy: jest.SpyInstance;
let mcPort: MessagePort;
let cryptoInterface: CryptoOps;
globalThis.MessageChannel = require("worker_threads").MessageChannel; // jsdom does not include an implementation for MessageChannel

beforeEach(() => {
postMessageSpy = sinon.spy(window, "postMessage");
sinon.stub(MessageEvent.prototype, "source").get(() => window); // source property not set by jsdom window messaging APIs
postMessageSpy = jest.spyOn(window, "postMessage");
jest.spyOn(MessageEvent.prototype, "source", "get").mockReturnValue(
window
); // source property not set by jsdom window messaging APIs
performanceClient = getDefaultPerformanceClient();
cryptoInterface = new CryptoOps(new Logger({}));
});

afterEach(() => {
mcPort.close();
sinon.restore();
jest.restoreAllMocks();
});

describe("createProvider", () => {
Expand All @@ -52,7 +53,7 @@ describe("NativeMessageHandler Tests", () => {
},
};

mcPort = postMessageSpy.args[0][2][0];
mcPort = postMessageSpy.mock.calls[0][2][0];
if (!mcPort) {
throw new Error("MessageChannel port was not transferred");
}
Expand Down Expand Up @@ -85,7 +86,7 @@ describe("NativeMessageHandler Tests", () => {
},
};

mcPort = postMessageSpy.args[0][2][0];
mcPort = postMessageSpy.mock.calls[0][2][0];
if (!mcPort) {
throw new Error("MessageChannel port was not transferred");
}
Expand Down Expand Up @@ -138,7 +139,7 @@ describe("NativeMessageHandler Tests", () => {
},
};

mcPort = postMessageSpy.args[1][2][0];
mcPort = postMessageSpy.mock.calls[1][2][0];
if (!mcPort) {
throw new Error("MessageChannel port was not transferred");
}
Expand Down Expand Up @@ -252,7 +253,7 @@ describe("NativeMessageHandler Tests", () => {
},
};

mcPort = postMessageSpy.args[0][2][0];
mcPort = postMessageSpy.mock.calls[0][2][0];
if (!mcPort) {
throw new Error("MessageChannel port was not transferred");
}
Expand Down Expand Up @@ -309,7 +310,7 @@ describe("NativeMessageHandler Tests", () => {
},
};

mcPort = postMessageSpy.args[0][2][0];
mcPort = postMessageSpy.mock.calls[0][2][0];
if (!mcPort) {
throw new Error("MessageChannel port was not transferred");
}
Expand Down Expand Up @@ -375,7 +376,7 @@ describe("NativeMessageHandler Tests", () => {
},
};

mcPort = postMessageSpy.args[0][2][0];
mcPort = postMessageSpy.mock.calls[0][2][0];
if (!mcPort) {
throw new Error("MessageChannel port was not transferred");
}
Expand Down Expand Up @@ -439,7 +440,7 @@ describe("NativeMessageHandler Tests", () => {
},
};

mcPort = postMessageSpy.args[0][2][0];
mcPort = postMessageSpy.mock.calls[0][2][0];
if (!mcPort) {
throw new Error("MessageChannel port was not transferred");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/msal-browser/test/cache/AsyncMemoryStorage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ describe("AsyncMemoryStorage Unit Tests", () => {
expect(item).toBe(TEST_CACHE_ITEMS.TestItem.value);
});

it("should get item from persistent cache when in-memory cache doesn't contain item", async () => {
it("should get item from persistent cache when in-memory cache does not contain item", async () => {
mockDatabase[TEST_DB_TABLE_NAME][
TEST_CACHE_ITEMS.TestItem.key
] = TEST_CACHE_ITEMS.TestItem.value;
Expand Down
Loading
Loading