Skip to content

Commit

Permalink
test: Use Jest (#981)
Browse files Browse the repository at this point in the history
  • Loading branch information
gillesdemey authored and dead-horse committed May 11, 2017
1 parent 13c7ca6 commit d394724
Show file tree
Hide file tree
Showing 67 changed files with 749 additions and 856 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ before_script:
- "[ ! -f wrk/bin/wrk ] && rm -rf wrk && git clone https://github.com/wg/wrk.git && make -C wrk && mkdir wrk/bin && mv wrk/wrk wrk/bin || true"
- export PATH=$PATH:$PWD/wrk/bin/
script:
- make lint
- make test-travis
- make bench
- npm run lint
- npm run test-cov
- npm run bench
after_script:
- npm install codecov
- ./node_modules/.bin/codecov
42 changes: 0 additions & 42 deletions Makefile

This file was deleted.

25 changes: 18 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"description": "Koa web app framework",
"main": "lib/application.js",
"scripts": {
"test": "make test"
"test": "jest --forceExit",
"test-cov": "npm run test -- --coverage",
"lint": "eslint benchmarks lib test --fix",
"bench": "make -C benchmarks"
},
"repository": "koajs/koa",
"keywords": [
Expand Down Expand Up @@ -50,16 +53,24 @@
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^2.1.1",
"istanbul": "^0.4.0",
"mocha": "^3.2.0",
"should": "^6.0.3",
"should-http": "0.0.3",
"supertest": "^3.0.0",
"test-console": "^0.7.1"
"jest": "^20.0.0",
"supertest": "^3.0.0"
},
"engines": {
"node": ">= 6.0.0"
},
"files": [
"lib"
]
],
"jest": {
"testMatch": [
"**/test/!(helpers)/*.js"
],
"coverageReporters": [
"text-summary",
"lcov"
],
"bail": true,
"testEnvironment": "node"
}
}
2 changes: 1 addition & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
env:
mocha: true
jest: true

rules:
space-before-blocks: [2, {functions: never, keywords: always}]
Expand Down
12 changes: 6 additions & 6 deletions test/application/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ describe('app.context', () => {
app1.context.msg = 'hello';
const app2 = new Koa();

it('should merge properties', done => {
it('should merge properties', () => {
app1.use((ctx, next) => {
assert.equal(ctx.msg, 'hello');
ctx.status = 204;
});

request(app1.listen())
return request(app1.listen())
.get('/')
.expect(204, done);
.expect(204);
});

it('should not affect the original prototype', done => {
it('should not affect the original prototype', () => {
app2.use((ctx, next) => {
assert.equal(ctx.msg, undefined);
ctx.status = 204;
});

request(app2.listen())
return request(app2.listen())
.get('/')
.expect(204, done);
.expect(204);
});
});
2 changes: 1 addition & 1 deletion test/application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('app', () => {
});

app.on('error', err => {
err.message.should.equal('boom');
assert.equal(err.message, 'boom');
done();
});

Expand Down
52 changes: 27 additions & 25 deletions test/application/onerror.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,71 @@

'use strict';

const stderr = require('test-console').stderr;
const assert = require('assert');
const Koa = require('../..');
const AssertionError = require('assert').AssertionError;

describe('app.onerror(err)', () => {
it('should throw an error if a non-error is given', done => {
const app = new Koa();
beforeEach(() => {
global.console = jest.genMockFromModule('console');
});

(() => app.onerror('foo')).should.throw(AssertionError, {message: 'non-error thrown: foo'});
afterEach(() => {
global.console = require('console');
});

it('should throw an error if a non-error is given', () => {
const app = new Koa();

done();
assert.throws(() => {
app.onerror('foo');
}, AssertionError, 'non-error thrown: foo');
});

it('should do nothing if status is 404', done => {
it('should do nothing if status is 404', () => {
const app = new Koa();
const err = new Error();

err.status = 404;

const output = stderr.inspectSync(() => app.onerror(err));
app.onerror(err);

output.should.eql([]);

done();
assert.deepEqual(console.error.mock.calls, []);
});

it('should do nothing if .silent', done => {
it('should do nothing if .silent', () => {
const app = new Koa();
app.silent = true;
const err = new Error();

const output = stderr.inspectSync(() => app.onerror(err));

output.should.eql([]);
app.onerror(err);

done();
assert.deepEqual(console.error.mock.calls, []);
});

it('should log the error to stderr', done => {
it('should log the error to stderr', () => {
const app = new Koa();
app.env = 'dev';

const err = new Error();
err.stack = 'Foo';

const output = stderr.inspectSync(() => app.onerror(err));
app.onerror(err);

output.should.eql(['\n', ' Foo\n', '\n']);

done();
const stderr = console.error.mock.calls.join('\n');
assert.deepEqual(stderr, '\n Foo\n');
});

it('should use err.toString() instad of err.stack', done => {
it('should use err.toString() instad of err.stack', () => {
const app = new Koa();
app.env = 'dev';

const err = new Error('mock stack null');
err.stack = null;

const output = stderr.inspectSync(() => app.onerror(err));

output.should.eql(['\n', ' Error: mock stack null\n', '\n']);
app.onerror(err);

done();
const stderr = console.error.mock.calls.join('\n');
assert.equal(stderr, '\n Error: mock stack null\n');
});
});
12 changes: 6 additions & 6 deletions test/application/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ describe('app.request', () => {
app1.request.message = 'hello';
const app2 = new Koa();

it('should merge properties', done => {
it('should merge properties', () => {
app1.use((ctx, next) => {
assert.equal(ctx.request.message, 'hello');
ctx.status = 204;
});

request(app1.listen())
return request(app1.listen())
.get('/')
.expect(204, done);
.expect(204);
});

it('should not affect the original prototype', done => {
it('should not affect the original prototype', () => {
app2.use((ctx, next) => {
assert.equal(ctx.request.message, undefined);
ctx.status = 204;
});

request(app2.listen())
return request(app2.listen())
.get('/')
.expect(204, done);
.expect(204);
});
});
Loading

0 comments on commit d394724

Please sign in to comment.