在测试中调用承诺在 NodeJS 中出现错误 400

Calling a promise in a test get error 400 in NodeJS

我正在尝试使用 Contentful,这是一个用于构建静态网站的新 JS 库。我想在 Node JS 中使用它。

我创建了一个这样的应用程序文件(名称是 getContent.js):

'use strict';

var contentful = require('contentful')


var client = contentful.createClient({
    space: '****',
    accessToken: '****' 
  });

module.exports = client;

function getEntry() {
  return client.getEntry('******')
  .then(function (entry) {
    // logs the entry metadata
    console.log(entry.sys)

    // logs the field with ID title
    console.log(entry.fields.channelName)
  })
}

然后我创建了一个这样的测试(getContent.test.js):

'use strict';

let chai = require('chai');
let should = chai.should();
var expect = chai.expect;

var rewire = require("rewire");
let getContent = rewire("./getContent.js");

describe('Retreive existing', () => {
  it('it should succeed', (done) => {
    getContent.getEntry({contentName:'****'
    }, undefined, (err, result) => {
      try {
        expect(err).to.not.exist;
        expect(result).to.exist;
        // res.body.sould be equal
        done();
      } catch (error) {
        done(error);
      }
    });
  });
});

但我得到这个错误:

Retreive existing (node:42572) UnhandledPromiseRejectionWarning: Error: Request failed with status code 400 at createError (/Users/ire/Projects/SZDEMUX_GDPR/api/node_modules/contentful/dist/contentful.node.js:886:15) at settle (/Users/ire/Projects/SZDEMUX_GDPR/api/node_modules/contentful/dist/contentful.node.js:1049:12) at IncomingMessage.handleStreamEnd (/Users/ire/Projects/SZDEMUX_GDPR/api/node_modules/contentful/dist/contentful.node.js:294:11) at emitNone (events.js:111:20) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1064:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9) (node:42572) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3) (node:42572) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

你知道我错过了什么吗?承诺没问题,我已经用一个简单的 node getContent.js

测试过了

我发现您的代码存在一些问题:

1.无效参数

在您的测试函数中,在 getContent.js 中,您将参数传递给 getEntry 方法 (return client.getEntry('******'),而您在测试中传递一个对象 (getContent.getEntry({}))

2。混合承诺和回调

it('it should succeed', (done) => {
  getContent.getEntry("****")
    .then((result) => {
      console.log(result);

      try {
        expect(result).to.exist;
        // res.body.sould be equal
        done();
      } catch (error) {
        done(error);
      }
    })
    .catch(error => {
      console.log(error);
      done(error)
    })
});

3。 Unhandled Promise 拒绝的来源不明确:

它是来自您在 getContent.js 中的测试功能,还是来自您的实际测试?

可能,这也可能来自,

expect(err).to.not.exist;
expect(result).to.exist;

始终捕获 Promise 中的错误并以适当的理由拒绝它以避免此类问题。

能否请您更新您的代码并重新发布,以便其他用户清楚?