From 2c86b10feafd868ebd071dda3a222e6f51972b5d Mon Sep 17 00:00:00 2001 From: Yiyu He Date: Tue, 30 Jul 2019 18:00:43 +0800 Subject: [PATCH] test: remove jest and use egg-bin(mocha) (#1363) --- package.json | 20 +++++--------------- test/application/onerror.js | 35 +++++++++++++++++++---------------- test/application/respond.js | 8 -------- test/context/onerror.js | 8 -------- 4 files changed, 24 insertions(+), 47 deletions(-) diff --git a/package.json b/package.json index a9db1e2..d3d3fa4 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "description": "Koa web app framework", "main": "lib/application.js", "scripts": { - "test": "jest", - "test-cov": "jest --coverage --runInBand --forceExit", + "test": "egg-bin test test", + "test-cov": "egg-bin cov test", "lint": "eslint benchmarks lib test", "bench": "make -C benchmarks", "authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS" @@ -31,6 +31,7 @@ "delegates": "^1.0.0", "depd": "^1.1.2", "destroy": "^1.0.4", + "egg-bin": "^4.13.0", "error-inject": "^1.0.0", "escape-html": "^1.0.3", "fresh": "~0.5.2", @@ -40,6 +41,7 @@ "koa-compose": "^4.1.0", "koa-convert": "^1.2.0", "koa-is-json": "^1.0.0", + "mm": "^2.5.0", "on-finished": "^2.3.0", "only": "~0.0.2", "parseurl": "^1.3.2", @@ -53,7 +55,6 @@ "eslint-config-standard": "^7.0.1", "eslint-plugin-promise": "^3.5.0", "eslint-plugin-standard": "^2.1.1", - "jest": "^20.0.0", "supertest": "^3.1.0" }, "engines": { @@ -61,16 +62,5 @@ }, "files": [ "lib" - ], - "jest": { - "testMatch": [ - "**/test/!(helpers)/*.js" - ], - "coverageReporters": [ - "text-summary", - "lcov" - ], - "bail": true, - "testEnvironment": "node" - } + ] } diff --git a/test/application/onerror.js b/test/application/onerror.js index ee3d396..487360b 100644 --- a/test/application/onerror.js +++ b/test/application/onerror.js @@ -3,15 +3,10 @@ const assert = require('assert'); const Koa = require('../..'); +const mm = require('mm'); describe('app.onerror(err)', () => { - beforeEach(() => { - global.console = jest.genMockFromModule('console'); - }); - - afterEach(() => { - global.console = require('console'); - }); + afterEach(mm.restore); it('should throw an error if a non-error is given', () => { const app = new Koa(); @@ -27,9 +22,10 @@ describe('app.onerror(err)', () => { err.status = 404; + let called = false; + mm(console, 'error', () => { called = true; }); app.onerror(err); - - assert.deepEqual(console.error.mock.calls, []); + assert(!called); }); it('should do nothing if .silent', () => { @@ -37,9 +33,10 @@ describe('app.onerror(err)', () => { app.silent = true; const err = new Error(); + let called = false; + mm(console, 'error', () => { called = true; }); app.onerror(err); - - assert.deepEqual(console.error.mock.calls, []); + assert(!called); }); it('should log the error to stderr', () => { @@ -49,10 +46,12 @@ describe('app.onerror(err)', () => { const err = new Error(); err.stack = 'Foo'; + let msg = ''; + mm(console, 'error', input => { + if (input) msg = input; + }); app.onerror(err); - - const stderr = console.error.mock.calls.join('\n'); - assert.deepEqual(stderr, '\n Foo\n'); + assert(msg === ' Foo'); }); it('should use err.toString() instad of err.stack', () => { @@ -64,7 +63,11 @@ describe('app.onerror(err)', () => { app.onerror(err); - const stderr = console.error.mock.calls.join('\n'); - assert.equal(stderr, '\n Error: mock stack null\n'); + let msg = ''; + mm(console, 'error', input => { + if (input) msg = input; + }); + app.onerror(err); + assert(msg === ' Error: mock stack null'); }); }); diff --git a/test/application/respond.js b/test/application/respond.js index 2834649..71d5c51 100644 --- a/test/application/respond.js +++ b/test/application/respond.js @@ -8,14 +8,6 @@ const Koa = require('../..'); const fs = require('fs'); describe('app.respond', () => { - beforeEach(() => { - global.console = jest.genMockFromModule('console'); - }); - - afterEach(() => { - global.console = require('console'); - }); - describe('when ctx.respond === false', () => { it('should function (ctx)', () => { const app = new Koa(); diff --git a/test/context/onerror.js b/test/context/onerror.js index 89dd51e..e4b8e6c 100644 --- a/test/context/onerror.js +++ b/test/context/onerror.js @@ -7,14 +7,6 @@ const Koa = require('../..'); const context = require('../helpers/context'); describe('ctx.onerror(err)', () => { - beforeEach(() => { - global.console = jest.genMockFromModule('console'); - }); - - afterEach(() => { - global.console = require('console'); - }); - it('should respond', () => { const app = new Koa();