egg-mock
mock server for egg
Last updated 8 years ago by popomore .
Repository · Bugs · Original npm · Tarball · package.json
$ npm install egg-mock 
SYNC missed versions from official npm registry.

egg-mock

NPM version Node.js CI Test coverage npm download

一个数据模拟的库,更方便地测试 Egg 应用、插件及自定义 Egg 框架。egg-mock 拓展自 node_modules/mm,你可以使用所有 mm 包含的 API。

Install

$ npm i egg-mock --save-dev

Usage

创建测试用例

通过 mm.app 启动应用,可以使用 App 的 API 模拟数据

// test/index.test.js
const path = require('path');
const mm = require('egg-mock');

describe('some test', () => {
  let app;
  before(() => {
    app = mm.app({
      baseDir: 'apps/foo'
      customEgg: path.join(__dirname, '../node_modules/egg'),
    });
    return app.ready();
  })
  after(() => app.close());

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

使用 mm.app 启动后可以通过 app.agent 访问到 agent 对象。

使用 mm.cluster 启动多进程测试,API 与 mm.app 一致。

应用开发者

应用开发者不需要传入 baseDir,其为当前路径

before(() => {
  app = mm.app({
    customEgg: path.join(__dirname, '../node_modules/egg'),
  });
  return app.ready();
});

框架开发者

框架开发者需要指定 customEgg,会将当前路径指定为框架入口

before(() => {
  app = mm.app({
    baseDir: 'apps/demo',
    customEgg: true,
  });
  return app.ready();
});

插件开发者

在插件目录下执行测试用例时,只要 package.json 中有 eggPlugin.name 字段,就会自动把当前目录加到插件列表中。

before(() => {
  app = mm.app({
    baseDir: 'apps/demo',
    customEgg: path.join(__dirname, '../node_modules/egg'),
  });
  return app.ready();
});

也可以通过 customEgg 指定其他框架,比如希望在 aliyun-egg 和 framework-b 同时测试此插件。

describe('aliyun-egg', () => {
  let app;
  before(() => {
    app = mm.app({
      baseDir: 'apps/demo',
      customEgg: path.join(__dirname, 'node_modules/aliyun-egg'),
    });
    return app.ready();
  });
});

describe('framework-b', () => {
  let app;
  before(() => {
    app = mm.app({
      baseDir: 'apps/demo',
      customEgg: path.join(__dirname, 'node_modules/framework-b'),
    });
    return app.ready();
  });
});

如果当前目录确实是一个 egg 插件,但是又不想当它是一个插件来测试,可以通过 options.plugin 选项来关闭:

before(() => {
  app = mm.app({
    baseDir: 'apps/demo',
    customEgg: path.join(__dirname, 'node_modules/egg'),
    plugin: false,
  });
  return app.ready();
});

API

mm.app(options)

创建一个 mock 的应用。

mm.cluster(options)

创建一个多进程应用,因为是多进程应用,无法获取 worker 的属性,只能通过 supertest 请求。

const mm = require('egg-mock');
describe('test/app.js', () => {
  let app, config;
  before(() => {
    app = mm.cluster();
    return app.ready();
  });
  after(() => app.close());

  it('some test', () => {
    return app.httpRequest()
      .get('/config')
      .expect(200)
  });
});

默认会启用覆盖率,因为覆盖率比较慢,可以设置 coverage 关闭

mm.cluster({
  coverage: false,
});

mm.env(env)

设置环境变量,主要用于启动阶段,运行阶段可以使用 app.mockEnv。

// 模拟生成环境
mm.env('prod');
mm.app({
  cache: false,
});

具体值见 https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L82

mm.consoleLevel(level)

mock 终端日志打印级别

// 不输出到终端
mm.consoleLevel('NONE');

可选 level 为 DEBUG, INFO, WARN, ERROR, NONE

mm.home(homePath)

模拟操作系统用户目录

mm.restore

还原所有 mock 数据,一般需要结合 afterEach(mm.restore) 使用

options

mm.app 和 mm.cluster 的配置参数

baseDir {String}

当前应用的目录,如果是应用本身的测试可以不填默认为 $CWD。

指定完整路径

mm.app({
  baseDir: path.join(__dirname, 'fixtures/apps/demo'),
})

也支持缩写,找 test/fixtures 目录下的

mm.app({
  baseDir: 'apps/demo',
})

customEgg {String/Boolean}

指定框架路径

mm.app({
  baseDir: 'apps/demo',
  customEgg: path.join(__dirname, 'fixtures/egg'),
})

对于框架的测试用例,可以指定 true,会自动加载当前路径。

plugin

指定插件的路径,只用于插件测试。设置为 true 会将当前路径设置到插件列表。

mm.app({
  baseDir: 'apps/demo',
  plugin: true,
})

plugins {Object}

传入插件列表,可以自定义多个插件

cache {Boolean}

是否需要缓存,默认开启。

是通过 baseDir 缓存的,如果不需要可以关闭,但速度会慢。

clean {Boolean}

是否需要清理 log 目录,默认开启。

如果是通过 ava 等并行测试框架进行测试,需要手动在执行测试前进行统一的日志清理,不能通过 mm 来处理,设置 cleanfalse

app.mockLog([logger]) and app.expectLog(str[, logger]), app.notExpectLog(str[, logger])

断言指定的字符串记录在指定的日志中。 建议 app.mockLog()app.expectLog() 或者 app.notExpectLog() 配对使用。 单独使用 app.expectLog() 或者 app.notExpectLog() 需要依赖日志的写入速度,在服务器磁盘高 IO 的时候,会出现不稳定的结果。

it('should work', async () => {
  // 将日志记录到内存,用于下面的 expectLog
  app.mockLog();
  await app.httpRequest()
    .get('/')
    .expect('hello world')
    .expect(200);

  app.expectLog('foo in logger');
  app.expectLog('foo in coreLogger', 'coreLogger');
  app.expectLog('foo in myCustomLogger', 'myCustomLogger');

  app.notExpectLog('bar in logger');
  app.notExpectLog('bar in coreLogger', 'coreLogger');
  app.notExpectLog('bar in myCustomLogger', 'myCustomLogger');
});

app.httpRequest()

请求当前应用 http 服务的辅助工具。

it('should work', () => {
  return app.httpRequest()
    .get('/')
    .expect('hello world')
    .expect(200);
});

更多信息请查看 supertest 的 API 说明。

.unexpectHeader(name)

断言当前请求响应不包含指定 header

it('should work', () => {
  return app.httpRequest()
    .get('/')
    .unexpectHeader('set-cookie')
    .expect(200);
});

.expectHeader(name)

断言当前请求响应包含指定 header

it('should work', () => {
  return app.httpRequest()
    .get('/')
    .expectHeader('set-cookie')
    .expect(200);
});

app.mockContext(options)

模拟上下文数据

const ctx = app.mockContext({
  user: {
    name: 'Jason'
  }
});
console.log(ctx.user.name); // Jason

app.mockContextScope(fn, options)

安全的模拟上下文数据,同一用例用多次调用 mockContext 可能会造成 AsyncLocalStorage 污染

await app.mockContextScope(async ctx => {
  console.log(ctx.user.name); // Jason
}, {
  user: {
    name: 'Jason'
  }
});

app.mockCookies(data)

app.mockCookies({
  foo: 'bar'
});
const ctx = app.mockContext();
console.log(ctx.getCookie('foo'));

app.mockHeaders(data)

模拟请求头

app.mockSession(data)

app.mockSession({
  foo: 'bar'
});
const ctx = app.mockContext();
console.log(ctx.session.foo);

app.mockService(service, methodName, fn)

it('should mock user name', function* () {
  app.mockService('user', 'getName', function* (ctx, methodName, args) {
    return 'popomore';
  });
  const ctx = app.mockContext();
  yield ctx.service.user.getName();
});

app.mockServiceError(service, methodName, error)

可以模拟一个错误

app.mockServiceError('user', 'home', new Error('mock error'));

app.mockCsrf()

模拟 csrf,不用传递 token

app.mockCsrf();

return app.httpRequest()
  .post('/login')
  .expect(302);

app.mockHttpclient(url, method, data)

模拟 httpclient 的请求,例如 ctx.curl

app.get('/', async ctx => {
  const ret = await ctx.curl('https://eggjs.org');
  this.body = ret.data.toString();
});

app.mockHttpclient('https://eggjs.org', {
  // 模拟的参数,可以是 buffer / string / json / function
  // 都会转换成 buffer
  // 按照请求时的 options.dataType 来做对应的转换
  data: 'mock egg',
});

return app.httpRequest()
  .post('/')
  .expect('mock egg');

Bootstrap

我们提供了一个 bootstrap 来减少单测中的重复代码:

const { app, mock, assert } = require('egg-mock/bootstrap');

describe('test app', () => {
  it('should request success', () => {
    // mock data will be restored each case
    mock.data(app, 'method', { foo: 'bar' });
    return app.httpRequest()
      .get('/foo')
      .expect(res => {
        assert(!res.headers.foo);
      })
      .expect(/bar/);
  });
});

describe('test ctx', () => {
  it('can use ctx', async function() {
    const res = await this.ctx.service.foo();
    assert(res === 'foo');
  });
});

我们将会在每个 case 中自定注入 ctx, 可以通过 app.currentContext 来获取当前的 ctx。 并且第一次使用 app.mockContext 会自动复用当前 case 的上下文。

const { app, mock, assert } = require('egg-mock/bootstrap');

describe('test ctx', () => {
  it('should can use ctx', () => {
    const ctx = app.currentContext;
    assert(ctx);
  });

  it('should reuse ctx', () => {
    const ctx = app.currentContext;
    // 第一次调用会复用上下文
    const mockCtx = app.mockContext();
    assert(ctx === mockCtx);
    // 后续调用会新建上下文
    // 极不建议多次调用 app.mockContext
    // 这会导致上下文污染
    // 建议使用 app.mockContextScope
    const mockCtx2 = app.mockContext();
    assert(ctx !== mockCtx);
  });
});

env for custom bootstrap

EGG_BASE_DIR: the base dir of egg app EGG_FRAMEWORK: the framework of egg app

Questions & Suggestions

Please open an issue here.

License

MIT

Contributors

Contributors

Made with contributors-img.

Current Tags

  • 5.8.2-beta.8                                ...           beta (2 years ago)
  • 5.12.5                                ...           latest (6 months ago)
  • 1.3.1                                ...           latest-1 (8 years ago)
  • 1.3.1                                ...           release-1 (8 years ago)

132 Versions

  • 5.12.5                                ...           6 months ago
  • 5.12.4                                ...           6 months ago
  • 5.12.3                                ...           6 months ago
  • 5.12.2                                ...           6 months ago
  • 5.12.1                                ...           6 months ago
  • 5.12.0                                ...           7 months ago
  • 5.11.0                                ...           7 months ago
  • 5.10.9                                ...           a year ago
  • 5.10.8                                ...           2 years ago
  • 5.10.7                                ...           2 years ago
  • 5.10.6                                ...           2 years ago
  • 5.10.5                                ...           2 years ago
  • 5.10.4                                ...           2 years ago
  • 5.10.3                                ...           2 years ago
  • 5.10.2                                ...           2 years ago
  • 5.10.1                                ...           2 years ago
  • 5.10.0                                ...           2 years ago
  • 5.9.4                                ...           2 years ago
  • 5.9.3                                ...           2 years ago
  • 5.9.2                                ...           2 years ago
  • 5.9.1                                ...           2 years ago
  • 5.9.0                                ...           2 years ago
  • 5.8.4                                ...           2 years ago
  • 5.8.3                                ...           2 years ago
  • 5.8.2-beta.8                                ...           2 years ago
  • 5.8.2-beta.7                                ...           2 years ago
  • 5.8.2-beta.6                                ...           2 years ago
  • 5.8.2-beta.5                                ...           2 years ago
  • 5.8.2-beta.4                                ...           2 years ago
  • 5.8.2-beta.3                                ...           2 years ago
  • 5.8.2-beta.2                                ...           2 years ago
  • 5.8.2-beta.1                                ...           2 years ago
  • 5.8.2-beta.0                                ...           2 years ago
  • 5.8.2                                ...           2 years ago
  • 5.8.1                                ...           2 years ago
  • 5.8.0                                ...           2 years ago
  • 5.7.1                                ...           2 years ago
  • 5.7.0                                ...           2 years ago
  • 5.6.0                                ...           2 years ago
  • 5.5.0                                ...           2 years ago
  • 5.4.0                                ...           2 years ago
  • 5.3.0                                ...           2 years ago
  • 5.2.1                                ...           2 years ago
  • 5.2.0                                ...           2 years ago
  • 5.1.0                                ...           2 years ago
  • 5.0.2                                ...           2 years ago
  • 5.0.1                                ...           2 years ago
  • 5.0.0                                ...           2 years ago
  • 4.2.1                                ...           3 years ago
  • 4.2.0                                ...           3 years ago
  • 3.26.0                                ...           3 years ago
  • 4.1.0                                ...           4 years ago
  • 4.0.1                                ...           4 years ago
  • 4.0.0                                ...           5 years ago
  • 3.25.1                                ...           5 years ago
  • 3.25.0                                ...           5 years ago
  • 3.24.2                                ...           5 years ago
  • 3.24.1                                ...           5 years ago
  • 3.24.0                                ...           5 years ago
  • 3.23.2                                ...           5 years ago
  • 3.23.1                                ...           6 years ago
  • 3.23.0                                ...           6 years ago
  • 3.22.4                                ...           6 years ago
  • 3.22.3                                ...           6 years ago
  • 3.22.2                                ...           6 years ago
  • 3.22.1                                ...           6 years ago
  • 3.22.0                                ...           6 years ago
  • 3.21.0                                ...           6 years ago
  • 3.20.1                                ...           6 years ago
  • 3.20.0                                ...           6 years ago
  • 3.19.7                                ...           6 years ago
  • 3.19.6                                ...           6 years ago
  • 3.19.5                                ...           6 years ago
  • 3.19.4                                ...           6 years ago
  • 3.19.3                                ...           6 years ago
  • 3.19.2                                ...           6 years ago
  • 3.19.1                                ...           6 years ago
  • 3.19.0                                ...           6 years ago
  • 3.18.0                                ...           6 years ago
  • 3.17.3                                ...           6 years ago
  • 3.17.2                                ...           7 years ago
  • 3.17.1                                ...           7 years ago
  • 3.17.0                                ...           7 years ago
  • 3.16.0                                ...           7 years ago
  • 3.15.1                                ...           7 years ago
  • 3.15.0                                ...           7 years ago
  • 3.14.1                                ...           7 years ago
  • 3.14.0                                ...           7 years ago
  • 3.13.1                                ...           7 years ago
  • 3.13.0                                ...           7 years ago
  • 3.12.2                                ...           7 years ago
  • 3.12.1                                ...           7 years ago
  • 3.12.0                                ...           7 years ago
  • 3.11.0                                ...           7 years ago
  • 3.10.0                                ...           7 years ago
  • 3.9.1                                ...           7 years ago
  • 3.9.0                                ...           7 years ago
  • 3.8.0                                ...           8 years ago
  • 3.7.2                                ...           8 years ago
  • 3.7.1                                ...           8 years ago
  • 3.7.0                                ...           8 years ago
  • 3.6.1                                ...           8 years ago
  • 3.6.0                                ...           8 years ago
  • 3.5.0                                ...           8 years ago
  • 3.4.0                                ...           8 years ago
  • 3.3.0                                ...           8 years ago
  • 3.2.0                                ...           8 years ago
  • 3.1.2                                ...           8 years ago
  • 3.1.1                                ...           8 years ago
  • 3.1.0                                ...           8 years ago
  • 3.0.1                                ...           8 years ago
  • 3.0.0                                ...           8 years ago
  • 2.4.0                                ...           8 years ago
  • 2.3.1                                ...           8 years ago
  • 2.3.0                                ...           8 years ago
  • 2.2.0                                ...           8 years ago
  • 2.1.0                                ...           8 years ago
  • 2.0.0                                ...           8 years ago
  • 1.3.1                                ...           8 years ago
  • 1.3.0                                ...           8 years ago
  • 1.2.1                                ...           8 years ago
  • 1.2.0                                ...           8 years ago
  • 1.1.0                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
  • 0.0.8                                ...           8 years ago
  • 0.0.7                                ...           8 years ago
  • 0.0.6                                ...           8 years ago
  • 0.0.5                                ...           8 years ago
  • 0.0.4                                ...           8 years ago
  • 0.0.3                                ...           8 years ago
  • 0.0.2                                ...           8 years ago
  • 0.0.1                                ...           8 years ago
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (15)
Dev Dependencies (13)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE