私の人生では、単純なスーパーエージェントの投稿リクエストでノックを機能させることはできません。これが私のスーパーエージェントとノックの両方の設定です。
スーパーエージェント:
request
.post('https://test.com/api/login')
.send({
email: 'test@test.com',
password: 'testpassword'
})
.end((err, res) => {
if (err) {
console.log(err);
}
});
ノック:
nock('https://test.com')
.post('/api/login')
.send({
email: 'test@test.com',
password: 'testpassword'
})
.reply(200, {
id: 1,
token: 'abc'
});
nock から次のエラーが表示されます。
{ [エラー: ノック: リクエスト POST https://test.com/api/login {"email":"test@test.com","password":"testpassword"}] ステータス: 404、statusCode: 404、応答: 未定義}
もう 1 つの奇妙な点は、このエラーがスーパーエージェントの応答ハンドラーに記録されていることです。したがって、通話が行われ、傍受されていることがわかります。