0

I am trying to mock a response for a request that uses query params. I've already used onrequestto in other tests, and it works seamlessly. There is already a similar question to what I am asking, but the answer does not work for me whatever I do :(

The request that my frontend makes to get data from backend is something like

https://fake.com/search?Status=authorised&SortBy=Order

what I do in my tests is:

import {RequestMock} from "testcafe";
const mock = RequestMock()
    .onRequestTo('\/search\?Status=authorised')
    .respond({data:ok}, 200, {'content-type': 'application/json charset=utf-8'})


fixture('Test suite')
    .page('fake.com')
    .requestHooks(mock);

and proceed with my .test. Neither OPTIONS nor GET requests return the data that I am expecting. what am I doing wrong?

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47

1 Answers1

1

onRequestTo Should receive Regex value. So you have to change it to onRequestTo(/\/search\?Status=authorised/).

See https://testcafe.io/documentation/402763/reference/test-api/requestmock/onrequestto

Jonah
  • 614
  • 7
  • 18
  • in the link that you provided the first example is ```.onRequestTo('http://external-service.com/api/')``` why can't I use the same exact match as they use in docs? – Kate Velasquez Jun 09 '21 at 12:20
  • In the example they provide whole URL. In your case, you want to match the request just by the part of the URL. – Jonah Jun 09 '21 at 14:26