Egg: 单元测试的问题

Created on 9 Apr 2017  ·  17Comments  ·  Source: eggjs/egg

运行 npm test 的时候报以下错误,是什么回事?

PS D:\node\mox-api> npm test

[email protected] test D:\node\mox-api
npm run lint -- --fix && npm run test-local

[email protected] lint D:\node\mox-api
eslint . "--fix"

EPERM: operation not permitted, scandir 'D:\node\mox-api\logs\mox-api\common-error.log'
Error: EPERM: operation not permitted, scandir 'D:\node\mox-api\logs\mox-api\common-error.log'
at Object.fs.readdirSync (fs.js:913:18)
at GlobSync._readdir (D:\node\mox-api\node_modules.7.1.1@glob\sync.js:288:41)
at GlobSync._readdir (D:\node\mox-api\node_modules.3.19.0@eslint\lib\util\glob.js:59:36)
at GlobSync._readdirInGlobStar (D:\node\mox-api\node_modules.7.1.1@glob\sync.js:267:20)
at GlobSync._readdir (D:\node\mox-api\node_modules.7.1.1@glob\sync.js:276:17)
at GlobSync._readdir (D:\node\mox-api\node_modules.3.19.0@eslint\lib\util\glob.js:59:36)
at GlobSync._processReaddir (D:\node\mox-api\node_modules.7.1.1@glob\sync.js:137:22)
at GlobSync._process (D:\node\mox-api\node_modules.7.1.1@glob\sync.js:132:10)
at GlobSync._processGlobStar (D:\node\mox-api\node_modules.7.1.1@glob\sync.js:380:10)
at GlobSync._process (D:\node\mox-api\node_modules.7.1.1@glob\sync.js:130:10)

npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "lint" "--" "--fix"
npm ERR! node v7.7.1
npm ERR! npm v4.1.2
npm ERR! code ELIFECYCLE
npm ERR! [email protected] lint: eslint . "--fix"
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] lint script 'eslint . "--fix"'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the mox-api package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! eslint . "--fix"
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs mox-api
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls mox-api
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR! D:\node\mox-api\npm-debug.log
npm ERR! Test failed. See above for more details.
PS D:\node\mox-api>

Most helpful comment

如果server占用着log文件,执行单元测试出错往common-error文件写时,会报这个错。所以执行单元测试的时候,把server停掉。

All 17 comments

加上 .eslintignore 忽略不想监测的文件

他这个好像是权限问题啥的,EPERM: operation not permitted scandir 'D:\node\mox-api\logs\mox-api\common-error.log'
印象中 eslint . 默认只检查 js 文件的。

如果server占用着log文件,执行单元测试出错往common-error文件写时,会报这个错。所以执行单元测试的时候,把server停掉。

提供下测试代码。是不是没有 close

发自我的 iPhone

在 2017年4月27日,11:44,Bruce Wang notifications@github.com 写道:

如果server占用着log文件,执行单元测试出错往common-error文件写时,会报这个错。所以执行单元测试的时候,把server停掉。


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

'use strict';
const assert = require('assert');
const mock = require('egg-mock');
describe('uuid()', () => {
  let app;
  before(() => {
    app = mock.app();
    return app.ready();
  });
  it('should uuid length 12', () => {
    const ctx = app.mockContext();
    assert(ctx.helper.uuid().length === 12);
  });
});

是不是因为web服务占用着这个文件资源,所以test无法写文件?

after(() => app.close());
afterEach(mock.restore);

在 close 里面会关闭 logger,你没用 egg-init 生成项目么?这些都包含在内了。

@atian25 不好意思,我直接把那个test case删了,没注意看

@atian25 你好。咨询下,什么情况下单测里需要close,刚刚运行单测,会有这样的问题,单测代码如下
test/app/controller/home.test.js

'use strict';
const mm = require('egg-mock');
const assert = require('assert');

describe('test/app/controller/home.test.js', () => {
  let app;
  before(() => {
    app = mm.app();
    return app.ready();
  });

  afterEach(mm.restore);
  after(() => app.close());

  it('should assert', () => {
    const pkg = require('../../../package.json');
    assert(app.config.keys.startsWith(pkg.name));
  });

  it('should GET /', () => {
    return app.httpRequest()
      .get('/')
      .expect('hi, egg')
      .expect(200);
  });
});

app/controller/home.js

'use strict';

module.exports = app => {
  class HomeController extends app.Controller {
    async index() {
      this.ctx.body = 'hi, egg';
    }
  }
  return HomeController;
};

运行npm test后,会提示这样的错误

   √ should assert
    √ should GET /
Error: D:\git\mab\egg-example\logs\mydemo\mydemo-web.log log stream had been closed
    at FileTransport.log (D:\git\mab\egg-example\node_modules\egg-logger\lib\transports\file.js:57:19)
    at Map.log (D:\git\mab\egg-example\node_modules\egg-logger\lib\logger.js:73:19)
    at Map.Logger.(anonymous function) [as info] (D:\git\mab\egg-example\node_modules\egg-logger\lib\logger.js:141:10)
    at Sequelize.log (D:\git\mab\egg-example\node_modules\egg-sequelize\lib\loader.js:12:24)
    at Request.userCallback (D:\git\mab\egg-example\node_modules\sequelize\lib\dialects\mssql\query.js:85:28)
    at Request._this.callback (D:\git\mab\egg-example\node_modules\tedious\lib\request.js:47:27)
    at Connection.message (D:\git\mab\egg-example\node_modules\tedious\lib\connection.js:1401:27)
    at Connection.dispatchEvent (D:\git\mab\egg-example\node_modules\tedious\lib\connection.js:687:45)
    at MessageIO.<anonymous> (D:\git\mab\egg-example\node_modules\tedious\lib\connection.js:602:18)
    at emitNone (events.js:86:13)
    at MessageIO.emit (events.js:188:7)
    at ReadablePacketStream.<anonymous> (D:\git\mab\egg-example\node_modules\tedious\lib\message-io.js:102:16)
    at emitOne (events.js:96:13)
    at ReadablePacketStream.emit (events.js:191:7)
    at readableAddChunk (D:\git\mab\egg-example\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:212:18)
    at ReadablePacketStream.Readable.push (D:\git\mab\egg-example\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:171:10)
    at ReadablePacketStream.Transform.push (D:\git\mab\egg-example\node_modules\tedious\node_modules\readable-stream\lib\_stream_transform.js:123:32)

而在test代码中,将close删掉就好了,整个过程并没有运行web程序。

应用不需要 close

@popomore 你说的应用是指controller和service等吗,我看楼上测的是helper方法,需要close。

有多个 *.test.js 文件?

@atian25 目前就一个。我是正准备写

如果是应用测试,一般不需要 close,除非你要测试不同 env 下的应用。
如果是插件和框架测试,就需要 close。

mm.app 会缓存,close 重新打开缓存就没用了,速度会变慢。插件 close 是因为 baseDir 会做切换,无法缓存。

明白,谢谢。

@popomore 就一个测试文件情况下,最后 after 才 close 的,这里怎么报错了?

报错跟 sequelize 有关吧

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ailein picture Ailein  ·  3Comments

wujianling picture wujianling  ·  3Comments

whlsxl picture whlsxl  ·  3Comments

zhaofinger picture zhaofinger  ·  3Comments

Leungkingman picture Leungkingman  ·  3Comments