Skip to content

Commit e6051a3

Browse files
committed
Merge pull request #2 from stereobooster/mocha-express-chai
Mocha express chai
2 parents 80e8570 + 2a21e8d commit e6051a3

File tree

10 files changed

+138
-22
lines changed

10 files changed

+138
-22
lines changed

mocha-express-chai/Readme.md

+41-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
1-
# Usage
1+
# Express + Mocha & Co
2+
3+
**Note**: `Grunt` is optional you can do all with `mocha` only
4+
5+
## Usage
26

37
```
4-
npm i
58
npm test
69
```
710

8-
# Travis
11+
or
12+
13+
```
14+
mocha
15+
```
16+
17+
or
18+
19+
```
20+
grunt test
21+
```
22+
23+
## Travis Badge
924

1025
[![Build Status](https://secure.travis-ci.org/<GITHUB_USER>/<REPO_NAME>.png)](http://travis-ci.org/<GITHUB_USER>/<REPO_NAME>)
26+
27+
## Autotest
28+
29+
```
30+
mocha -w
31+
```
32+
or
33+
34+
```
35+
grunt watch
36+
```
37+
38+
## Coverage
39+
40+
```
41+
mocha -R html-cov > coverage.html
42+
```
43+
44+
## Test Documentation
45+
46+
```
47+
mocha -R doc > doc.html
48+
```

mocha-express-chai/app.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
var express = require("express");
2-
1+
var express = require('express');
2+
3+
var index = require('./app/controllers/index');
4+
35
var app = express();
46

57
// Config
6-
app.configure('development', function() {
8+
app.configure(function() {
79
app.set('port', 8000);
10+
app.set('views', __dirname + '/app/views');
11+
app.set('view engine', 'jade');
812
});
913

1014
app.configure('development', function() {
@@ -18,9 +22,7 @@ app.configure('test', function () {
1822
app.configure('production', function() {
1923
});
2024

21-
app.get('/', function(req, res, next) {
22-
res.send('Hello world!');
23-
})
25+
index.init(app);
2426

2527
module.exports = app;
2628

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
exports.init = function(app) {
2+
app.get('/', function (req, res, next) {
3+
res.render('index');
4+
});
5+
6+
app.get('/hello', function (req, res, next) {
7+
res.send('Hello world!');
8+
});
9+
10+
app.get('/form', function (req, res, next) {
11+
res.render('index');
12+
});
13+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
doctype 5
2+
html
3+
head
4+
title= 'Hello world'
5+
body
6+
form(method='GET',action='/form')
7+
input(type='text',name='email')
8+
input(type='submit',name='Save')

mocha-express-chai/grunt.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@ module.exports = function(grunt) {
22

33
// Add our custom tasks.
44
grunt.loadNpmTasks('grunt-mocha-test');
5+
grunt.loadNpmTasks('grunt-exec');
56

67
// Project configuration.
78
grunt.initConfig({
9+
exec: {
10+
coverage: {
11+
command: 'node_modules/.bin/mocha -R html-cov > coverage.html',
12+
}
13+
},
814
mochaTest: {
9-
files: ['spec/**/*.spec.js']
15+
files: ['test/**/*.test.js']
1016
},
1117
mochaTestConfig: {
1218
options: {
1319
reporter: 'dot',
14-
require: 'spec/common'
20+
require: 'test/common'
1521
}
1622
},
1723
watch: {
1824
test: {
19-
files: ['spec/**/*.spec.js'],
25+
files: ['test/**/*.js', 'app/**/*.js'],
2026
tasks: 'test'
2127
}
2228
}
2329
});
24-
30+
31+
grunt.registerTask('coverage', 'exec:coverage');
2532
grunt.registerTask('test', 'mochaTest');
2633
grunt.registerTask('default', 'test');
2734
}

mocha-express-chai/package.json

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@
44
"private": true,
55
"scripts": {
66
"start": "node app.js",
7-
"test": "node_modules/.bin/grunt test"
7+
"test": "node_modules/.bin/mocha",
8+
"blanket": {
9+
"pattern": "/app/"
10+
}
811
},
912
"author": "TODO",
1013
"main": "./app.js",
1114
"engines": {
1215
"node": ">=0.8.x"
1316
},
1417
"dependencies": {
15-
"express": "3.x"
18+
"express": "3.x",
19+
"jade": "*"
1620
},
1721
"devDependencies": {
1822
"grunt": "*",
1923
"grunt-mocha-test": "*",
24+
"grunt-exec": "*",
2025
"chai": "*",
2126
"mocha": "*",
2227
"zombie": "*",
23-
"chai-supertest": "*"
28+
"phantom": "*",
29+
"chai-supertest": "*",
30+
"blanket": ">= 1.0.3"
2431
}
2532
}

mocha-express-chai/spec/common.js mocha-express-chai/test/common.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ chai.use(chaiSupertest.httpAsserts);
1919
// global.sinon = require("sinon");
2020
// var sinonChai = require("sinon-chai");
2121
// chai.use(sinonChai);
22+
23+
// use zombie.js as headless browser
24+
global.Browser = require('zombie');
25+
26+
// get the phantom
27+
global.phantom = require('phantom')
2228

2329
// force the test environment to 'test'
2430
process.env.NODE_ENV = 'test';
2531

32+
// test coverage
33+
require('blanket');
34+
2635
// get the application server module
2736
global.app = require('./../app');
2837

2938
// get the super-agent
3039
global.user = request(app).agent();
31-
32-
// use zombie.js as headless browser
33-
global.Browser = require('zombie');
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
describe("phantom suite", function() {
2+
3+
before(function(done) {
4+
this.server = app.listen(3000);
5+
var self = this;
6+
phantom.create(function(ph){
7+
self.ph = ph;
8+
ph.createPage(function(page){
9+
self.page = page;
10+
done();
11+
})
12+
})
13+
});
14+
15+
after(function(done){
16+
this.ph.exit();
17+
this.server.close(done);
18+
});
19+
20+
it("testing with the help of phantom", function(done) {
21+
this.page.open("http://localhost:3000", function(status){
22+
status.should.be.eql("success");
23+
done();
24+
})
25+
});
26+
});

mocha-express-chai/spec/supertest.spec.js mocha-express-chai/test/supertest.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
describe("supertest suite", function() {
22
it("testing with the help of super-agen", function(done) {
33
user
4-
.get('/')
4+
.get('/hello')
55
.end(function (res) {
66
res.should.be.html;
77
res.should.have.status(200);

mocha-express-chai/spec/zombie.spec.js mocha-express-chai/test/zombie.test.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ describe("zombie suite", function() {
1919
this.server.close(done);
2020
});
2121

22-
it("testing with the help of zombie", function() {
23-
this.browser.success.should.be.true;
22+
it("testing with the help of zombie", function(done) {
23+
var browser = this.browser;
24+
browser.success.should.be.true;
25+
browser.text("title").should.eql('Hello world');
26+
browser
27+
.fill("email", "[email protected]")
28+
.pressButton("Save", function() {
29+
// Form submitted, new page loaded.
30+
browser.success.should.be.true;
31+
done();
32+
})
2433
});
2534
});

0 commit comments

Comments
 (0)