Skip to content

Commit 2c86b10

Browse files
authored
test: remove jest and use egg-bin(mocha) (#1363)
1 parent 219bf22 commit 2c86b10

File tree

4 files changed

+24
-47
lines changed

4 files changed

+24
-47
lines changed

package.json

+5-15
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"description": "Koa web app framework",
55
"main": "lib/application.js",
66
"scripts": {
7-
"test": "jest",
8-
"test-cov": "jest --coverage --runInBand --forceExit",
7+
"test": "egg-bin test test",
8+
"test-cov": "egg-bin cov test",
99
"lint": "eslint benchmarks lib test",
1010
"bench": "make -C benchmarks",
1111
"authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS"
@@ -31,6 +31,7 @@
3131
"delegates": "^1.0.0",
3232
"depd": "^1.1.2",
3333
"destroy": "^1.0.4",
34+
"egg-bin": "^4.13.0",
3435
"error-inject": "^1.0.0",
3536
"escape-html": "^1.0.3",
3637
"fresh": "~0.5.2",
@@ -40,6 +41,7 @@
4041
"koa-compose": "^4.1.0",
4142
"koa-convert": "^1.2.0",
4243
"koa-is-json": "^1.0.0",
44+
"mm": "^2.5.0",
4345
"on-finished": "^2.3.0",
4446
"only": "~0.0.2",
4547
"parseurl": "^1.3.2",
@@ -53,24 +55,12 @@
5355
"eslint-config-standard": "^7.0.1",
5456
"eslint-plugin-promise": "^3.5.0",
5557
"eslint-plugin-standard": "^2.1.1",
56-
"jest": "^20.0.0",
5758
"supertest": "^3.1.0"
5859
},
5960
"engines": {
6061
"node": "^4.8.4 || ^6.10.1 || ^7.10.1 || >= 8.1.4"
6162
},
6263
"files": [
6364
"lib"
64-
],
65-
"jest": {
66-
"testMatch": [
67-
"**/test/!(helpers)/*.js"
68-
],
69-
"coverageReporters": [
70-
"text-summary",
71-
"lcov"
72-
],
73-
"bail": true,
74-
"testEnvironment": "node"
75-
}
65+
]
7666
}

test/application/onerror.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33

44
const assert = require('assert');
55
const Koa = require('../..');
6+
const mm = require('mm');
67

78
describe('app.onerror(err)', () => {
8-
beforeEach(() => {
9-
global.console = jest.genMockFromModule('console');
10-
});
11-
12-
afterEach(() => {
13-
global.console = require('console');
14-
});
9+
afterEach(mm.restore);
1510

1611
it('should throw an error if a non-error is given', () => {
1712
const app = new Koa();
@@ -27,19 +22,21 @@ describe('app.onerror(err)', () => {
2722

2823
err.status = 404;
2924

25+
let called = false;
26+
mm(console, 'error', () => { called = true; });
3027
app.onerror(err);
31-
32-
assert.deepEqual(console.error.mock.calls, []);
28+
assert(!called);
3329
});
3430

3531
it('should do nothing if .silent', () => {
3632
const app = new Koa();
3733
app.silent = true;
3834
const err = new Error();
3935

36+
let called = false;
37+
mm(console, 'error', () => { called = true; });
4038
app.onerror(err);
41-
42-
assert.deepEqual(console.error.mock.calls, []);
39+
assert(!called);
4340
});
4441

4542
it('should log the error to stderr', () => {
@@ -49,10 +46,12 @@ describe('app.onerror(err)', () => {
4946
const err = new Error();
5047
err.stack = 'Foo';
5148

49+
let msg = '';
50+
mm(console, 'error', input => {
51+
if (input) msg = input;
52+
});
5253
app.onerror(err);
53-
54-
const stderr = console.error.mock.calls.join('\n');
55-
assert.deepEqual(stderr, '\n Foo\n');
54+
assert(msg === ' Foo');
5655
});
5756

5857
it('should use err.toString() instad of err.stack', () => {
@@ -64,7 +63,11 @@ describe('app.onerror(err)', () => {
6463

6564
app.onerror(err);
6665

67-
const stderr = console.error.mock.calls.join('\n');
68-
assert.equal(stderr, '\n Error: mock stack null\n');
66+
let msg = '';
67+
mm(console, 'error', input => {
68+
if (input) msg = input;
69+
});
70+
app.onerror(err);
71+
assert(msg === ' Error: mock stack null');
6972
});
7073
});

test/application/respond.js

-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ const Koa = require('../..');
88
const fs = require('fs');
99

1010
describe('app.respond', () => {
11-
beforeEach(() => {
12-
global.console = jest.genMockFromModule('console');
13-
});
14-
15-
afterEach(() => {
16-
global.console = require('console');
17-
});
18-
1911
describe('when ctx.respond === false', () => {
2012
it('should function (ctx)', () => {
2113
const app = new Koa();

test/context/onerror.js

-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ const Koa = require('../..');
77
const context = require('../helpers/context');
88

99
describe('ctx.onerror(err)', () => {
10-
beforeEach(() => {
11-
global.console = jest.genMockFromModule('console');
12-
});
13-
14-
afterEach(() => {
15-
global.console = require('console');
16-
});
17-
1810
it('should respond', () => {
1911
const app = new Koa();
2012

0 commit comments

Comments
 (0)