首页 > 解决方案 > 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');

        }

      });

    });

  });

标签: node.jsunit-testingprocessmocha.js

解决方案


推荐阅读