Skip to content

Instantly share code, notes, and snippets.

@mrkvon
Last active September 26, 2016 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkvon/6caadea482f4e624f96a3b824d97ac69 to your computer and use it in GitHub Desktop.
Save mrkvon/6caadea482f4e624f96a3b824d97ac69 to your computer and use it in GitHub Desktop.
example of asynchronous tests with should
// assertion passes - test passes
it('[first reply] should give tag with key `position` and value `first_reply`', function (done) {
messageToInfluxService.process(message2to1, function (err, fields, tags) {
if (err) return done(err);
try {
tags.should.have.property('position', 'first_reply');
return done();
} catch (e) {
return done(e);
}
});
});
// assertion passes - test passes (without try catch)
it('[first reply] should give tag with key `position` and value `first_reply`', function (done) {
messageToInfluxService.process(message2to1, function (err, fields, tags) {
if (err) return done(err);
tags.should.have.property('position', 'first_reply');
return done();
});
});
// assertion fails - test fails with a proper error
it('[first reply] should give tag with key `position` and value `first_reply`', function (done) {
messageToInfluxService.process(message2to1, function (err, fields, tags) {
if (err) return done(err);
try {
tags.should.have.property('position', 'first_replay'); // note the different assertion
return done();
} catch (e) {
return done(e);
}
});
});
// missing try - catch
// (!) assertion fails - test fails with UnhandledPromiseRejectionWarning printed in console and with timeout
it('[first reply] should give tag with key `position` and value `first_reply`', function (done) {
messageToInfluxService.process(message2to1, function (err, fields, tags) {
if (err) return done(err);
tags.should.have.property('position', 'first_replay');
return done();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment