Testcafe: Mock HTTP calls and standard API

Created on 27 Feb 2017  路  13Comments  路  Source: DevExpress/testcafe

Are you requesting a feature or reporting a bug?

Feature

What is the current behavior?

Wait for Ajax Request responses.

What is the expected behavior?

Rather waiting for long sometimes as a developer, I want to mock all HTTP calls for faster execution of tests. Something like this (https://github.com/atecarlos/protractor-http-mock) will be very helpful.

How would you reproduce the current behavior (if this is a bug)?

Provide the test code and the tested page URL (if applicable)

Tested page URL:

Test code


Specify your

  • operating system:
  • testcafe version:
  • node.js version:
server Auto-locked API hammerhead enhancement

Most helpful comment

Will be implemented with special [request hook](https://github.com/DevExpress/testcafe/issues/1341 RequestMock. E.g.:

import { RequestMock } from 'testcafe';

const mock = new RequestMock();

mock
      .onRequest('http://example.com/*') 
      .filter({ method: 'POST', isAjax: true })
      .respond({
           status: 200,
           body: 'test'
      });

fixture `Fixture`
    .useRequestHook(mock);

test('My test', () => {
    //...
})

All 13 comments

Will be implemented with special [request hook](https://github.com/DevExpress/testcafe/issues/1341 RequestMock. E.g.:

import { RequestMock } from 'testcafe';

const mock = new RequestMock();

mock
      .onRequest('http://example.com/*') 
      .filter({ method: 'POST', isAjax: true })
      .respond({
           status: 200,
           body: 'test'
      });

fixture `Fixture`
    .useRequestHook(mock);

test('My test', () => {
    //...
})

Looks great.

How granular mock can become? is It possible to achieve following?

      .onRequest('http://example.com/*') 
      .filter({ method: 'POST', isAjax: true, data: {name: 'adi'} })
      .respond({
           status: 200,
           body: 'test1'
      });
     .onRequest('http://example.com/*') 
      .filter({ method: 'POST', isAjax: true, data: {name: 'jack'} })
      .respond({
           status: 200,
           body: 'test2'
      });
      .onRequest('http://example.com/*') 
      .filter({ method: 'GET', isAjax: true, params: {name: 'jack2'} })
      .respond({
           status: 200,
           body: 'test3'
      });
     .onRequest('http://example.com/*') 
      .filter({ method: 'GET', isAjax: true, params: {name: 'jack2', job: 'programmer'} })
      .respond({
           status: 200,
           body: 'test4'
      });

@thecodejack Sure

Hey guys, any update regarding this? or is there a current recommended workaround to mock responses?

Hi @tombertrand,

This feature is our short-list but now we can't say when exactly it'll be implemented. I suppose it'll be one of the next few releases.
As a workaround you can use nock. See our discussion.

Thanks for the quick reply, I'll give it a try!

Any updates on this? I use testcafe on a project and mocking would be really useful.

@AlexanderMoskovkin does that work only with "node.js driven" tests?

Because I'm trying using it with webpack proxy but it doesn't work.

I have webpack's proxy configured this way:

{
  proxy: {
      '/api': {
        target: 'http://localhost:5000',
      },
  }
}

which enables webpack dev server to talk to the backend running on port 5000.

I tried like this:

import { Selector } from 'testcafe'
import nock from 'nock'

import config from '../../../config'

const data = {
  results: [{foo: 'bar'}],
  total: 1,
}

fixture('Result List')
  .beforeEach(() => {
    nock('http://localhost:5000')
      .get(/api$/)
      .reply(200, data)
  })
  .page(`http://localhost:${config.dev.port}/`)


test('Click "Create info"', async (t) => {
  const createLink = await Selector('#create')

  await t
    .click(createLink)
    .expect(Selector('h1').innerText).eql('Create info')
})

Hi @rhymes ,

Actually I haven't checked this case, so it requires some time to try it. If you have some results before please let us know.
Meanwhile we plan to implement built-in request hooks in the next release (after the current one)

@rhymes
Could you please provide a full example that I can run on my computer without writing additional code?
It will allow me to find the reason of the problem quickly.

@miherlosev not really sorry, it was just an experiment. I used a webpack config with the proxy activated and sent a request to a backend.

Done. For more information see the https://github.com/DevExpress/testcafe/issues/1341#issuecomment-386626497

This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests. We recommend you ask TestCafe API, usage and configuration inquiries on StackOverflow.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

madroneropaulo picture madroneropaulo  路  3Comments

calisven picture calisven  路  3Comments

Lukas-Kullmann picture Lukas-Kullmann  路  3Comments

KaneMorgan picture KaneMorgan  路  3Comments

inikulin picture inikulin  路  3Comments