node.js - Node.js 中 mocha.js 中的退出代码
问题描述
我正在测试一个最终在控制台上打印的功能。我在标准输入上检查并相应地断言。但是如果我这样做,那么只有当我按下键盘上的 enter 键或调用outer mostprocess.exit(0)
时,该过程才会结束。现在,如果我打电话,那么即使测试用例失败,我仍然会得到退出状态 0,如何使用 mocha 的退出代码退出?或者有没有更好的方法来实现这一点(我不打算更改我的代码,因为它是一个 CLI 工具)。after()
describe()
process.exit(0)
我的测试代码:
describe('Testing createTestExecutionForTestCases(testExecutionData)', () => {
const testExecutionData = {
info: {
summary: 'summary',
},
tests: [{
testKey: 'KEY-11',
comment: 'failed',
status: 'FAIL',
}],
};
before(() => {
const basePath = `${JIRAConfig.host}:${JIRAConfig.port}`;
process.env.TOKEN = 'fake_token';
const issueSearch = nock(basePath);
issueSearch
.post(`${JIRAConfig.testExecutionEndpoint}`, () => true).reply(200, {
testExecIssue: {
id: '11410',
key: 'KEY-1',
self: 'http://172.16.201.132:8080/rest/api/2/issue/11410',
},
})
.post(`${JIRAConfig.testExecutionEndpoint}`, () => true).reply(404, {
error: 'ERROR',
errorMessage: 'Summary could not be empty',
});
});
after(() => {
nock.cleanAll();
});
it('Should create test execution issue in JIRA', async () => {
await createTestExecutionForTestCases(testExecutionData);
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
expect(chunk).to.be.a('string');
expect(chunk).to.equal('Test Execution created with ID KEY-1');
}
});
});
it('Should not create test execution issue if any of the mandatory is not present', async () => {
await createTestExecutionForTestCases(testExecutionData);
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
expect(chunk).to.be.a('string');
expect(chunk).to.equal('Unable to create Test Execution');
}
});
});
});
解决方案
推荐阅读
- c++ - 将 Gst-RTSP-Server 与 OpenCV c++ Windows 一起使用
- react-native - 如何刷新原生基础中的内部选项卡?
- angular - 为什么反应式表单验证会导致“无法读取未定义的属性'错误'”?
- java - 在不同的类中使用 JTextArea
- docker - 无法在 docker 中查看新创建的文件
- unity3d - Macbook 上的 Unity Oculus Link
- c# - 将 RTF 字符串合并为一个
- javascript - Javascript/Jquery/Gantt Highchart:点击任务进度弹出窗口应该打开
- ios - CallKit:屏幕锁定时启动应用程序
- php - 使文件可下载