Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RequestMock] fetch call with mock is blocking test #2534

Closed
davidcunha opened this issue Jun 19, 2018 · 5 comments
Closed

[RequestMock] fetch call with mock is blocking test #2534

davidcunha opened this issue Jun 19, 2018 · 5 comments
Assignees
Labels
STATE: Auto-locked An issue has been automatically locked by the Lock bot. SYSTEM: hammerhead TYPE: enhancement The accepted proposal for future implementation.
Milestone

Comments

@davidcunha
Copy link

davidcunha commented Jun 19, 2018

I'm testing to mock a remote API endpoint (http://remote-api.com/api/customers/1) that is being called with "fetch" when opening a page http://localhost:3000/account.
I'm using Next.js 6.0.3 with React 16.2.0 and isomorphic-fetch 2.2.1:

pages/login.js

import React, { Component } from 'react';
import fetch from 'isomorphic-fetch';

function getCustomer(customerId) {
  return fetch(`http://remote-api.com/api/customers/${customerId}`, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  })
    .then(res => res.json())
    .catch(error => error);
}

class Account extends Component {
  static async getInitialProps(ctx) {
    const customerId = 1;
    const customer = await getCustomer(customerId);

    return {
      customer,
    };
  }

  render() {
    const { customer } = this.props;
    return (
      <div>
        {customer ? (
          <div>
            <h1>Hi, {customer.name}</h1>
          </div>
        ) : null}
      </div>
    );
  }

Tested page URL: http://localhost:3000/account

Test code

const mock = RequestMock()
  .onRequestTo('http://remote-api.com/api/customers/1')
  .respond({
    id: 1,
    name: "John Doe"
  }, 200, {'Access-Control-Allow-Origin': '*'});

fixture('As a customer I want to be able to view my profile')
  .page('http://localhost:3000/account')
  .requestHooks(mock);

test('View my profile with success', async (t) => {
  await t.expect(Selector('h1').innerText).eql('John Doe');
});

When I run this test the function call "getCustomer" in "Account" class, which in turn calls a simple fetch and return the promise with the json content from a remote API (CORS), blocks the test at that specific line and no request is made and intercepted. However if I remove the mock, the test is "green".

I suppose this must be related with RequestMock + server-side rendering as in Next.js documentation the following description is mentioned:

"For the initial page load, getInitialProps will execute on the server only. getInitialProps will only be executed on the client when navigating to a different route via the Link component or using the routing APIs."

operating system: Mac OS X 10.13.4
testcafe version: 0.20.0
node.js version: 8.11.1

@miherlosev miherlosev added the STATE: Need clarification An issue lacks information for further research. label Jun 20, 2018
@Farfurix
Copy link
Contributor

I'm working on it

@Farfurix
Copy link
Contributor

Hello, @davidcunha.
While we are working on this problem, you can use the following workaround:

  .respond({
    id: 1,
    name: "John Doe"
-  }, 200, {'Access-Control-Allow-Origin': '*'});
+  }, 200, {'access-control-allow-origin': '*'});

@Farfurix Farfurix added TYPE: bug The described behavior is considered as wrong (bug). SYSTEM: hammerhead and removed STATE: Need clarification An issue lacks information for further research. labels Jun 22, 2018
@miherlosev miherlosev added TYPE: enhancement The accepted proposal for future implementation. and removed TYPE: bug The described behavior is considered as wrong (bug). labels Jun 22, 2018
@miherlosev miherlosev added this to the Sprint #13 milestone Jun 22, 2018
@davidcunha
Copy link
Author

@Farfurix Thanks, the workaround really worked. 😎

@AndreyBelym
Copy link
Contributor

Hi @davidcunha, I've just published [email protected] with the fix! Please, update your TestCafe installation, and feel free to reopen the issue if the problem isn't fixed in your environment.

@lock
Copy link

lock bot commented Mar 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or feature requests. For TestCafe API, usage and configuration inquiries, we recommend asking them on StackOverflow.

@lock lock bot added the STATE: Auto-locked An issue has been automatically locked by the Lock bot. label Mar 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 28, 2019
kirovboris pushed a commit to kirovboris/testcafe-phoenix that referenced this issue Dec 18, 2019
…Express#2588)

* update hammerhead

* fix tests after hammerhead update
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
STATE: Auto-locked An issue has been automatically locked by the Lock bot. SYSTEM: hammerhead TYPE: enhancement The accepted proposal for future implementation.
Projects
None yet
Development

No branches or pull requests

4 participants