NodeJS TypeError:stub预期产生,但未传递回调

deikduxw  于 2023-05-22  发布在  Node.js
关注(0)|答案(1)|浏览(87)

我正在将一些mysql数据库从一个主要版本迁移到下一个版本,我有几个使用它们的nodejs项目。为了使这些项目能够在新的mysql版本中工作,我必须升级mysql 2依赖项(和node版本)。我有一个mysql healthcheck,在这个新版本的mysql 2下没有运行:

const check = {
  mysql: client =>
    cb => client.query('SELECT 1+1 AS result', error => cb(null, {
      success: !error,
      details: { error },
    }))
}

错误为Callback function is not available with promise clients.
我更新了代码,看起来像这样:

const check = {
  mysql: client =>
    cb => client.query('SELECT 1+1 AS result')
      .then(_ => {
        cb(null, {
          success: true,
        });
      })
      .catch(err => {
        cb(null, {
          success: !err,
          details: { err },
        })})
}

我不知道这是否真的修复了健康检查,但它确实破坏了测试:

it('should return when client does not returns an err', (done) => {
      const fakeClient = { query: sinon.stub().yieldsAsync(null) };

      const mysqlCheck = check.mysql(fakeClient);

      mysqlCheck((err, report) => {
        expect(fakeClient.query).calledWith('SELECT 1+1 AS result');

        expect(err).not.to.exist;
        expect(report.success).to.be.true;
        expect(report.details.error).to.not.exist;
        done();
      });
    });

错误如下:TypeError: stub expected to yield, but no callback was passed. Received [SELECT 1+1 AS result]
我试过修改测试,但我对nodejs了解不多,我甚至不确定我是否理解问题所在。
有人能给我指个方向吗?
先谢谢你了

编辑我已经更新了我的存根看起来像这样,感谢@Bergi:

const fakeClient = { query: () => Promise.resolve() };
sinon.stub(fakeClient, 'query').resolves().callsFake(() => {
  return Promise.resolve({
    success: true
  });
});

测试通过,但我收到警告:UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected false to be true
我能安全地忽略它们吗?或者我应该做些什么?

pcrecxhr

pcrecxhr1#

示例:
check.js

const check = {
  mysql: client =>
    cb => client.query('SELECT 1+1 AS result')
      .then(_ => {
        cb(null, {
          success: true,
        });
      })
      .catch(err => {
        cb(null, {
          success: !err,
          details: { err },
        })
      })
}

module.exports = { check }

check.test.js

const sinon = require('sinon');
const { check } = require('./check');

describe('76045264', () => {
  it('should return when client does not returns an err', (done) => {
    const fakeClient = { query: sinon.stub().resolves() };
    const mysqlCheck = check.mysql(fakeClient);

    mysqlCheck((err, report) => {
      sinon.assert.calledWithExactly(fakeClient.query, 'SELECT 1+1 AS result')
      sinon.assert.match(report, { success: true })
      sinon.assert.match(err, null)
      done();
    });
  });
})

检测结果

76045264
    ✓ should return when client does not returns an err

  1 passing (13ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |   83.33 |      100 |      75 |      80 |                   
 check.js |   83.33 |      100 |      75 |      80 | 10                
----------|---------|----------|---------|---------|-------------------

软件包版本:"sinon": "^8.1.1"

相关问题