Skip to content

Commit 10f7a1a

Browse files
test: fix flaky concurrency test with deferred runIframe mock
1 parent ef2ae8a commit 10f7a1a

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

__tests__/Auth0Client/getTokenSilently.test.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,16 +1076,36 @@ describe('Auth0Client', () => {
10761076
it('should make its own network request when cacheMode is off and a cacheMode on call is in flight', async () => {
10771077
const client = setup();
10781078

1079-
jest.spyOn(<any>utils, 'runIframe').mockResolvedValue({
1079+
const runIframeResponse = {
10801080
access_token: TEST_ACCESS_TOKEN,
10811081
state: TEST_STATE,
10821082
code: TEST_CODE
1083+
};
1084+
1085+
// Block the first (cacheMode:'on') call at runIframe so it is
1086+
// definitively in-flight — and hasn't stored anything to cache yet —
1087+
// when the second (cacheMode:'off') call starts.
1088+
let unfreezeFirstCall: (() => void) | undefined;
1089+
const firstCallAtIframe = new Promise<void>(resolve => {
1090+
jest.spyOn(<any>utils, 'runIframe')
1091+
.mockImplementationOnce(
1092+
() => new Promise(res => {
1093+
resolve(); // signal: first call is now blocked at runIframe
1094+
unfreezeFirstCall = () => res(runIframeResponse);
1095+
})
1096+
)
1097+
.mockResolvedValue(runIframeResponse);
10831098
});
10841099

1085-
await Promise.all([
1086-
getTokenSilently(client),
1087-
getTokenSilently(client, { cacheMode: 'off' })
1088-
]);
1100+
const onCallPromise = getTokenSilently(client);
1101+
1102+
// Wait until cacheMode:'on' is blocked at runIframe before launching
1103+
// cacheMode:'off', guaranteeing it is in-flight with no cached result yet.
1104+
await firstCallAtIframe;
1105+
const offCallPromise = getTokenSilently(client, { cacheMode: 'off' });
1106+
1107+
unfreezeFirstCall?.();
1108+
await Promise.all([onCallPromise, offCallPromise]);
10891109

10901110
expect(utils.runIframe).toHaveBeenCalledTimes(2);
10911111
});

0 commit comments

Comments
 (0)