In the Node. js environment, how does one add the travis ci continuous integration tool to Koa2? node. jskoa2
Preface
Because we recently used koa2 for project testing and development, we wanted to integrate travis ci with less information on the Internet, so we made a whole record. Share it for your reference and study. Let's take a look at the detailed introduction below.
The method is as follows:
Let's take a look at the configuration of travis. yml.
language: node_jsnode_js: - "6"before_script: - ./node_modules/.bin/knex migrate:latest --knexfile='./app/knexfile.js'script: - npm run test
Because it is an interface test, table creation and other operations are required first.
Test command:
NODE_ENV=production NODE_CONFIG_DIR='./app/config/' ./node_modules/.bin/mocha --require 'babel-polyfill' --compilers js:babel-register ./app/test/**/*.js
The main purpose is to test the usage of supertest. Let's take a look at how to call it.
const request = require('supertest');const should = require('should');const index = require('../../index');let app = request(index.listen());describe('/api/persons', function() { let personId; it('POST /api/persons - create person success and respond with 200', function(done) { app.post('/api/persons') .send({ 'firstName': 'Jennifer', 'lastName': 'Lawrence', 'age': 24 }) .expect(200) .expect(function(res) { (res.body.id > 0).should.be.true; }) .end(function(err, res) { if (err) { return done(err); } let resJson = JSON.parse(res.text); personId = resJson.id; done(); }) }); it('GET /api/persons - fetch persons item', function(done) { app.get('/api/persons') .expect(200) .expect(function(res) { (res.body.length > 0).should.be.true; }) .end(function(err, res) { if (err) { return done(err); } done(); }) }); it('GET /api/persons/:id - fetch a person', function(done) { app.get(`/api/persons/${personId}`) .expect(200) .expect(function(res) { (res.body.id == personId).should.be.true; }) .end(function(err, res) { if (err) { return done(err); } done(); }) }); it('DELETE /api/persons/:id - delete a person', function(done) { app.delete(`/api/persons/${personId}`) .expect(200) .end(function(err, res) { if (err) { return done(err); } done(); }) }); it('GET /api/persons/:id - fetch a person should 404', function(done) { app.get(`/api/persons/${personId}`) .expect(404) .end(function(err, res) { if (err) { return done(err); } done(); }) });});
Note that
const index = require('../../index');
The koa instance needs to be exposed. Otherwise, after the integration of travis ci, the project is started and the specific access address is still not found during the test.
Let's take a look at my index. js
import Knex from 'knex';import { Model} from 'objection';import knexConfig from './knexfile';import config from 'config';import Koa from 'koa';import koaLogger from 'koa-logger';import bodyParser from 'koa-bodyparser';import render from 'koa-ejs';import co from 'co';import koaStatic from "koa2-static"import router from './router';const path = require('path');// initial knexconst knex = Knex(knexConfig.development);Model.knex(knex);// initial appconst app = new Koa();// initial renderrender(app, { root: path.join(__dirname + '/view'), layout: 'template', viewExt: 'ejs', cache: true, debug: true});app.context.render = co.wrap(app.context.render);// initial staticapp.use(koaLogger()) .use(bodyParser()) .use(router.routes()) .use(koaStatic({ path: '/web', root: __dirname + "/../static" }));module.exports = app;
Note that
module.exports = app;
The server test can be started independently only in supertest.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.