Skip to content

Commit

Permalink
Added initial tooling to support test for routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dozoisch committed Feb 20, 2017
1 parent 5923922 commit 1bedb0a
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Expand Up @@ -43,7 +43,7 @@ module.exports = {

"import/no-extraneous-dependencies": ["error", {
"devDependencies": [
"**/test/*.js",
"**/test/**/*.js",
"**/scripts/*.js",
"**/webpack.config.js",
]
Expand Down
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -68,7 +68,8 @@
"eslint-plugin-lodash": "^2.3.5",
"mocha": "3.0.2",
"nodemon": "^1.11.0",
"pre-commit": "1.1.3"
"pre-commit": "1.1.3",
"supertest": "^3.0.0"
},
"engines": {
"node": ">=6.0.0",
Expand All @@ -82,7 +83,8 @@
"build-dev": "webpack --watch",
"build": "NODE_ENV=production webpack",
"test": "npm run mocha && npm run lint",
"mocha": "mocha",
"mocha": "NODE_ENV=test mocha",
"test-watch": "NODE_ENV=test mocha --watch --reporter spec",
"postinstall": "npm run build"
},
"pre-commit": [
Expand Down
5 changes: 5 additions & 0 deletions test/.eslintrc.js
@@ -0,0 +1,5 @@
module.exports = {
env: {
mocha: true,
},
};
5 changes: 2 additions & 3 deletions test/bson.js → test/lib/bsonSpec.js
@@ -1,11 +1,10 @@
'use strict';

/*globals describe, it*/

var bson = require('../lib/bson');
var expect = require('chai').expect;
var mongodb = require('mongodb');

var bson = require('../../lib/bson');

describe('BSON', function () {
describe('toBSON', function () {
it('should convert documents with native JS types', function () {
Expand Down
4 changes: 1 addition & 3 deletions test/csv.js → test/lib/csvSpec.js
@@ -1,10 +1,8 @@
'use strict';

/*globals describe, it*/

var expect = require('chai').expect;
var mongo = require('mongodb');
var csv = require('../lib/csv');
var csv = require('../../lib/csv');

var test = function (data, result) {
expect(csv(data)).to.eql(result);
Expand Down
36 changes: 36 additions & 0 deletions test/lib/routers/indexSpec.js
@@ -0,0 +1,36 @@
'use strict';

const expect = require('chai').expect;

const httpUtils = require('../../testHttpUtils');
const testMongoUtils = require('../../testMongoUtils');
const asPromise = require('../../testUtils').asPromise;

describe('Routers index', () => {
let request;
let close;
let db;
before(() => {
const server = httpUtils.createServer();
request = server.request;
close = server.close;
return testMongoUtils.initializeDb()
.then((newDb) => {
db = newDb;
});
});

it('GET / should return html', () =>
asPromise(cb => request.get('/').expect(200).end(cb))
.then((res) => {
expect(res.text).to.match(/<h4 style="font-weight: bold;">Databases<\/h4>/);
expect(res.text).to.match(new RegExp('<a href="/db/mongo-express-test-db/">mongo-express-test-db</a></h3>'));
})
);

after(() => Promise.all([
testMongoUtils.cleanAndCloseDb(db),
close(),
]));
});

1 change: 1 addition & 0 deletions test/mocha.opts
@@ -1 +1,2 @@
--recursive
--reporter dot
43 changes: 43 additions & 0 deletions test/testDefaultConfig.js
@@ -0,0 +1,43 @@
'use strict';

const mongoConfig = require('./testMongoConfig');

module.exports = () => ({
mongodb: {
server: mongoConfig.host,
port: mongoConfig.port,

autoReconnect: true,
poolSize: 4,
admin: true,
auth: [],

adminUsername: '',
adminPassword: '',
whitelist: [mongoConfig.dbName],
blacklist: [],
},

site: {
host: 'localhost',
port: 3000,
cookieSecret: 'cookiesecret',
sessionSecret: 'sessionsecret',
cookieKeyName: 'mongo-express',
sslEnabled: false,
sslCert: '',
sslKey: '',
},

useBasicAuth: false,

options: {
documentsPerPage: 10,
editorTheme: 'rubyblue',

logger: { skip: () => true },
readOnly: false,
},

defaultKeyNames: {},
});
14 changes: 14 additions & 0 deletions test/testHttpUtils.js
@@ -0,0 +1,14 @@
'use strict';

const supertest = require('supertest');

const defaultConf = require('./testDefaultConfig');
const asPromise = require('./testUtils').asPromise;
const middleware = require('../lib/middleware');

exports.createServer = () => {
const app = middleware(defaultConf());
const httpServer = app.listen();
const request = supertest.agent(httpServer);
return { request, close: () => asPromise(cb => httpServer.close(cb)) };
};
11 changes: 11 additions & 0 deletions test/testMongoConfig.js
@@ -0,0 +1,11 @@
'use strict';

module.exports = {
host: 'localhost',
port: 27017,
dbName: 'mongo-express-test-db',
makeConnectionUrl: () => {
const m = module.exports;
return `mongodb://${m.host}:${m.port}/${m.dbName}`;
},
};
34 changes: 34 additions & 0 deletions test/testMongoUtils.js
@@ -0,0 +1,34 @@
'use strict';

const MongoClient = require('mongodb').MongoClient;

const mongoConfig = require('./testMongoConfig');
const asPromise = require('./testUtils').asPromise;

exports.testData = [
{ testItem: 1 },
{ testItem: 2 },
{ testItem: 3 },
{ testItem: 4 },
];
exports.testCollectionName = 'test/items';

exports.createConnection = () =>
asPromise(cb => MongoClient.connect(mongoConfig.makeConnectionUrl(), cb));

exports.createTestCollection = db =>
asPromise(cb => db.collection(exports.testCollectionName).insertMany(exports.testData, cb));

exports.dropTestCollection = db =>
asPromise(cb => db.collection(exports.testCollectionName).drop(cb));

exports.closeDb = db =>
asPromise(cb => db.close(cb));

exports.initializeDb = () =>
exports.createConnection().then(db =>
exports.createTestCollection(db).then(() => db)
);

exports.cleanAndCloseDb = db =>
exports.dropTestCollection(db).then(() => exports.closeDb(db));
10 changes: 10 additions & 0 deletions test/testUtils.js
@@ -0,0 +1,10 @@
'use strict';

exports.asPromise = function asPromise(fct) {
return new Promise((resolve, reject) =>
fct((err, result) => {
if (err) reject(err);
else resolve(result);
})
);
};

0 comments on commit 1bedb0a

Please sign in to comment.