1.0.0 • Published 5 years ago

egg-unittest-extend v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

egg-unittest-extend

unittest extension for egg.js, based on ctimmerm/axios-mock-adapter and aexmachina/factory-girl

Install

$ npm i egg-unittest-extend --save

Usage

// {app_root}/config/plugin.js
exports.eggUnittestExtend = {
  enable: true,
  package: 'egg-unittest-extend',
};

Example

factory-girl

Read aexmachina/factory-girl for more details.

// {app_root}/test/mock/factory/xxx.js
'use strict';

module.exports = app => {
  const { factory } = app;
  factory.define('User', app.model.User, {
    username: factory.chance('name'),
    age: factory.chance('age'),
  });
};

// {app_root}/test/app/service/xxx.test.js
'use strict';

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

let user
before(async () => {
  await app.ready();
  user = app.factory.build('User');
});

mock-axios-adapter

Read ctimmerm/axios-mock-adapter for more details.

// {app_root}/test/mock/axios/xxx.js
'use strict';

module.exports = app => {
  const { mockAxios } = app;
  mockAxios.onGet('https://test.mock.axios/')
    .reply(200, 'load mockAxios successfully');
};

// {app_root}/test/app/service/xxx.test.js
'use strict';

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

describe('test/app/service/xxx.test.js', () => {
  it('should get mock response by axios', async () => {
    const res = await axios.get('https://test.mock.axios/');
    assert(res.data === 'load mockAxios successfully');
  });
});

Questions & Suggestions

Please open an issue here.

License

MIT