question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow package to be mocked for testing

See original GitHub issue

We would like to be able to mock out loadStripe during our unit tests, which run in a restricted, containerized CI/CD environment; however, simply importing loadStripe has side effects because stripePromise is created immediately when it’s imported. The TypeScript doc recommends against side-effects in modules, and in this case it makes testing a pain because loadStripe cannot be mocked out. Plus it’s unexpected: Intuitively, loadStripe, sounds like a function/action that loads the stripe script, when in fact it’s a wrapper around Promise instance.

Unit testing would be simplified if loadStripe were a mockable function that loaded the stripe script on demand.

As a side question, using the current version of stripe (1.3.0), what is the proposed way to unit test code that depends on Stripe.js without making HTTP and XHR requests? How can this be mocked out?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
benbottocommented, Apr 9, 2020

No problem on the late response. This was actually a deal breaker for us so we moved away from using this module.

“[…] by design to enable Stripe’s fraud prevention.” Sure, I understand the reason, but recommending that users either insert a <script> tag or call loadStripe() in top-level code (e.g. in a root module) would make the code testable. The side-effects are, IMHO, overly protective and make dependent code un-testable (or at best, difficult to test).

“Is there a reason you cannot mock the entire @stripe/stripe-js module […]?” Yes. Namely because that would involve hooking into the module loading system or something equally duck-punch quail-kick hacky. FWIW we’re using Jasmine, which is the default for Angular.

At a minimum, it would be nice if the documentation provided a suggesting for testing code that depends on Stripe.js without making HTTP/XHR requests, as that may not be possible in a CI/CD-type environment.

As an aside, while I appreciate the libraries and robust documentation that Stripe offers, this integration has been abrasive. There’s no clear path for Angular developers to follow, and as a framework that puts testing in the forefront, this module’s side-effects break the paradigm of injectable, mockable services. In my opinion, that’s a driving reason for using one of the two external Angular libraries for Stripe–ngx-stripe and stripe-angular–or rolling your own (which my team did). Neither of those aforementioned modules uses @stripe/stripe-js, by the way; rather, both opt to use the external types, @types/stripe-v3, and load Stripe manually, which is testable.

0reactions
hofman-stripecommented, Jun 9, 2020

Hopefully the pure entrypoint solved the problem. Please re-open if there are still issues mocking the package.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock functions in a package. — with_mock • testthat
Mocking allows you to temporary replace the implementation of functions within a package, which useful for testing code that relies on functions that...
Read more >
Using Mock to test package builds - Fedora Project Wiki
Mock takes a srpm and builds it in a chroot. This ensures that your BuildRequires lines are correct, that there are no missing...
Read more >
How to mock a package method in Go? - Stack Overflow
Use an interface and embed it within the struct. When client uses it initialize with the actual method and for test mock it....
Read more >
Mocking is catching - R-hub blog
In this blog post we shall offer a round-up of resources around mocking, or not mocking, when unit testing an R package.
Read more >
5 Mocking Techniques for Go | Golang Mocking for Unit Testing
Use when you need to mock some package level function. Consider this source code that you want to test. It opens a DB...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found