Skip to content

Commit a32bf42

Browse files
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)
1 parent e0d00e9 commit a32bf42

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

sdk/communication/communication-sms/test/internal/smsClient.internal.mocked.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
import { HttpClient } from "@azure/core-rest-pipeline";
55

6-
import { Uuid } from "../../src/utils/uuid";
76
import { generateSendMessageRequest } from "../../src/utils/smsUtils";
7+
import { Uuid } from "../../src/utils/uuid";
88

99
import { assert } from "chai";
1010
import sinon from "sinon";
@@ -15,7 +15,7 @@ import { MockHttpClient } from "../public/utils/mockHttpClient";
1515
const API_VERSION = apiVersion.mapper.defaultValue;
1616
const TEST_NUMBER = "+14255550123";
1717

18-
describe("[mocked] SmsClient Internal", async () => {
18+
describe("[mocked] SmsClient Internal", async function () {
1919
const baseUri = "https://contoso.api.fake";
2020
const connectionString = `endpoint=${baseUri};accesskey=banana`;
2121
let sendRequestSpy: sinon.SinonSpy;
@@ -29,17 +29,17 @@ describe("[mocked] SmsClient Internal", async () => {
2929
message: "message",
3030
};
3131

32-
describe("when sending an SMS", () => {
32+
describe("when sending an SMS", function () {
3333
let smsClient: SmsClient;
34-
beforeEach(() => {
34+
beforeEach(function () {
3535
uuidStub = sinon.stub(Uuid, "generateUuid");
3636
uuidStub.returns(mockedGuid);
3737
sendRequestSpy = sinon.spy(mockHttpClient, "sendRequest");
3838
sinon.useFakeTimers();
3939
smsClient = new SmsClient(connectionString, { httpClient: mockHttpClient });
4040
});
4141

42-
it("sends with the correct request body", async () => {
42+
it("sends with the correct request body", async function () {
4343
await smsClient.send(testSendRequest);
4444

4545
const request = sendRequestSpy.getCall(0).args[0];
@@ -49,14 +49,14 @@ describe("[mocked] SmsClient Internal", async () => {
4949
assert.deepEqual(JSON.parse(request.body as string), expectedRequestBody);
5050
});
5151

52-
it("generates a new repeatability id each time", async () => {
52+
it("generates a new repeatability id each time", async function () {
5353
await smsClient.send(testSendRequest);
5454
assert.isTrue(uuidStub.calledOnce);
5555
await smsClient.send(testSendRequest);
5656
assert.isTrue(uuidStub.calledTwice);
5757
});
5858

59-
afterEach(() => {
59+
afterEach(function () {
6060
sinon.restore();
6161
});
6262
});

sdk/communication/communication-sms/test/internal/smsClient.internal.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
* These tests will be skipped in Live Mode since the public tests run in live mode only.
99
*/
1010

11-
import { matrix } from "@azure/test-utils";
1211
import { Recorder, isLiveMode, isPlaybackMode } from "@azure-tools/test-recorder";
12+
import { matrix } from "@azure/test-utils";
13+
import { Context } from "mocha";
1314
import * as sinon from "sinon";
15+
import { SmsClient } from "../../src";
1416
import { Uuid } from "../../src/utils/uuid";
17+
import sendSmsSuites from "../public/suites/smsClient.send";
1518
import {
1619
createRecordedSmsClient,
1720
createRecordedSmsClientWithToken,
1821
} from "../public/utils/recordedClient";
19-
import { Context } from "mocha";
20-
import sendSmsSuites from "../public/suites/smsClient.send";
21-
import { SmsClient } from "../../src";
2222

2323
matrix([[true, false]], async function (useAad: boolean) {
24-
describe(`SmsClient [Playback/Record]${useAad ? " [AAD]" : ""}`, async () => {
24+
describe(`SmsClient [Playback/Record]${useAad ? " [AAD]" : ""}`, async function () {
2525
let recorder: Recorder;
2626
let client: SmsClient;
2727

sdk/communication/communication-sms/test/public/smsClient.mocked.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { isNode } from "@azure/core-util";
5-
import { HttpClient } from "@azure/core-rest-pipeline";
64
import { AzureKeyCredential } from "@azure/core-auth";
5+
import { HttpClient } from "@azure/core-rest-pipeline";
6+
import { isNode } from "@azure/core-util";
7+
import { TokenCredential } from "@azure/identity";
78
import { assert } from "chai";
89
import sinon from "sinon";
910
import { SmsClient, SmsClientOptions, SmsSendRequest } from "../../src";
1011
import { MockHttpClient } from "./utils/mockHttpClient";
11-
import { TokenCredential } from "@azure/identity";
1212

1313
const TEST_NUMBER = "+14255550123";
1414

15-
describe("[mocked] SmsClient", async () => {
15+
describe("[mocked] SmsClient", async function () {
1616
const baseUri = "https://contoso.api.fake";
1717
const connectionString = `endpoint=${baseUri};accesskey=banana`;
1818
const dateHeader = "x-ms-date";
@@ -25,16 +25,16 @@ describe("[mocked] SmsClient", async () => {
2525
message: "message",
2626
};
2727

28-
describe("when instantiating SMS client", () => {
29-
it("can instantiate with a connection string", async () => {
28+
describe("when instantiating SMS client", function () {
29+
it("can instantiate with a connection string", async function () {
3030
new SmsClient(connectionString);
3131
});
3232

33-
it("can instantiate with a url and KeyCredential ", async () => {
33+
it("can instantiate with a url and KeyCredential ", async function () {
3434
new SmsClient(baseUri, new AzureKeyCredential("banana"));
3535
});
3636

37-
it("can instantiate with a token", async () => {
37+
it("can instantiate with a token", async function () {
3838
const fakeToken: TokenCredential = {
3939
getToken: async (_scopes) => {
4040
return { token: "testToken", expiresOnTimestamp: 11111 };
@@ -44,9 +44,9 @@ describe("[mocked] SmsClient", async () => {
4444
});
4545
});
4646

47-
describe("when sending an SMS", () => {
47+
describe("when sending an SMS", function () {
4848
let smsClient: SmsClient;
49-
beforeEach(() => {
49+
beforeEach(function () {
5050
sendRequestSpy = sinon.spy(mockHttpClient, "sendRequest");
5151
sinon.useFakeTimers();
5252
// workaround: casting because min testing has issues with httpClient newer versions having extra optional fields
@@ -55,7 +55,7 @@ describe("[mocked] SmsClient", async () => {
5555
} as SmsClientOptions);
5656
});
5757

58-
it("sends with the correct headers", async () => {
58+
it("sends with the correct headers", async function () {
5959
await smsClient.send(testSendRequest);
6060

6161
const request = sendRequestSpy.getCall(0).args[0];
@@ -70,7 +70,7 @@ describe("[mocked] SmsClient", async () => {
7070
);
7171
});
7272

73-
it("returns the correct results", async () => {
73+
it("returns the correct results", async function () {
7474
const smsTestResults = await smsClient.send(testSendRequest);
7575

7676
const smsTestResult = smsTestResults[0];
@@ -79,7 +79,7 @@ describe("[mocked] SmsClient", async () => {
7979
assert.equal(smsTestResult.messageId, "id");
8080
});
8181

82-
afterEach(() => {
82+
afterEach(function () {
8383
sinon.restore();
8484
});
8585
});

sdk/communication/communication-sms/test/public/smsClient.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
* They are duplicated in an internal test which contains workaround logic to record/playback the tests
77
*/
88

9-
import { matrix } from "@azure/test-utils";
109
import { Recorder, env, isPlaybackMode } from "@azure-tools/test-recorder";
11-
import { createRecordedSmsClient, createRecordedSmsClientWithToken } from "./utils/recordedClient";
10+
import { matrix } from "@azure/test-utils";
1211
import { Context } from "mocha";
13-
import sendSmsSuites from "./suites/smsClient.send";
14-
import { SmsClient } from "../../src";
1512
import sinon from "sinon";
13+
import { SmsClient } from "../../src";
1614
import { Uuid } from "../../src/utils/uuid";
15+
import sendSmsSuites from "./suites/smsClient.send";
16+
import { createRecordedSmsClient, createRecordedSmsClientWithToken } from "./utils/recordedClient";
1717

1818
matrix([[true, false]], async function (useAad: boolean) {
19-
describe(`SmsClient [Live]${useAad ? " [AAD]" : ""}`, async () => {
19+
describe(`SmsClient [Live]${useAad ? " [AAD]" : ""}`, async function () {
2020
let recorder: Recorder;
2121
let client: SmsClient;
2222

0 commit comments

Comments
 (0)