Skip to content

Commit ac37aa5

Browse files
Refactored all the unit tests to use Mocha's Promises support (I didn't know it existed until today)
1 parent 0464605 commit ac37aa5

File tree

19 files changed

+275
-487
lines changed

19 files changed

+275
-487
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"build": "npm run lint && npm run browserify",
2525
"browserify": "simplifyify lib/index.js --outfile dist/ref-parser.js --standalone \\$RefParser --debug --minify",
2626
"watch": "npm run browserify -- --watch",
27-
"mocha": "mocha --bail --recursive tests/fixtures tests/specs",
27+
"mocha": "mocha --bail --async-only tests/fixtures tests/specs",
2828
"istanbul": "istanbul cover _mocha --dir coverage/node -- --bail --recursive tests/fixtures tests/specs",
2929
"karma": "karma start --single-run",
3030
"test": "npm run browserify -- --test && npm run istanbul && npm run karma",

tests/fixtures/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Configure Mocha
66
mocha.setup('bdd');
77
mocha.fullTrace();
8+
mocha.asyncOnly();
89
mocha.checkLeaks();
910
mocha.globals(['$0', '$1', '$2', '$3', '$4', '$5']);
1011
}

tests/fixtures/helper.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,10 @@
1919
helper.bundled = {};
2020

2121
/**
22-
* Returns a function that throws an error if called.
23-
*
24-
* @param {function} done
22+
* Throws an error if called.
2523
*/
26-
helper.shouldNotGetCalled = function shouldNotGetCalled(done) {
27-
return function shouldNotGetCalledFN(err) {
28-
if (!(err instanceof Error)) {
29-
err = new Error('This function should not have gotten called.');
30-
}
31-
done(err);
32-
};
24+
helper.shouldNotGetCalled = function shouldNotGetCalled() {
25+
throw new Error('This function should not have gotten called.');
3326
};
3427

3528
/**
@@ -80,7 +73,7 @@
8073

8174
done();
8275
})
83-
.catch(helper.shouldNotGetCalled(done));
76+
.catch(helper.shouldNotGetCalled);
8477
}
8578
};
8679

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,42 @@
11
'use strict';
22

33
describe('File names with special characters', function() {
4-
it('should parse successfully', function(done) {
4+
it('should parse successfully', function() {
55
var parser = new $RefParser();
6-
parser
6+
return parser
77
.parse(path.rel('specs/__({[ ! % & $ # @ ` ~ ,)}]__/__({[ ! % & $ # @ ` ~ ,)}]__.yaml'))
88
.then(function(schema) {
99
expect(schema).to.equal(parser.schema);
1010
expect(schema).to.deep.equal(helper.parsed.specialCharacters.schema);
1111
expect(parser.$refs.paths()).to.deep.equal([path.abs('specs/__({[ ! % & $ # @ ` ~ ,)}]__/__({[ ! % & $ # @ ` ~ ,)}]__.yaml')]);
12-
done();
13-
})
14-
.catch(helper.shouldNotGetCalled(done));
12+
});
1513
});
1614

1715
it('should resolve successfully', helper.testResolve(
1816
'specs/__({[ ! % & $ # @ ` ~ ,)}]__/__({[ ! % & $ # @ ` ~ ,)}]__.yaml', helper.parsed.specialCharacters.schema,
1917
'specs/__({[ ! % & $ # @ ` ~ ,)}]__/__({[ ! % & $ # @ ` ~ ,)}]__/__({[ ! % & $ # @ ` ~ ,)}]__.json', helper.parsed.specialCharacters.file
2018
));
2119

22-
it('should dereference successfully', function(done) {
20+
it('should dereference successfully', function() {
2321
var parser = new $RefParser();
24-
parser
22+
return parser
2523
.dereference(path.rel('specs/__({[ ! % & $ # @ ` ~ ,)}]__/__({[ ! % & $ # @ ` ~ ,)}]__.yaml'))
2624
.then(function(schema) {
2725
expect(schema).to.equal(parser.schema);
2826
expect(schema).to.deep.equal(helper.dereferenced.specialCharacters);
2927

3028
// The "circular" flag should NOT be set
3129
expect(parser.$refs.circular).to.equal(false);
32-
33-
done();
34-
})
35-
.catch(helper.shouldNotGetCalled(done));
30+
});
3631
});
3732

38-
it('should bundle successfully', function(done) {
33+
it('should bundle successfully', function() {
3934
var parser = new $RefParser();
40-
parser
35+
return parser
4136
.bundle(path.rel('specs/__({[ ! % & $ # @ ` ~ ,)}]__/__({[ ! % & $ # @ ` ~ ,)}]__.yaml'))
4237
.then(function(schema) {
4338
expect(schema).to.equal(parser.schema);
4439
expect(schema).to.deep.equal(helper.dereferenced.specialCharacters);
45-
done();
46-
})
47-
.catch(helper.shouldNotGetCalled(done));
40+
});
4841
});
4942
});

tests/specs/blank/blank.spec.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,34 @@ describe('Blank files', function() {
1818

1919
it('should throw an error if parsed as YAML', function(done) {
2020
testDone = done;
21-
$RefParser
21+
return $RefParser
2222
.parse(path.rel('specs/blank/blank.yaml'))
23-
.then(helper.shouldNotGetCalled(done))
23+
.then(helper.shouldNotGetCalled)
2424
.catch(function(err) {
2525
expect(err).to.be.an.instanceOf(SyntaxError);
2626
expect(err.message).to.contain('blank/blank.yaml" is not a valid JSON Schema');
2727
done();
28-
})
29-
.catch(helper.shouldNotGetCalled(done));
28+
});
3029
});
3130

3231
it('should throw an error if parsed as JSON', function(done) {
3332
testDone = done;
34-
$RefParser
33+
return $RefParser
3534
.parse(path.rel('specs/blank/blank.yaml'), {allow: {yaml: false}})
36-
.then(helper.shouldNotGetCalled(done))
35+
.then(helper.shouldNotGetCalled)
3736
.catch(function(err) {
3837
expect(err).to.be.an.instanceOf(SyntaxError);
3938
expect(err.message).to.contain('Error parsing "');
4039
expect(err.message).to.contain('blank/blank.yaml"');
4140
done();
42-
})
43-
.catch(helper.shouldNotGetCalled(done));
41+
});
4442
});
4543

4644
it('should throw an error if "options.allow.empty" is disabled', function(done) {
4745
testDone = done;
48-
$RefParser
46+
return $RefParser
4947
.parse(path.rel('specs/blank/blank.yaml'), {allow: {empty: false}})
50-
.then(helper.shouldNotGetCalled(done))
48+
.then(helper.shouldNotGetCalled)
5149
.catch(function(err) {
5250
if (userAgent.isNode) {
5351
expect(err).to.be.an.instanceOf(SyntaxError);
@@ -60,7 +58,6 @@ describe('Blank files', function() {
6058
expect(err.message).to.contain('HTTP 204: No Content');
6159
}
6260
done();
63-
})
64-
.catch(helper.shouldNotGetCalled(done));
61+
});
6562
});
6663
});

tests/specs/callbacks.spec.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Callback & Promise syntax', function() {
2727
done();
2828
}
2929
catch (e) {
30-
done(e)
30+
done(e);
3131
}
3232
});
3333
}
@@ -66,12 +66,11 @@ describe('Callback & Promise syntax', function() {
6666
}
6767

6868
function testPromiseError(method) {
69-
return function(done) {
70-
$RefParser[method](path.rel('specs/invalid/invalid.yaml'))
71-
.then(helper.shouldNotGetCalled(done))
69+
return function() {
70+
return $RefParser[method](path.rel('specs/invalid/invalid.yaml'))
71+
.then(helper.shouldNotGetCalled)
7272
.catch(function(err) {
7373
expect(err).to.be.an.instanceOf(SyntaxError);
74-
done();
7574
});
7675
}
7776
}

0 commit comments

Comments
 (0)