简体   繁体   English

使用摩卡测试异步功能

[英]using mocha to test the async function

I'm trying to test the following asynchronous function. 我正在尝试测试以下异步功能。

async taskComplete() {
    this.logger.log('Task Complete');

    try {
      if (fs.existsSync(`${this.repoDir}/.MENLOLAB_EXIT`)) {
        console.log('.MENLOLAB_EXIT Found');

        const exitCode = fs.readFileSync(`${this.repoDir}/.MENLOLAB_EXIT`, 'utf8');
        let msg = '';

        if (exitCode.localeCompare('1')) msg = 'Catchall for general errors';
        else if (exitCode.localeCompare('2')) msg = 'Misuse of shell builtins';
        else if (exitCode.localeCompare('126')) msg = 'Command invoked cannot execute';
        else if (exitCode.localeCompare('127')) msg = '“command not found"';
        else if (exitCode.localeCompare('128')) msg = 'Invalid argument to exit';
        else if (exitCode.localeCompare('128+n')) msg = 'Fatal error signal “n”';
        else if (exitCode.localeCompare('130')) msg = 'Script terminated by Control-C';
        else if (exitCode.localeCompare('255\\*')) msg = 'Exit status out of range';

        await socket.taskExitCode({ jobID: this.taskData.jobID, exitCode, msg });
      } else {
        await socket.taskComplete(this.taskData.jobID);
      }
    } catch (err) {
      this.logger.error('Error attempting to send task complete');
      return this.taskComplete();
    }

    return this.destroyTask();
  }

My test code is bellow, it keeps timing out after 10000 ms. 我的测试代码如下,它在10000 ms之后一直超时。

1) Test MenloLab Runner - Task Class
       Report
         TASK.taskComplete()
           Generate a Report on Task Completion:
     Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/yury.stanev/menlolab/runner/test/tasks-tests.js)

From what I'm seeing the taskComplete() isn't getting called at all. 从我所看到的, taskComplete()根本没有被调用。 What can be the cause of this behaviour? 造成这种行为的原因是什么?

    describe('Report', () => {
    describe('TASK.taskComplete()', () => {
      it('Generate a Report on Task Completion', async () => {
        try {
          const task = new TASK(taskData, () => {});
          /* const result =  */await task.taskComplete();
          // console.log(`${this.repoDir}`);

          // shell.exec(`${taskData.cmd}`);

          // console.log('HERE ->', result);

          // if (fs.existsSync(`${this.repoDir}/.MENLOLAB_EXIT`)) {
          // const exitCode = ['1', '2', '126', '127', '128', '128+n', '130', '255\\*'];
          //   expect(`${this.repoDir}/.MENLOLAB_EXIT`).to.be.a.file().and.not.empty
          //     .to.be.a.file().with.contents('1' || '2' || '126' || '127' || '128' || '128+n' || '130' || '255');
          // } else {
          expect(task.destroyTask()).to.be.true;
          // }
        } catch (err) {
          expect(err).to.not.be.undefined;
        }
      });
    });
  });

Had to modify the function call to await this.task.taskComplete() which is strange. 必须修改函数调用以await this.task.taskComplete() ,这很奇怪。 The final test code is bellow. 最终的测试代码如下。

describe('Report', () => {
    describe('TASK.taskComplete()', () => {
      it('Generate a Report on Task Completion', async () => {
        try {
          const task = new TASK(taskData, () => {});
          await this.task.taskComplete();
          if (fs.existsSync(`${this.repoDir}/.MENLOLAB_EXIT`)) {
            expect(`${this.repoDir}/.MENLOLAB_EXIT`).to.be.a.file().and.not.empty;
          } else {
            expect(task.destroyTask()).to.be.true;
          }
        } catch (err) {
          expect(err).to.not.be.undefined;
        }
      });
    });
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM