Мокко перед крючком не запускается перед свитой - PullRequest
0 голосов
/ 19 декабря 2018

Я новичок в Node и Mocha, и мне трудно понять, почему Mocha пропускает мой код перехвата "до", чтобы создать токен Json Web в следующем примере:

//index.js
const createJWT = require('./lib/config/createJWT');
const expect = require('chai').expect;

before(async() => {

  const jwt = await createJWT() {
    return new Promise(
      function(resolve, reject) {
        resolve(jwt);
      }
    )
  }
  const person = createPerson(jwt);

}); //this is where the jwt is created, making it useless for createPerson

describe('My other tests', () => {
it('Customer sign up', async() => {
  const signUpText = await customerSignUp(page, frame);

  expect(signUpText).to.equal("You have signed up")

  });
 });
});

Метод createJWT () выглядит следующим образом:

 //createJWT.js
module.exports = async() => {

  const options = {
    method: 'POST',
    url: 'https://my-website.auth.io/oauth/token',
    headers: {
      'content-type': 'application/json'
    },
    body: '{"client_id":"dew76dw7e65d7w65d7wde"}'
  };

    request(options, function (error, response, body) {
    try {
        console.log(body);
        jwt = body;
        return jwt = body;
    } catch
        (error) {
    }
  });
};

При отладке код установки пропускается.Есть ли что-то очевидное, что мне не хватает?

1 Ответ

0 голосов
/ 19 декабря 2018

Я почти уверен, что вам нужно поместить ловушку before в тот же тестовый блок, чтобы он запускался до обработки.Пример:

before(async() => {

  const jwt = await createJWT();

});

describe('My other tests', () => {
  it('Customer sign up', async() => {
    const signUpText = await customerSignUp(page, frame);

    expect(signUpText).to.equal("You have signed up")
  });
});

Или:

describe('My other tests', () => {
  before(async() => {

    const jwt = await createJWT();

  });
  it('Customer sign up', async() => {
    const signUpText = await customerSignUp(page, frame);

    expect(signUpText).to.equal("You have signed up")
  });
});

Кроме того, ваш метод createJwt не возвращает Promise, что препятствует работе await.Вам нужно сделать что-то вроде этого:

module.exports = async() => {

  const options = {
    method: 'POST',
    url: 'https://my-website.auth.io/oauth/token',
    headers: {
      'content-type': 'application/json'
    },
    body: '{"client_id":"dew76dw7e65d7w65d7wde"}'
  };

  return new Promise((resolve, reject) => request(options, function (error, response, body) {
    if(error) {
      reject(error);
    }
    resolve(body);
  }));
};
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...