Skip to content

Instantly share code, notes, and snippets.

Avatar

Marcus Hammarberg marcusoftnet

View GitHub Profile
View writingMocha2.js
describe("Users", function (){
describe("Validation", function (){
it("validates an email address");
it("validates the presence of a name");
it("validates the password");
// and more
});
describe("Storage", function (){
it("stores an user");
it("retrieves an user by id");
@marcusoftnet
marcusoftnet / should2.js
Last active Aug 29, 2015
should to check for null
View should2.js
var should = require("should");
var myObject = null;
// DON’T
// this will fail with a "TypeError: Cannot read property 'should' of null" error
myObject.should.exsist();
// DO
// this is the proper way of writing an assertion for null
@marcusoftnet
marcusoftnet / should3.js
Created Feb 2, 2014
forgetting require...
View should3.js
var name = "marcus";
name.should.equal("marcus");
@marcusoftnet
marcusoftnet / callbackex1.js
Last active Aug 29, 2015
Example callback function
View callbackex1.js
function doSomething(p, callback){
// does something using the p parameter
// maybe call some other service
// that takes a long time to complete
// when that is finally completed.
// it calls back
// by just calling the callback function
callback();
}
@marcusoftnet
marcusoftnet / writingCallbacks.js
Created Feb 2, 2014
Writing callbacks without loosing your mind
View writingCallbacks.js
// First I write the call without parameters
doSomething();
// I then adds the parameters
doSomething(123);
// and the function is just another param...
doSomething(123, function(){});
// I then create two new lines between the curlies
@marcusoftnet
marcusoftnet / mochawriting1.js
Last active Aug 29, 2015
How I write mocha
View mochawriting1.js
// first just write the top level describe function
describe('Users');
// now add an empty function to the describe function
describe('Users', function (){});
// add two line breaks and fill the function out
describe('Users', function (){
describe('Validation');
describe('Storage');
@marcusoftnet
marcusoftnet / package.json
Last active Aug 29, 2015
demo package.json
View package.json
{
"name": "Noun.js",
"description": "This is a REST API against the noun part of Merriam-Webster dictionary",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js",
"test" : "./node_modules/mocha/bin/mocha -u bdd -R spec",
"startLocal" : "nodemon -e html app.js"
},
@marcusoftnet
marcusoftnet / doneMocha.js
Last active Aug 29, 2015
the done parameter in mocha
View doneMocha.js
var should = require("should");
describe("Configuration setup", function () {
it("loads local configuration default", function (done) {
var config = require("../config")();
config.mode.should.equal("local");
done(); // tell mocha that we're done and it can process next test
});
});
@marcusoftnet
marcusoftnet / should1.js
Last active Aug 29, 2015
should is nice to write almost english with
View should1.js
var should = require("should");
var name = "marcus";
name.should.equal("marcus");
// You could go to town and write almost-english...
user.age.should.be.within(5, 50);
(5).should.be.within(5, 10).and.within(5, 5);
(99.99).should.be.approximately(100, 0.1);
@marcusoftnet
marcusoftnet / example.package.json
Last active Aug 29, 2015
Example scripts node of package.json
View example.package.json
"scripts": {
"start": "node app.js",
"start_stage": "node app.js 4000",
"test" : "mocha -u bdd -R spec",
"test_watch" : "mocha -u bdd -R spec -w"
}