Skip to content

Instantly share code, notes, and snippets.

@airandopal
Last active September 21, 2020 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airandopal/b6c92033536143a73db1415528cb24d8 to your computer and use it in GitHub Desktop.
Save airandopal/b6c92033536143a73db1415528cb24d8 to your computer and use it in GitHub Desktop.
Cypress / Sinon Stub Help
I'm using Next.js and getting serverside data using getServerSideProps, and it depends on stripe. The data stripe returns dictates the props passed to the page or will result in a redirect.
When I make a fake function to replace stripe, I can stub that as a dependency just fine. But I cannot seem to stub the stripe call. The imported getServerSideProps function will run and just hang when it's supposed to call the stripe stub. I think it has something to do with the fact that stripe is a constructor? The stripe node library has a note about exporting the Stripe constructor to be used with sinon, but I don't seem to be using it correctly.
I've seen the example with window / new Notification in the Cypress github, and tried to recreate it, but that didn't work.
So, my questions are
- is there a better way to be going about this?
- if not, how do I properly stub a dependency that is a constructor?
References
https://github.com/stripe/stripe-node/blob/c11dc1310902895aa87adf318df9f45bc8fbd83f/lib/stripe.js#L541
https://sinonjs.org/how-to/stub-dependency/
https://github.com/cypress-io/cypress/blob/cee5df601478b3a3bbc6eae557885c6f0c44e9b7/packages/driver/test/cypress/integration/commands/agents_spec.coffee#L39
export const fakeLibAsync = {
customers: {
create: async (customerDetails) => {
const setTimeoutPromise = timeout => {
return new Promise(resolve => setTimeout(resolve, timeout))
}
await setTimeoutPromise(1200)
return { id: 2 }
}
}
}
import { fakeLibAsync } from '../utilities/fakes.js'
....
export const getServerSideProps = async (context) => {
try {
// ..... [when running the stripe test, everything here runs fine]
const options = {
description: 'My First Test Customer (created for API docs)',
}
const customer = await fakeLibAsync.customers.create(options)
// // what is actually used in application
// const customer = await stripe.customers.create(options);
return {
props: {
customerId: customer.id
}
}
} catch (error) {
console.log("error", error)
return {
props: {
error: true
}
}
}
}
const { getServerSideProps: gspCustomer } = require('../../pages/stripe-test-customer.js')
const { fakeLibAsync } = require('../../utilities/fakes.js')
....
it('this works to call with fakeLib ASYNC', async () => {
cy.stub(fakeLibAsync.customers, 'create').resolves({ id: 2 })
const customerProps = await gspCustomer()
expect(customerProps).to.eql({ props: { customerId: 2 }})
})
const { getServerSideProps: gspCustomer } = require('../../pages/stripe-test-customer.js')
// tried many stripe imports here
const stripeModule = require("stripe")
const stripe = require("stripe")('test')
const stripe = require("stripe").Stripe('test')
const stripe = new stripeModule.Stripe()
.....
it('test', async () => {
// tried many stub setups here
const stripe = cy.stub(stripeModule,'Stripe').returns(stripe);
cy.stub(stripe.customers, 'create').resolves({id: stripeCustomerId});
cy.stub(stripe.customers, 'create').returns({id: stripeCustomerId});
cy.stub(stripe.customers, 'create').callsFake(async () => {
const setTimeoutPromise = timeout => {
return new Promise(resolve => setTimeout(resolve, timeout))
}
await setTimeoutPromise(1200)
return { id: 2 }
});
const customerProps = await gspCustomer()
expect(customerProps).to.eql({ props: { customerId: 2 }})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment