Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

start adding tests for connect

  • Loading branch information...
commit 3f6a96b42c7767e7453c56ac76207579ae190e73 1 parent 941a745
Gilles Ruppert authored August 20, 2012
52  lib/webmake-middleware.js
... ...
@@ -1,4 +1,56 @@
  1
+var webmake = require('webmake');
  2
+
  3
+/**
  4
+ * configuration function
  5
+ * Takes either:
  6
+ * - 2 strings, 1 being the requested url and the other being
  7
+ *   the path to the entry file, i.e. main.js
  8
+ * or
  9
+ * - 1 object where the key is the url and the value is entry file. This is
  10
+ *   useful if you have different urls and entry points
  11
+ *
  12
+ * @param {string|object} url
  13
+ * @param {string} [file]
  14
+ * @return {function} middleware
  15
+ */
1 16
 module.exports = function(url, file) {
  17
+  var l = arguments.length;
  18
+  var config;
  19
+  // configuration must be either 2 strings or 1 object
  20
+  if (!(l === 2 && typeof url === 'string' && typeof file === 'string' ||
  21
+        l === 1 && typeof url === 'object') )  throw new TypeError('wrong arguments passed');
  22
+
  23
+  if (l === 2) {
  24
+    config = {};
  25
+    config[url] = file;
  26
+  }
  27
+  else if (l === 1) config = url;
  28
+
2 29
   return function webmakeMiddleware(req, res, next) {
  30
+    if (! (req.url in config)) {
  31
+      next();
  32
+      return;
  33
+    }
  34
+
  35
+    webmake(config[req.url], { sourceMap: true }, function(err, src) {
  36
+      if (err) {
  37
+        // fail loudly on the cli and in the browser
  38
+        console.error(err);
  39
+        res.writeHead(500);
  40
+        res.end('document.write(\'<div style="position:absolute;' +
  41
+                'top:10px;width:80%;color:red;font-size:150%;background-color:white;' +
  42
+                'border:5px solid black;padding:10px;font:sans-serif;">' +
  43
+                err.message.replace(/'/g, '\\\'') +
  44
+               '</div>\')');
  45
+        return;
  46
+      }
  47
+
  48
+      res.writeHead(200, {
  49
+        'Content-Type': 'text/javascript; charset=utf-8',
  50
+        'Cache-Control': 'no-cache'
  51
+      });
  52
+
  53
+      res.end(src);
  54
+    });
3 55
   };
4 56
 };
13  package.json
@@ -4,17 +4,24 @@
4 4
   "description": "connect and express middleware for webmake",
5 5
   "main": "lib/webmake-middleware.js",
6 6
   "scripts": {
7  
-    "test": "./node_modules/.bin/tap ./test"
  7
+    "test": "./node_modules/.bin/mocha"
8 8
   },
9 9
   "keywords": [
10 10
     "webmake",
11 11
     "middleware",
12 12
     "connect",
13  
-    "express"
  13
+    "express",
  14
+    "browser"
14 15
   ],
15 16
   "author": "Gilles Ruppert",
16 17
   "license": "MIT",
17 18
   "devDependencies": {
18  
-    "tap": "~0.3.0"
  19
+    "connect": "~2.4.3",
  20
+    "express": "~3.0.0rc3",
  21
+    "mocha": "~1.3.2",
  22
+    "expect.js": "~0.1.2"
  23
+  },
  24
+  "dependencies": {
  25
+    "webmake": "~0.3.11"
19 26
   }
20 27
 }
31  test/configuration-test.js
... ...
@@ -0,0 +1,31 @@
  1
+var expect = require('expect.js');
  2
+var webmakeMw = require('../lib/webmake-middleware');
  3
+
  4
+describe('webmake middleware', function() {
  5
+  describe('configuration function', function() {
  6
+    it('should be a function which takes to arguments', function() {
  7
+      expect(webmakeMw).to.be.a('function');
  8
+      expect(webmakeMw).to.have.length(2);
  9
+    });
  10
+
  11
+    it('should return a middelware function which takes 3 arguments', function() {
  12
+      expect(webmakeMw({})).to.be.a('function');
  13
+      expect(webmakeMw({})).to.have.length(3);
  14
+    });
  15
+
  16
+    it('should take 2 strings as arguments', function() {
  17
+      function err1() { webmakeMw() }
  18
+      function err2() { webmakeMw(1,2,3) }
  19
+      function err3() { webmakeMw(1,2) }
  20
+
  21
+      expect(webmakeMw('foo', 'bar')).to.be.a('function');
  22
+      expect(err1).to.throwError();
  23
+      expect(err2).to.throwError();
  24
+      expect(err3).to.throwError();
  25
+    });
  26
+
  27
+    it('should take 1 object as argument', function() {
  28
+      expect(webmakeMw({})).to.be.a('function');
  29
+    });
  30
+  });
  31
+});
58  test/connect-test.js
... ...
@@ -0,0 +1,58 @@
  1
+var expect = require('expect.js');
  2
+var connect = require('connect');
  3
+var http = require('http');
  4
+var path = require('path');
  5
+var webmakeMw = require('../lib/webmake-middleware');
  6
+
  7
+describe('connect middleware', function() {
  8
+
  9
+  it('should return the compiled file when requesting a valid url ', function(done) {
  10
+    var app = connect()
  11
+    .use(webmakeMw('/test.js', path.normalize(__dirname + '/fixtures/main.js')))
  12
+    .listen(3000);
  13
+
  14
+    http.get('http://localhost:3000/test.js', function(res) {
  15
+      res.setEncoding('utf8');
  16
+      expect(res.statusCode).to.equal(200);
  17
+      expect(res.headers['content-type']).to.be('text/javascript; charset=utf-8');
  18
+      expect(res.headers['cache-control']).to.be('no-cache');
  19
+
  20
+      res.on('data', function(chunk) {
  21
+        expect(chunk).to.be.a('string');
  22
+        expect(chunk).to.not.be.empty();
  23
+      });
  24
+
  25
+      res.on('close', function(err) {
  26
+        expect(false).to.be(true);
  27
+        done();
  28
+      });
  29
+
  30
+      res.on('end', function() {
  31
+        app.close();
  32
+        done();
  33
+      });
  34
+    });
  35
+  });
  36
+
  37
+  it('should return a 500 response when webmake has a problem with compiling the script', function(done) {
  38
+    this.timeout(0);
  39
+    var app = connect()
  40
+    .use(webmakeMw('/test2.js', path.normalize(__dirname + '/fixtures/err.js')))
  41
+    .listen(3000);
  42
+
  43
+    http.get('http://localhost:3000/test2.js', function(res) {
  44
+      res.setEncoding('utf8');
  45
+      expect(res.statusCode).to.be(500);
  46
+
  47
+      res.on('data', function(chunk) {
  48
+        expect(chunk).to.be.a('string');
  49
+        expect(chunk).to.contain('Cannot require');
  50
+      })
  51
+      res.on('end', done);
  52
+      app.close();
  53
+    });
  54
+  });
  55
+
  56
+  it('should be configurable with an object');
  57
+});
  58
+
1  test/fixtures/err.js
... ...
@@ -0,0 +1 @@
  1
+require('fs');
2  test/fixtures/main.js
... ...
@@ -0,0 +1,2 @@
  1
+var test1 = require('./test1');
  2
+
1  test/fixtures/test1.js
... ...
@@ -0,0 +1 @@
  1
+exports.test1 = 'test1';
14  test/webmake-middleware-test.js
... ...
@@ -1,14 +0,0 @@
1  
-var test = require('tap').test;
2  
-var webmakeMw = require('../lib/webmake-middleware');
3  
-
4  
-test('configuration function', function(t) {
5  
-  t.equal(typeof webmakeMw, 'function', 'configuration function should be a function');
6  
-  t.equal(webmakeMw.length, 2, 'configuration function should take 2 arguments');
7  
-  t.end();
8  
-});
9  
-
10  
-test('configuration function return value', function(t) {
11  
-  t.equal(typeof webmakeMw(), 'function', 'configuration function should return the middleware');
12  
-  t.equal(webmakeMw().length, 3, 'the middleware should take 3 arguments');
13  
-  t.end();
14  
-});

0 notes on commit 3f6a96b

Please sign in to comment.
Something went wrong with that request. Please try again.