Skip to content

Instantly share code, notes, and snippets.

@debonx
Last active January 26, 2020 10:05
Show Gist options
  • Save debonx/d3b87f0d16865eb5712456ad1c074422 to your computer and use it in GitHub Desktop.
Save debonx/d3b87f0d16865eb5712456ad1c074422 to your computer and use it in GitHub Desktop.
Node, Mocha: Simple test suite for Rooster object with Mocha and Node asserts methods. (https://mochajs.org, https://nodejs.org/api/assert.html).
// Define a rooster
Rooster = {};
// Return a morning rooster call
Rooster.announceDawn = () => {
return 'moo!';
}
// Return hour as string
// Throws Error if hour is not between 0 and 23 inclusive
Rooster.timeAtDawn = (hour) => {
if (hour < 0 || hour > 23) {
throw new RangeError;
} else {
return hour.toString();
};
}
module.exports = Rooster;
/**
* More https://mochajs.org/#examples
*/
const assert = require('assert');
const Rooster = require('./rooster.js');
describe('string', () => {
describe('string', () => {
it('returns a rooster call', () => {
//Setup
const expected = 'blablabla';
//exercise
const actual = Rooster.announceDawn();
//verify
assert.strictEqual(typeof actual, typeof expected);
});
it('returns its argument as a string', () => {
//setup
const expected = 'blablabla';
//exercise
const actual = Rooster.timeAtDawn(14);
//verify
assert.strictEqual(typeof actual, typeof expected);
})
it('throws an error if passed a number less than 0', () => {
//setup & exercise
const hour = -1;
//verify
assert.throws(
() => {
Rooster.timeAtDawn(hour);
},
RangeError
);
});
it('throws an error if passed a number greater than 23', () => {
//setup & exercise
const hour = 24;
//verify
assert.throws(
() => {
Rooster.timeAtDawn(hour);
},
RangeError
);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment