Skip to content

Commit 209a709

Browse files
Fixed some tests that were occasionally failing in IE
1 parent f16e07c commit 209a709

1 file changed

Lines changed: 77 additions & 80 deletions

File tree

Lines changed: 77 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict';
22

33
describe('Callback & Promise syntax', function() {
4-
// NOTE: This delay is so high because TravisCI and SauceLabs,
5-
// both of which are VERY SLOW
6-
var delay = global.__karma__ ? 2000 : 200;
4+
// We need a delay to allow the callback functions to be called asynchronously.
5+
var delayMS = 200;
6+
7+
function delay(fn) {
8+
setTimeout(fn, delayMS);
9+
}
710

811
beforeEach(function() {
9-
// These tests all have a delay, to ensure that all callbacks and promises are called.
10-
// So we need to increase the test timeouts
11-
this.currentTest.timeout(delay * 2);
12-
this.currentTest.slow(delay * 2 + 50);
12+
this.currentTest.slow(delayMS * 2);
1313
});
1414

1515
['parse', 'resolve', 'dereference', 'bundle', 'validate'].forEach(function(method) {
@@ -32,35 +32,34 @@ describe('Callback & Promise syntax', function() {
3232

3333
SwaggerParser[method](path.rel('specs/callbacks-promises/callbacks-promises.yaml'), callbackFn)
3434
.then(thenFn)
35-
.catch(catchFn);
36-
37-
setTimeout(function() {
38-
try {
39-
// Make sure the correct functions were called
40-
sinon.assert.calledOnce(callbackFn);
41-
sinon.assert.calledOnce(thenFn);
42-
sinon.assert.notCalled(catchFn);
43-
44-
// Make sure they were called with the same value
45-
var thenFnArg = thenFn.firstCall.args[0];
46-
var callbackFnArg = callbackFn.firstCall.args[1];
47-
expect(thenFnArg).to.equal(callbackFnArg);
48-
sinon.assert.calledWithExactly(callbackFn, null, thenFnArg);
49-
sinon.assert.calledWithExactly(thenFn, callbackFnArg);
50-
51-
// Make sure the API was parsed correctly
52-
if (method !== 'resolve') {
53-
var actual = thenFn.firstCall.args[0];
54-
var expected = method === 'validate' ? helper.dereferenced.callbacksPromises : helper[method + 'd'].callbacksPromises;
55-
expect(actual).to.deep.equal(expected);
35+
.catch(catchFn)
36+
.then(delay(function() {
37+
try {
38+
// Make sure the correct functions were called
39+
sinon.assert.calledOnce(callbackFn);
40+
sinon.assert.calledOnce(thenFn);
41+
sinon.assert.notCalled(catchFn);
42+
43+
// Make sure they were called with the same value
44+
var thenFnArg = thenFn.firstCall.args[0];
45+
var callbackFnArg = callbackFn.firstCall.args[1];
46+
expect(thenFnArg).to.equal(callbackFnArg);
47+
sinon.assert.calledWithExactly(callbackFn, null, thenFnArg);
48+
sinon.assert.calledWithExactly(thenFn, callbackFnArg);
49+
50+
// Make sure the API was parsed correctly
51+
if (method !== 'resolve') {
52+
var actual = thenFn.firstCall.args[0];
53+
var expected = method === 'validate' ? helper.dereferenced.callbacksPromises : helper[method + 'd'].callbacksPromises;
54+
expect(actual).to.deep.equal(expected);
55+
}
56+
57+
done();
5658
}
57-
58-
done();
59-
}
60-
catch (e) {
61-
done(e)
62-
}
63-
}, delay);
59+
catch (e) {
60+
done(e);
61+
}
62+
}));
6463
}
6564
}
6665

@@ -77,29 +76,28 @@ describe('Callback & Promise syntax', function() {
7776
var parser = new SwaggerParser();
7877
parser[method](path.rel('specs/callbacks-promises/callbacks-promises.yaml'), callbackFn)
7978
.then(thenFn)
80-
.catch(catchFn);
81-
82-
setTimeout(function() {
83-
try {
84-
// Make sure the correct functions were called
85-
sinon.assert.calledOnce(callbackFn);
86-
sinon.assert.calledOnce(thenFn);
87-
sinon.assert.notCalled(catchFn);
88-
89-
// Make sure they were called with the correct values
90-
var result = method === 'resolve' ? parser.$refs : parser.api;
91-
var api = method === 'resolve' ? helper.parsed.callbacksPromises : method === 'validate' ? helper.dereferenced.callbacksPromises : helper[method + 'd'].callbacksPromises;
92-
expect(parser.api).to.deep.equal(api);
93-
expect(parser.$refs.paths()).to.deep.equal([path.abs('specs/callbacks-promises/callbacks-promises.yaml')]);
94-
sinon.assert.calledWithExactly(callbackFn, null, result);
95-
sinon.assert.calledWithExactly(thenFn, result);
96-
97-
done();
98-
}
99-
catch (e) {
100-
done(e)
101-
}
102-
}, delay);
79+
.catch(catchFn)
80+
.then(delay(function() {
81+
try {
82+
// Make sure the correct functions were called
83+
sinon.assert.calledOnce(callbackFn);
84+
sinon.assert.calledOnce(thenFn);
85+
sinon.assert.notCalled(catchFn);
86+
87+
// Make sure they were called with the correct values
88+
var result = method === 'resolve' ? parser.$refs : parser.api;
89+
var api = method === 'resolve' ? helper.parsed.callbacksPromises : method === 'validate' ? helper.dereferenced.callbacksPromises : helper[method + 'd'].callbacksPromises;
90+
expect(parser.api).to.deep.equal(api);
91+
expect(parser.$refs.paths()).to.deep.equal([path.abs('specs/callbacks-promises/callbacks-promises.yaml')]);
92+
sinon.assert.calledWithExactly(callbackFn, null, result);
93+
sinon.assert.calledWithExactly(thenFn, result);
94+
95+
done();
96+
}
97+
catch (e) {
98+
done(e);
99+
}
100+
}));
103101
}
104102
}
105103

@@ -116,28 +114,27 @@ describe('Callback & Promise syntax', function() {
116114
var parser = new SwaggerParser();
117115
parser[method](path.rel('specs/callbacks-promises/callbacks-promises-error.yaml'), callbackFn)
118116
.then(thenFn)
119-
.catch(catchFn);
120-
121-
setTimeout(function() {
122-
try {
123-
// Make sure the correct functions were called
124-
sinon.assert.calledOnce(callbackFn);
125-
sinon.assert.calledOnce(catchFn);
126-
sinon.assert.notCalled(thenFn);
127-
128-
// Make sure they were called with the correct values
129-
expect(parser.api).to.deep.equal({swagger: 'ERROR'});
130-
expect(parser.$refs.paths()).to.deep.equal([path.abs('specs/callbacks-promises/callbacks-promises-error.yaml')]);
131-
var result = method === 'resolve' ? parser.$refs : sinon.match(parser.api);
132-
sinon.assert.calledWithExactly(callbackFn, sinon.match.instanceOf(SyntaxError), result);
133-
sinon.assert.calledWithExactly(catchFn, sinon.match.instanceOf(SyntaxError));
134-
135-
done();
136-
}
137-
catch (e) {
138-
done(e)
139-
}
140-
}, delay);
117+
.catch(catchFn)
118+
.then(delay(function() {
119+
try {
120+
// Make sure the correct functions were called
121+
sinon.assert.calledOnce(callbackFn);
122+
sinon.assert.calledOnce(catchFn);
123+
sinon.assert.notCalled(thenFn);
124+
125+
// Make sure they were called with the correct values
126+
expect(parser.api).to.deep.equal({swagger: 'ERROR'});
127+
expect(parser.$refs.paths()).to.deep.equal([path.abs('specs/callbacks-promises/callbacks-promises-error.yaml')]);
128+
var result = method === 'resolve' ? parser.$refs : sinon.match(parser.api);
129+
sinon.assert.calledWithExactly(callbackFn, sinon.match.instanceOf(SyntaxError), result);
130+
sinon.assert.calledWithExactly(catchFn, sinon.match.instanceOf(SyntaxError));
131+
132+
done();
133+
}
134+
catch (e) {
135+
done(e);
136+
}
137+
}));
141138
}
142139
}
143140
});

0 commit comments

Comments
 (0)