1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| const assert = require('assert'); const { describe, it } = require("mocha"); const toolkit = require('../help/toolkit'); const request = require('supertest');
const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.json({ limit: '10mb' })); app.use(bodyParser.urlencoded({ limit: '10mb', extended: false }));
app.get('/', (req, res) => res.send('Hello World!')); app.get('/get', (req, res) => res.send(`Hello ${req.query.name}!`)); app.post('/post', (req, res) => res.send({msg: `Hello ${req.body.name}!`})); app.listen(4567, () => console.log('Test listening port 4567'));
const url = 'http://127.0.0.1:4567';
describe('basic 测试', function () { it('strictEqual', async function () { assert.strictEqual(1, 1, '不相等'); }); it('notStrictEqual', async function () { assert.notStrictEqual(1, 2); }); it('ok', async function () { assert.ok(true); }); it('ok', async function () { assert.ok(1); }); it('throws', async function () { assert.throws(() => { throw new Error('throw error'); }) }); it('doesNotThrow', async function () { assert.doesNotThrow(() => { let a = 123; }) }); it('deepStrictEqual', async function () { const person1 = { "name":"jane", "age":"19" }; const person2 = { "name":"jane", "age":"19" }; assert.deepStrictEqual(person1, person2) }); it('notDeepStrictEqual', async function () { const person1 = { "name":"jaae", "age":"22" }; const person2 = { "name":"jane", "age":"19" }; assert.notDeepStrictEqual(person1, person2) }); });
describe('HTTP 测试', function () { const name = 'World'; it('get', async function () { const res = await toolkit.get(`${url}/get?name=${name}`); assert.strictEqual(res.body, 'Hello World!'); }); it('post', async function () { const res = await toolkit.post(url, '/post', { name }); assert.strictEqual(res.body.msg, 'Hello World!'); }); it('request GET', function (done) { request(url) .get('/get?name=World') .expect('Hello World!', done) }); it('request POST', function (done) { request(url) .post('/post') .send({name}) .expect(function(res) { }) .expect(200, { msg: 'Hello World!', }, done); }); });
|