Skip to content

Instantly share code, notes, and snippets.

@CodeVachon
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save CodeVachon/019d5eb1c31f1866399f to your computer and use it in GitHub Desktop.

Select an option

Save CodeVachon/019d5eb1c31f1866399f to your computer and use it in GitHub Desktop.

Revisions

  1. Christopher Vachon revised this gist Jun 30, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    var request = require('supertest'),
    app = require('./../app'),
    myModule = require('./../myModule'),
    app = require('./app'),
    myModule = require('./myModule'),
    defaultString = "It Works",
    testString = "This is my Test String"
    ;
  2. Christopher Vachon created this gist Jun 30, 2015.
    23 changes: 23 additions & 0 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    var express = require('express'),
    app = express(),
    port = process.env.PORT || 3030,
    myModule = require('./myModule')
    ;

    app.use(express.static(__dirname + '/public'));

    app.get('/', function(request, response, next) {
    myModule("Hello World", function(error, result) {
    if (error) {
    response.status(400).json(error);
    } else {
    response.json(result);
    }
    });
    });

    app.listen(port, function(){
    console.log('Listening on port ' + port);
    });

    module.exports = app;
    23 changes: 23 additions & 0 deletions myModule.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    module.exports = function(options, callback) {
    if (!options) {
    options = {
    text: "It Works"
    };
    }
    setTimeout(function() {
    try {
    var thisString = options.text || options;
    if (typeof thisString == "string") {
    callback(undefined, thisString);
    } else {
    callback(new Error("value is not a string"), undefined);
    }
    } catch (error) {
    callback(error, undefined);
    }
    }, getRandomInt(50,800));
    }

    function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
    }
    72 changes: 72 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    var request = require('supertest'),
    app = require('./../app'),
    myModule = require('./../myModule'),
    defaultString = "It Works",
    testString = "This is my Test String"
    ;

    describe("the Server", function() {
    it("starts", function(done) {
    request(app)
    .get("/")
    .expect(200)
    .end(done)
    ;
    });
    });

    describe("middleware", function() {
    it("returns the default string", function(done) {
    myModule(null, function callback(error, result) {
    if (error) {
    throw error;
    }
    if (result != defaultString) {
    throw new Error("Expected String to Be ["+defaultString+"], Got ["+result+"]");
    }
    done();
    });
    });

    it("returns the test string", function(done) {
    myModule(testString, function callback(error, result) {
    if (error) {
    throw error;
    }
    if (result != testString) {
    throw new Error("Expected String to Be ["+testString+"], Got ["+result+"]");
    }
    done();
    });
    });

    it("returns the test string in dot notation", function(done) {
    myModule({text: testString}, function callback(error, result) {
    if (error) {
    throw error;
    }
    if (result != testString) {
    throw new Error("Expected String to Be ["+testString+"], Got ["+result+"]");
    }
    done();
    });
    });

    it ("errors when an array is passed", function(done) {
    myModule([1,2,3,4,5], function callback(error, result) {
    if (!error) {
    throw new Error("Expect an error");
    }
    done();
    });
    });

    it ("errors when an array is passed in dot notation", function(done) {
    myModule({text: [1,2,3,4,5]}, function callback(error, result) {
    if (!error) {
    throw new Error("Expect an error");
    }
    done();
    });
    });
    });