lundi 7 mai 2018

Calling a promise in a test get error 400 in NodeJS

I'm trying to use Contentful, a new JS library for building static websites. I want to use it in Node JS.

I created an app file like this (the name is 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)
  })
}

Then I created a test (getContent.test.js) like this:

'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({
    }, undefined, (err, result) => {
      try {
        expect(err).to.not.exist;
        expect(result).to.exist;
        // res.body.sould be equal
        done();
      } catch (error) {
        done(error);
      }
    });
  });
});

but I obtain this 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.

Do you know what I'm missing? the promise is ok, I already tested it with a simple node getContent.js

Aucun commentaire:

Enregistrer un commentaire