Как решить UnhandledPromiseRejectionWarning в NodeJS инфраструктуре тестирования - PullRequest
0 голосов
/ 28 апреля 2020

Я использую пользовательские команды в моей среде тестирования на основе Nightwatch.js. Я хочу сделать запрос PUT через superagent. Это моя собственная команда:

const superagent = require("superagent");

exports.command = function(url, header, body, callback) {
  return superagent
    .put(url)
    .send(body) // sends a JSON post body
    .set(header)
    .then(result => {
      callback(true, result);
      return this;
    })
    .catch(err => {
      if (err) {
        callback(false, err);
        return this;
      }
    });
};

И эту команду я использую в своих тестах:

return client
  .apiPut(apiUrl, header, body, function(status, result) {
    assert.isTrue(status, "response status is false");
    assert.isTrue(result.ok, "response result is NOT ok");
  })

Если все в порядке с запросом PUT (status == true и успешным ответом) ) тогда все в порядке, и тест завершится sh, но если запрос PUT не выполнен (status != true и ошибка как результат), я получаю следующее сообщение об ошибке, и текущий процесс узла не завершается sh:

09:11:12 (node:103) UnhandledPromiseRejectionWarning: AssertionError: response status is false: expected false to be true
09:11:12     at /var/jenkins_home/jobs/MYJOB/workspace/testautomation/end2end-web-tests/pageobjects/MyPageView.js:59:20
09:11:12     at superagent.put.send.set.then.catch.err (/var/jenkins_home/jobs/MYJOB/workspace/testautomation/end2end-web-tests/commands/apiPut.js:14:9)
09:11:12     at <anonymous>
09:11:12     at process._tickCallback (internal/process/next_tick.js:189:7)
09:11:12 (node:103) 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: 2)
09:11:12 (node:103) [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.

Но я не знаю, что я делаю неправильно с обратным вызовом в моей пользовательской команде. Что я должен сделать, чтобы решить эту проблему?

1 Ответ

0 голосов
/ 04 мая 2020

Спасибо за помощь. Теперь он работает. Я заменил обещание и полностью заменил его обратными вызовами. Сейчас работает:)

const superagent = require("superagent");

exports.command = function(url, header, body, callback) {
  return superagent
    .put(url)
    .send(body) // sends a JSON post body
    .set(header)
    .end((error, result) => {
      if (error) {
        callback(false, result, error);
      } else {
        callback(true, result, error);
      }
    });
};
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...