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

Unit Test: How to test ajax calls #3842

Closed
couchcrew-thomas opened this issue May 21, 2019 · 1 comment
Closed

Unit Test: How to test ajax calls #3842

couchcrew-thomas opened this issue May 21, 2019 · 1 comment
Labels

Comments

@couchcrew-thomas
Copy link
Contributor

Question: How to test calls to src/ajax in unit tests?

Description

I want to test that calls to src/ajax are called with the correct parameters for my custom adapter. The ajax request is used in the onTimeout and onBidWon callback. Because the ajax method is an exported function, it is not possible to stub it with sinon.

Example Adapter Spec

export const spec = {
  /* ... */
  onTimeout: function (timeoutData) {
    ajax('http://localhost:3000/onTimeout', null, JSON.stringify(timeoutData), {
      withCredentials: true,
      method: 'POST',
      contentType: 'application/json'
    })
  }
};

Intended Unit Test

import * as xhr from '../../../src/ajax';

/* ... */
let stub = sinon.stub(xhr, "ajax");
spec.onTimeout(something);
expect(stub.called).to.be.true;

But the above does not work because sinon cannot spy on exported functions.

@stale
Copy link

stale bot commented Jun 4, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant