Skip to content

Instantly share code, notes, and snippets.

@elyasha
Created April 2, 2022 14:33
Show Gist options
  • Save elyasha/29602f23eb485b0ba65e1e4729dced39 to your computer and use it in GitHub Desktop.
Save elyasha/29602f23eb485b0ba65e1e4729dced39 to your computer and use it in GitHub Desktop.
LEARN JAVASCRIPT UNIT TESTING Rooster Regulation
// Define a rooster
Rooster = {};
// Return a morning rooster call
Rooster.announceDawn = () => {
return 'cock-a-doodle-doo!';
}
// 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;
const assert = require('assert')
const Rooster = require("../index")
describe("Rooster",() => {
describe("announceDawn", () => {
it("returns a rooster call", () => {
// Define expected output
const expected = 'cock-a-doodle-doo!';
// Call Rooster.announceDawn and store result in variable
const result = Rooster.announceDawn()
// Use an assert method to compare actual and expected result
assert.strictEqual(result, expected)
})
})
describe("timeAtDawn", () => {
it("returns its argument as a string", () => {
const expected = '16'
const result = Rooster.timeAtDawn(16)
assert.strictEqual(result, expected)
})
it("throws an error if passed a number less than 0", () => {
assert.throws(() => {Rooster.timeAtDawn(-1)}, RangeError)
})
it("throws an error if passed a number greater than 23", () => {
assert.throws(() => {Rooster.timeAtDawn(24)}, RangeError)
})
})
})
{
"name": "learn-mocha-intro-start",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha test/**/*_test.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"mocha": "^3.5.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment