是否可以从节点端点运行mocha测试套件?

时间:2018-10-12 15:48:03

标签: javascript node.js mocha chai supertest

我们的团队已构建了一个API,用于为我们的组织提供内部网络服务。我建立了一个用于测试代码库的mocha测试套件,当前正在通过CLI运行它。

我们有一个公司监控服务,该服务会定期测试每台服务器的'/ webcheck'终结点,并通知我们这些服务器是否已关闭。

当前,该端点仅以“ overall_status_code:ok”字符串作为响应,这告诉我们服务器正在运行,仅此而已。其他端点可能被弄乱了,我们不知道。

我想将此端点绑定到我的mocha测试套件中,以便在所有测试均通过的情况下,它仅返回“ overall_status_code:ok”。

有人暗示我该怎么做?基本上,如何从节点内部调用测试?

如果您有兴趣,这是我的测试代码:

const expect = require('chai').expect;
const app = require('../server/app.js');
const supertest = require('supertest');

const request = supertest.agent(app);

describe('Active Directory Module', () => {
    it('/ad/members endpoint should return: \n          -> an array of members for a given group \n          -> each element returned should be an object \n          -> John Doe should be the first user in the array', (done) => {
        request.get('/ad/members?group=%27CHQ%20Service%20Management%27')
        .expect(200)
        .end((err, res) => {
            let result = JSON.parse(res.text);
            expect(Array.isArray(result)).to.be.true;
            expect(typeof(result[0]) === 'object').to.be.true;
            expect(result[0].name === 'John Doe').to.be.true;
            done();
        })
    })
    it('/ad/membership endpoint should return: \n          -> an array of groups for a given user \n          -> each element returned should be an object \n          -> \'Domain Users\' should be the first element in the array', (done) => {
        request.get('/ad/membership?user=%27chq-mikeru%27')
        .expect(200)
        .end((err, res) => {
            let result = JSON.parse(res.text);
            // let result = res;
            expect(Array.isArray(result)).to.be.true;
            expect(typeof(result[0]) === 'object').to.be.true;
            expect(result[0].name === 'Domain Users').to.be.true;
            done();
        })
    })
    it('/ad/userinfo endpoint should return: \n          -> an object\n          -> Should include property called \'Given Name\'\n          -> given \'chq-stephenro\' the returned object should contain a property \'UserPrincipleName\' with value \'chq-stephenro@corp.company.com\'', (done) =>{
        request.get('/ad/userinfo?user=%27chq-stephenho%27')
        .expect(200)
        .end((err, res) => {
            let result = JSON.parse(res.text);
            expect(typeof(result) === 'object').to.be.true;
            expect(result['GivenName']).to.not.be.undefined;
            expect(result['UserPrincipalName'] === 'chq-stephenro@corp.company.com').to.be.true;
            done();
        })
    })
})

describe('Database Querying Module', () => {
    it('/db/ci-co-conflict-checker should return \n          --> an array\n          --> of arrays\n          --> For each element:\n                    * the first, third, and fourth elements should be numbers\n                    * the second element should be a string\n                    * the fifth element should be a string', (done) => {
        request.get('/db/ci-co-conflict-checker/?cis=%27CA%20Service%20Catalog%27,%27CA%20Service%20Desk%20Manager%27&start=1533106860&end=1535699040')
        .expect(200)
        .end((err, res) => {
            let result = res.body;
            expect(Array.isArray(result)).to.be.true;
            expect(Array.isArray(result[0])).to.be.true;
            for ( var i = 0; i < result.length; i++ ) {
                for (var j = 0; j < result[i].length; j++ ) {
                    if ( j === 0 || j === 2 || j === 3) {
                        expect(typeof(result[i][j]) === 'number').to.be.true;
                    }
                    if ( j === 1 || j === 4 ) {
                        expect (typeof(result[i][j]) === 'string').to.be.true;
                    }
                }
            }
            done();
        })
    })
})

1 个答案:

答案 0 :(得分:0)

是的,您只需要休息一下就可以对nodejs进行预期结果的比较,但这不是单元测试,而是集成测试。