Skip to content

Commit 4ef11f0

Browse files
committed
refactor transports test to use async/await instead of done()
1 parent 98d9a3d commit 4ef11f0

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

packages/browser/test/unit/transports/new-xhr.test.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ function createXHRMock() {
3333
},
3434
};
3535

36-
//@ts-ignore because TS thinks window doesn't have XMLHttpRequest
37-
jest.spyOn(window, 'XMLHttpRequest').mockImplementation(() => xhrMock as XMLHttpRequest);
36+
// casting `window` as `any` because XMLHttpRequest is missing in Window (TS-only)
37+
jest.spyOn(window as any, 'XMLHttpRequest').mockImplementation(() => xhrMock as XMLHttpRequest);
3838

3939
return xhrMock;
4040
}
@@ -50,23 +50,22 @@ describe('NewXHRTransport', () => {
5050
jest.restoreAllMocks();
5151
});
5252

53-
it('makes an XHR request to the given URL', done => {
53+
it('makes an XHR request to the given URL', async () => {
5454
const transport = makeNewXHRTransport(DEFAULT_XHR_TRANSPORT_OPTIONS);
5555
expect(xhrMock.open).toHaveBeenCalledTimes(0);
5656
expect(xhrMock.setRequestHeader).toHaveBeenCalledTimes(0);
5757
expect(xhrMock.send).toHaveBeenCalledTimes(0);
5858

59-
Promise.all([transport.send(ERROR_ENVELOPE), (xhrMock as XMLHttpRequest).onreadystatechange(null)]).then(
60-
([res]) => {
61-
expect(xhrMock.open).toHaveBeenCalledTimes(1);
62-
expect(xhrMock.open).toHaveBeenCalledWith('POST', DEFAULT_XHR_TRANSPORT_OPTIONS.url);
63-
expect(xhrMock.send).toBeCalledWith(serializeEnvelope(ERROR_ENVELOPE));
59+
const [res] = await Promise.all([
60+
transport.send(ERROR_ENVELOPE),
61+
(xhrMock as XMLHttpRequest).onreadystatechange(null),
62+
]);
6463

65-
expect(res).toBeDefined();
66-
expect(res.status).toEqual('success');
64+
expect(xhrMock.open).toHaveBeenCalledTimes(1);
65+
expect(xhrMock.open).toHaveBeenCalledWith('POST', DEFAULT_XHR_TRANSPORT_OPTIONS.url);
66+
expect(xhrMock.send).toBeCalledWith(serializeEnvelope(ERROR_ENVELOPE));
6767

68-
done();
69-
},
70-
);
68+
expect(res).toBeDefined();
69+
expect(res.status).toEqual('success');
7170
});
7271
});

0 commit comments

Comments
 (0)