-
-
Save airandopal/b6c92033536143a73db1415528cb24d8 to your computer and use it in GitHub Desktop.
Cypress / Sinon Stub Help
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const fakeLibAsync = { | |
customers: { | |
create: async (customerDetails) => { | |
const setTimeoutPromise = timeout => { | |
return new Promise(resolve => setTimeout(resolve, timeout)) | |
} | |
await setTimeoutPromise(1200) | |
return { id: 2 } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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