0

我正在学习 nodejs 并为 shelljs 函数编写了这个包装器,实际上它似乎按预期工作。

/**
 * Wrapper for Shelljs.exec to always return a promise
 *
 * @param  {String} cmd - bash-compliant command string
 * @param  {String} path - working directory of the process
 * @param {Object} _shell - alternative exec function for testing.
 * @returns {String}
 * @throws {TypeError}
 */
function shellExec(cmd, path, _shell = shelljs){
    if( typeof _shell.exec !== "function") throw new TypeError('_shell.exec must be a function');
    return new Promise((resolve, reject) => {
        let options =  { cwd: path, silent: true, asyc: true }
        // eslint-disable-next-line no-unused-vars
        return _shell.exec(cmd, options, (code, stdout, stderr) => {
            // shelljs.exec does not always return a code
            if(stderr) {
                return reject(stderr);
            }
            return resolve(stdout);
        });
    });
}

但是,当我尝试对其进行单元测试时,该功能会超时。我已经阅读了 mochajs 文档中关于异步代码、promiseasync/await的测试。我想使用一个sinon fake 来返回一个我知道有效的承诺。Mocha 告诉我错误是该函数没有通过错误返回承诺Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves。我想我已经不正确地构建了假货,但我看不出我应该怎么做。

const { expect, use } = require('chai');
const sinon = require('sinon');
const sinonChai = require("sinon-chai");
const utils = require('../utility/exec');

use(sinonChai);

it('sinon fake should resolve', async () =>{
    const fake = sinon.fake.resolves('resolved');

    const result = await fake();
    expect(result).to.equal('resolved');
});
describe('Utility Functions', () =>{
    describe('shellExec', () =>{
        it('should accept an alternate execute function', async () =>{
            const fakeShell = { exec: sinon.fake.resolves('pass') };
            const result = await utils.shellExec('pwd', 'xyz', fakeShell);
            expect(result).to.equal('pass');
            expect(fakeShell.exec).to.have.been.calledOnce;
        });
    });
});
4

2 回答 2

1

your_shell.exec只是一个回调函数,它不是 Promise。这就是为什么当您将 shell.exec 伪装成一个承诺时,您resolve将永远不会被调用。我认为你需要把你的 fakeShell 伪装成这样:

const fakeShell = { 
   exec: (cmd, options, cb) => {
      cb(true, 'pass', null);
   }
};
于 2018-08-09T15:27:18.870 回答
1

您的功能有点复杂,但没有任何 sinon 无法处理存根。有关更多信息,请参见https://sinonjs.org/releases/v1.17.7/stubs/,但您应该callsArgOnWith在函数之前使用。

您需要将其设置为存根,而不是设置exec返回承诺。这样,您可以在callsArgOnWith遇到回调时使用该函数调用回调。

我已经更改了您的测试,因此它现在通过更改假exec函数以返回存根并在运行您的函数之前const fakeShell = { exec: sinon.stub() };添加该行来通过fakeShell.exec.callsArgOnWith(2, null, null, 'pass', null)

const { expect, use } = require('chai');
const sinon = require('sinon');
const sinonChai = require("sinon-chai");
const utils = require('./main');


use(sinonChai);

it('sinon fake should resolve', async () =>{
    const fake = sinon.fake.resolves('resolved');

    const result = await fake();
    expect(result).to.equal('resolved');
});
describe('Utility Functions', () =>{
    describe('shellExec', () =>{
        it('should accept an alternate execute function', async () =>{
            const fakeShell = { exec: sinon.stub() };
            fakeShell.exec.callsArgOnWith(2, null, null, 'pass', null)
            const result = await utils.shellExec('pwd', 'xyz', fakeShell);            
            expect(result).to.equal('pass');
            expect(fakeShell.exec).to.have.been.calledOnce;
        });
    });
});
于 2018-08-09T15:40:03.737 回答