Skip to content

Commit edb8a21

Browse files
compwrightJonathon Hill
authored andcommitted
fix: Fix timing bug in interceptor tests
1 parent 614dd09 commit edb8a21

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "OAuth 2.0 client utils for axios",
55
"main": "src/",
66
"scripts": {
7-
"test": "mocha tests/"
7+
"test": "mocha tests/ --timeout 10000"
88
},
99
"author": "Jonathon Hill <jhill9693@gmail.com>",
1010
"license": "MIT",

tests/interceptor.test.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ describe('interceptor', function() {
2323
});
2424
});
2525

26-
it('should cache the oauth access token until it expires', function(done) {
26+
it('should cache the oauth access token until it expires', async function() {
27+
function delay(ms) {
28+
return new Promise((resolve, reject) => setTimeout(resolve, ms));
29+
}
30+
2731
let calls = 0;
2832
const authenticate = async() => {
2933
return {
3034
access_token: 'foo' + calls++,
31-
expires_in: 1
35+
expires_in: 2
3236
}
3337
};
3438

@@ -43,27 +47,23 @@ describe('interceptor', function() {
4347
])
4448
}
4549

46-
function delay(ms) {
47-
return new Promise((resolve, reject) => setTimeout(resolve, ms));
48-
}
49-
5050
function verify(results, expected) {
5151
for (let result of results) {
5252
assert.deepEqual(result, expected);
5353
}
5454
}
5555

56-
test3x(req)
57-
.then(results => verify(results, {
58-
headers: { Authorization: 'Bearer foo0' }
59-
}))
60-
.then(() => delay(1500))
61-
.then(() => test3x(req))
62-
.then(results => verify(results, {
63-
headers: { Authorization: 'Bearer foo1' }
64-
}))
65-
.then(() => done())
66-
.catch(done);
56+
let results = await test3x(req);
57+
verify(results, {
58+
headers: { Authorization: 'Bearer foo0' }
59+
});
60+
61+
await delay(3000);
62+
63+
results = await test3x(req);
64+
verify(results, {
65+
headers: { Authorization: 'Bearer foo1' }
66+
});
6767
});
6868
});
6969
});

0 commit comments

Comments
 (0)