Skip to content

Instantly share code, notes, and snippets.

View marcusoftnet's full-sized avatar

Marcus Hammarberg marcusoftnet

View GitHub Profile
@marcusoftnet
marcusoftnet / package.json
Last active August 29, 2015 13:55
demo 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 / example.package.json
Last active August 29, 2015 13:55
Example scripts node of 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"
}
@marcusoftnet
marcusoftnet / callbackex1.js
Last active August 29, 2015 13:55
Example callback function
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 February 2, 2014 09:09
Writing callbacks without loosing your mind
// 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 / doneMocha.js
Last active August 29, 2015 13:55
the done parameter in mocha
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 / mochawriting1.js
Last active August 29, 2015 13:55
How I write mocha
// 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 / writingMocha2.js
Created February 2, 2014 12:15
Filling out the it's
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 / should1.js
Last active August 29, 2015 13:55
should is nice to write almost english with
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 / should2.js
Last active August 29, 2015 13:55
should to check for null
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 February 2, 2014 13:08
forgetting require...
var name = "marcus";
name.should.equal("marcus");