是否可以从节点端点 运行 mocha 测试套件?
Is it possible to run mocha test suite from node endpoint?
我们的团队构建了一个 API 来为我们的组织提供内部网络服务。我已经构建了一个用于测试代码库的 mocha 测试套件,目前正在通过 CLI 运行 它。
我们有一个企业监控服务,定期测试每台服务器的“/webcheck”端点,并在这些服务器中的任何一台出现故障时通知我们。
目前该端点仅以 'overall_status_code: ok' 字符串响应,告诉我们服务器是 运行,仅此而已。其他端点可能会搞砸,而我们不会知道。
我想将此端点绑定到我的 mocha 测试套件中,这样只有 returns '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();
})
})
})
是的,你只需要一个client rest到nodejs a compare the result in expect,但这不是单元测试,是集成测试。
我们的团队构建了一个 API 来为我们的组织提供内部网络服务。我已经构建了一个用于测试代码库的 mocha 测试套件,目前正在通过 CLI 运行 它。
我们有一个企业监控服务,定期测试每台服务器的“/webcheck”端点,并在这些服务器中的任何一台出现故障时通知我们。
目前该端点仅以 'overall_status_code: ok' 字符串响应,告诉我们服务器是 运行,仅此而已。其他端点可能会搞砸,而我们不会知道。
我想将此端点绑定到我的 mocha 测试套件中,这样只有 returns '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();
})
})
})
是的,你只需要一个client rest到nodejs a compare the result in expect,但这不是单元测试,是集成测试。