测试文件,统一以 *.test.js结尾
image.png

home.test.js

  1. 'use strict';
  2. const { app, assert } = require('egg-mock/bootstrap');
  3. describe('test/app/controller/home.test.js', () => {
  4. it('should assert', () => {
  5. const pkg = require('../../../package.json');
  6. assert(app.config.keys.startsWith(pkg.name));
  7. // const ctx = app.mockContext({});
  8. // yield ctx.service.xx();
  9. });
  10. it('should GET /', () => {
  11. return app.httpRequest()
  12. .get('/')
  13. .expect('hi, egg')
  14. .expect(200);
  15. });
  16. });

user.test.js

  1. 'use strict';
  2. const { app } = require('egg-mock/bootstrap');
  3. describe('user test', () => {
  4. it('test/app/controller/user.test.js', () => {
  5. return app.httpRequest()
  6. .get('/user')
  7. .expect(200)
  8. .expect('user controller index');
  9. });
  10. it('test/app/controller/user.test.js', async () => {
  11. await app.httpRequest()
  12. .get('/user/lists')
  13. .expect(200)
  14. .expect('[{"id":1,"name":"lucy"}]'); // 严格的JSON
  15. });
  16. });