gpt4 book ai didi

node.js - 无效的 Chai 属性 : setEncoding on nock

转载 作者:太空宇宙 更新时间:2023-11-04 01:32:49 25 4
gpt4 key购买 nike

我使用 nock 9.1.6chai 3.5.0 模拟 nylas API。它与代码完美配合:

it('fails when no state is given and provider fails', () => {
const user = fixture.elements.user1;
const connector = new NylasConnector('nylas', 'connect', user);

nock(NYLAS_API)
.post(`/a/${DN_NYLAS_APP_ID}/accounts/${user.nylas.account_id}/downgrade`)
.reply(500, (uri, body) => expect(body).to.eql(''));

sinon.stub(Nylas, 'urlForAuthentication').callsFake(params => {
expect(params).to.eql({
redirectURI: `${DN_AUTH_SERVICE_URL}/oauth2/nylas/callback`,
trial: false,
state: JSON.stringify({})
});
return chance.url() + '?' + objectToQuery(params);
});

return expect(connector.getAuthenticationUrl()).to.be.rejectedWith(ProviderError)
.then(() => User.findById(user._id))
.then(user => {
expect(user.nylas).not.null;
expect(user.nylasToken).not.null;
expect(user.nylasCursor).not.null;
});
})

我尝试将 nock 升级到 10.0.6,将 chai 升级到 4.2.0,现在出现以下错误:

"Error: Invalid Chai property: setEncoding",
" at Object.proxyGetter [as get] (/home/jeremy/datananas/datananas/packages/api/node_modules/chai/lib/chai/utils/proxify.js:78:17)",
" at Object.isStream (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/common.js:382:24)",
" at continueWithResponseBody (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:435:20)",
" at end (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:385:12)",
" at /home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:160:9",
" at OverriddenClientRequest.RequestOverrider.req.write (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:139:9)",
" at OverriddenClientRequest.RequestOverrider.req.end (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:156:11)",
" at Request.end (/home/jeremy/datananas/datananas/packages/api/node_modules/request/request.js:1514:14)",
" at end (/home/jeremy/datananas/datananas/packages/api/node_modules/request/request.js:564:14)",
" at Immediate.<anonymous> (/home/jeremy/datananas/datananas/packages/api/node_modules/request/request.js:578:7)",
" at runCallback (timers.js:705:18)",
" at tryOnImmediate (timers.js:676:5)",
" at processImmediate (timers.js:658:5)"

该错误出现在 connector.getAuthenticationUrl() 上,因为它是调用模拟函数的函数。在此函数中,它们是对 nylasRequest 的调用,并且当到达包含 new Promise() 的行时出现错误:

function nylasRequest(uri, token, body) {
const params = {
uri: `https://api.nylas.com${uri}`,
method: 'POST',
json: body
};

if (token)
params.auth = { user: token };

console.log('This line appears. All params are valid.')
return new Promise((resolve, reject) => request(params, (error, res, body) => {
console.log('This line never shows up');
// some code
}));
}

我使用request 2.88.0并且我没有更改我使用的版本。

最佳答案

我假设您的问题出在这一行:

.reply(500, (uri, body) => expect(body).to.eql(''))

Nock 将使用该函数的返回值作为虚假响应的正文。当 nock 试图确定响应主体是否可流式传输时,从函数返回 chai 期望似乎会让 nock 感到困惑。

我会把它改成这样:

nock(NYLAS_API)
.post(`/a/${DN_NYLAS_APP_ID}/accounts/${user.nylas.account_id}/downgrade`)
.reply(500, (uri, body) => {
expect(body).to.eql('')
return ''
});

关于node.js - 无效的 Chai 属性 : setEncoding on nock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55547576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com