assaf / zombie

Insanely fast, full-stack, headless browser testing using node.js
http://zombie.js.org/
MIT License
5.65k stars 518 forks source link

async call with React/Redux/Axios fails #1191

Open lucasstarick opened 5 years ago

lucasstarick commented 5 years ago

I am trying to implement a test with zombie version 6.1.4 in a react js application.

The application at the time of login makes a request for an external API through the axios and if the login is ok the user is redirected to the administrative area.

My test is failing when the user is invalid and the API returns me the status of the 401 request.

H:\newVersionProject\mi-web-frontend\node_modules\react-scripts\scripts\test.js:20
  throw err;
  ^
StatusCodeError: 401 - "{\n    \"message\": \"Invalid credentials\"\n}\n"
    at new StatusCodeError (H:\newVersionProject\mi-web-frontend\node_modules\request-promise-core\lib\errors.js:32:15)
    at Request.plumbing.callback (H:\newVersionProject\mi-web-frontend\node_modules\request-promise-core\lib\plumbing.js:104:33)
    at Request.RP$callback [as _callback] (H:\newVersionProject\mi-web-frontend\node_modules\request-promise-core\lib\plumbing.js:46:31)
    at Request.self.callback (H:\newVersionProject\mi-web-frontend\node_modules\request\request.js:185:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (H:\newVersionProject\mi-web-frontend\node_modules\request\request.js:1161:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (H:\newVersionProject\mi-web-frontend\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:313:30)
    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:139:11)
    at process._tickCallback (internal/process/next_tick.js:181:9)
npm ERR! Test failed.  See above for more details.

Below the test code:

/* eslint-disable */
const assert = require('assert');
const Browser = require('zombie');

describe('Funcionalidade de Autenticação', function () {
  var originalTimeout;

  originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
  jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;

  const browser = new Browser();

  describe('Acesso Usuário Inexistente', function () {
    it('Login', async function(){

      await browser.visit('http://localhost:3000/#/').
            then(() => browser.fill("input[name=email]", 'user1554@mi.me')).
            then(() => browser.fill('input[name=password]', '12345678')).
            then(() => browser.pressButton('Realizar Login'));
    });
  });

  afterAll(function () {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;

    browser.destroy();
  });
});

When the user exists the test works normally

Thank you