From a32bf42213727854f1915a81edfc742daf196df4 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Fri, 28 Jul 2023 12:16:49 -0500 Subject: [PATCH] fix: communication-sms: remove mocha arrow functions (#26659) ### Packages impacted by this PR `sdk\communication\communication-sms` ### Issues associated with this PR #13005 ### Describe the problem that is addressed by this PR The existing mocha tests for the `sdk\communication\communication-sms` made use of the arrow syntax for callback functions. Mocha recommends not to do this because you lose access to the mocha context (https://mochajs.org/#arrow-functions). ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? The reason for utilizing the function keyword instead of an arrow syntax to write the callback functions in these mocha tests is to maintain access to the mocha context. ### Are there test cases added in this PR? _(If not, why?)_ No additional test cases were added in this PR as the change only required modifying existing test cases. ### Provide a list of related PRs _(if any)_ #23761 - Same fix, but for the `sdk\search\search-documents` package #23789 - Same fix but for the `sdk\attestation\attestation` package #23835 - Same fix but for the `sdk\batch\batch` package #23850 - Same fix but for the `sdk\cognitivelanguage\ai-language-conversations` package #23881 - Same fix but for the `sdk\cognitiveservices\cognitiveservices-luis-authoring` package #24126 - Same fix but for the `sdk\cognitiveservices\cognitiveservices-luis-runtime` package #21470 - Same fix but for the `sdk\communication\communication-chat` package #24746 - Same fix but for the `sdk\communication\communication-common` package #24747 - Same fix but for the `sdk\communication\communication-email` package #24797 - Same fix but for the `sdk\communication\communication-identity` package #24800 - Same fix but for the `sdk\communication\communication-rooms` package #24865 - Same fix but for the `sdk\communication\communication-job-router` package #25148 - Same fix but for the `sdk\confidentialledger\confidential-ledger-rest` package ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ **_Not applicable_** ### Checklists - [x] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary) --- .../smsClient.internal.mocked.spec.ts | 14 +++++----- .../test/internal/smsClient.internal.spec.ts | 10 +++---- .../test/public/smsClient.mocked.spec.ts | 26 +++++++++---------- .../test/public/smsClient.spec.ts | 10 +++---- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/sdk/communication/communication-sms/test/internal/smsClient.internal.mocked.spec.ts b/sdk/communication/communication-sms/test/internal/smsClient.internal.mocked.spec.ts index 06d62f4982eb..8931e814573c 100644 --- a/sdk/communication/communication-sms/test/internal/smsClient.internal.mocked.spec.ts +++ b/sdk/communication/communication-sms/test/internal/smsClient.internal.mocked.spec.ts @@ -3,8 +3,8 @@ import { HttpClient } from "@azure/core-rest-pipeline"; -import { Uuid } from "../../src/utils/uuid"; import { generateSendMessageRequest } from "../../src/utils/smsUtils"; +import { Uuid } from "../../src/utils/uuid"; import { assert } from "chai"; import sinon from "sinon"; @@ -15,7 +15,7 @@ import { MockHttpClient } from "../public/utils/mockHttpClient"; const API_VERSION = apiVersion.mapper.defaultValue; const TEST_NUMBER = "+14255550123"; -describe("[mocked] SmsClient Internal", async () => { +describe("[mocked] SmsClient Internal", async function () { const baseUri = "https://contoso.api.fake"; const connectionString = `endpoint=${baseUri};accesskey=banana`; let sendRequestSpy: sinon.SinonSpy; @@ -29,9 +29,9 @@ describe("[mocked] SmsClient Internal", async () => { message: "message", }; - describe("when sending an SMS", () => { + describe("when sending an SMS", function () { let smsClient: SmsClient; - beforeEach(() => { + beforeEach(function () { uuidStub = sinon.stub(Uuid, "generateUuid"); uuidStub.returns(mockedGuid); sendRequestSpy = sinon.spy(mockHttpClient, "sendRequest"); @@ -39,7 +39,7 @@ describe("[mocked] SmsClient Internal", async () => { smsClient = new SmsClient(connectionString, { httpClient: mockHttpClient }); }); - it("sends with the correct request body", async () => { + it("sends with the correct request body", async function () { await smsClient.send(testSendRequest); const request = sendRequestSpy.getCall(0).args[0]; @@ -49,14 +49,14 @@ describe("[mocked] SmsClient Internal", async () => { assert.deepEqual(JSON.parse(request.body as string), expectedRequestBody); }); - it("generates a new repeatability id each time", async () => { + it("generates a new repeatability id each time", async function () { await smsClient.send(testSendRequest); assert.isTrue(uuidStub.calledOnce); await smsClient.send(testSendRequest); assert.isTrue(uuidStub.calledTwice); }); - afterEach(() => { + afterEach(function () { sinon.restore(); }); }); diff --git a/sdk/communication/communication-sms/test/internal/smsClient.internal.spec.ts b/sdk/communication/communication-sms/test/internal/smsClient.internal.spec.ts index 39c418509012..3593df3e4447 100644 --- a/sdk/communication/communication-sms/test/internal/smsClient.internal.spec.ts +++ b/sdk/communication/communication-sms/test/internal/smsClient.internal.spec.ts @@ -8,20 +8,20 @@ * These tests will be skipped in Live Mode since the public tests run in live mode only. */ -import { matrix } from "@azure/test-utils"; import { Recorder, isLiveMode, isPlaybackMode } from "@azure-tools/test-recorder"; +import { matrix } from "@azure/test-utils"; +import { Context } from "mocha"; import * as sinon from "sinon"; +import { SmsClient } from "../../src"; import { Uuid } from "../../src/utils/uuid"; +import sendSmsSuites from "../public/suites/smsClient.send"; import { createRecordedSmsClient, createRecordedSmsClientWithToken, } from "../public/utils/recordedClient"; -import { Context } from "mocha"; -import sendSmsSuites from "../public/suites/smsClient.send"; -import { SmsClient } from "../../src"; matrix([[true, false]], async function (useAad: boolean) { - describe(`SmsClient [Playback/Record]${useAad ? " [AAD]" : ""}`, async () => { + describe(`SmsClient [Playback/Record]${useAad ? " [AAD]" : ""}`, async function () { let recorder: Recorder; let client: SmsClient; diff --git a/sdk/communication/communication-sms/test/public/smsClient.mocked.spec.ts b/sdk/communication/communication-sms/test/public/smsClient.mocked.spec.ts index f49046808fb1..57bc16bcc113 100644 --- a/sdk/communication/communication-sms/test/public/smsClient.mocked.spec.ts +++ b/sdk/communication/communication-sms/test/public/smsClient.mocked.spec.ts @@ -1,18 +1,18 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { isNode } from "@azure/core-util"; -import { HttpClient } from "@azure/core-rest-pipeline"; import { AzureKeyCredential } from "@azure/core-auth"; +import { HttpClient } from "@azure/core-rest-pipeline"; +import { isNode } from "@azure/core-util"; +import { TokenCredential } from "@azure/identity"; import { assert } from "chai"; import sinon from "sinon"; import { SmsClient, SmsClientOptions, SmsSendRequest } from "../../src"; import { MockHttpClient } from "./utils/mockHttpClient"; -import { TokenCredential } from "@azure/identity"; const TEST_NUMBER = "+14255550123"; -describe("[mocked] SmsClient", async () => { +describe("[mocked] SmsClient", async function () { const baseUri = "https://contoso.api.fake"; const connectionString = `endpoint=${baseUri};accesskey=banana`; const dateHeader = "x-ms-date"; @@ -25,16 +25,16 @@ describe("[mocked] SmsClient", async () => { message: "message", }; - describe("when instantiating SMS client", () => { - it("can instantiate with a connection string", async () => { + describe("when instantiating SMS client", function () { + it("can instantiate with a connection string", async function () { new SmsClient(connectionString); }); - it("can instantiate with a url and KeyCredential ", async () => { + it("can instantiate with a url and KeyCredential ", async function () { new SmsClient(baseUri, new AzureKeyCredential("banana")); }); - it("can instantiate with a token", async () => { + it("can instantiate with a token", async function () { const fakeToken: TokenCredential = { getToken: async (_scopes) => { return { token: "testToken", expiresOnTimestamp: 11111 }; @@ -44,9 +44,9 @@ describe("[mocked] SmsClient", async () => { }); }); - describe("when sending an SMS", () => { + describe("when sending an SMS", function () { let smsClient: SmsClient; - beforeEach(() => { + beforeEach(function () { sendRequestSpy = sinon.spy(mockHttpClient, "sendRequest"); sinon.useFakeTimers(); // workaround: casting because min testing has issues with httpClient newer versions having extra optional fields @@ -55,7 +55,7 @@ describe("[mocked] SmsClient", async () => { } as SmsClientOptions); }); - it("sends with the correct headers", async () => { + it("sends with the correct headers", async function () { await smsClient.send(testSendRequest); const request = sendRequestSpy.getCall(0).args[0]; @@ -70,7 +70,7 @@ describe("[mocked] SmsClient", async () => { ); }); - it("returns the correct results", async () => { + it("returns the correct results", async function () { const smsTestResults = await smsClient.send(testSendRequest); const smsTestResult = smsTestResults[0]; @@ -79,7 +79,7 @@ describe("[mocked] SmsClient", async () => { assert.equal(smsTestResult.messageId, "id"); }); - afterEach(() => { + afterEach(function () { sinon.restore(); }); }); diff --git a/sdk/communication/communication-sms/test/public/smsClient.spec.ts b/sdk/communication/communication-sms/test/public/smsClient.spec.ts index 2aefd9d9aed9..87440fdb2592 100644 --- a/sdk/communication/communication-sms/test/public/smsClient.spec.ts +++ b/sdk/communication/communication-sms/test/public/smsClient.spec.ts @@ -6,17 +6,17 @@ * They are duplicated in an internal test which contains workaround logic to record/playback the tests */ -import { matrix } from "@azure/test-utils"; import { Recorder, env, isPlaybackMode } from "@azure-tools/test-recorder"; -import { createRecordedSmsClient, createRecordedSmsClientWithToken } from "./utils/recordedClient"; +import { matrix } from "@azure/test-utils"; import { Context } from "mocha"; -import sendSmsSuites from "./suites/smsClient.send"; -import { SmsClient } from "../../src"; import sinon from "sinon"; +import { SmsClient } from "../../src"; import { Uuid } from "../../src/utils/uuid"; +import sendSmsSuites from "./suites/smsClient.send"; +import { createRecordedSmsClient, createRecordedSmsClientWithToken } from "./utils/recordedClient"; matrix([[true, false]], async function (useAad: boolean) { - describe(`SmsClient [Live]${useAad ? " [AAD]" : ""}`, async () => { + describe(`SmsClient [Live]${useAad ? " [AAD]" : ""}`, async function () { let recorder: Recorder; let client: SmsClient;