#jettary_code
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1306603230444392470
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
hi! hmm you seem to say that the issue is spyOn doesn't work, but isn't it just possible that whatever cc() does never calls stripe.subscriptions.update() ? that's what I would check first with logs/breakpoints.
- Stripe Mock Server (stripe/stripe-mock:latest)
- My
Servicewith common functions and stripeClient
class Service {
private _stripeClient: Stripe;
constructor () {}
get stripeClient() {
if (!this._stripeClient) {
throw new Error('Not initialized');
}
return this._stripeClient;
}
/**
* helper function in case test init Stripe() with mock server,
* in live mode - goes to Secret Storage to get secret key, after that init Stripe()
* */
async init() {
if (!this._stripeClient) {
this._stripeClient = await getClient();
}
}
}
export const service = new Service()
- My controller that handles the request
import { service } from '../service';
export async function cancelController(subscriptionId): Promise<boolean> {
await service.init();
const subscription = await service.stripeClient.subscriptions.retrieve(subscriptionId);
if (subscription.status === 'active') {
await service.stripeClient.subscriptions.update(subscriptionId, { cancel_at_period_end: true });
return true;
}
if (subscription.status === 'trialing') {
await service.stripeClient.subscriptions.cancel(subscriptionId);
return true;
}
return false;
}
I've add more context how it looks and what I tried to test
yep. The question remains though, maybe that line "await service.stripeClient.subscriptions.update" never happens, maybe that if-statement's condition is not true. You should check that.
Code above simplified... I take info from lnternal DB and additionaly perform checks like this:
if (status !== 'active' && status !== 'trialing') { return true; }
so state quite assured
hard for me to say, I suppose I just have to trust you.
I don't know if spyOn should work with our library, I can't find any previous examples of people asking about this and it's not something we have resources on
I will try some black js magic and let you know if something will help
let me ask other people on my team for their impressions on this
๐ taking over for my colleague. Let me catch up.
yes spyOn works with the Stripe instance
what errors are you getting?
I think you're not mocking const subscription = await service.stripeClient.subscriptions.retrieve(subscriptionId); this functions
that's why your test is not functioning
did you try and spyOn the retrieve method?
at least you could see if the retrieve was called successfully before passing to the other steps
It kinda strange.... I totally recreate example I shared (previously I just simplified existing code), and make it JS.
And it starts to work ๐ซ
Able to to spy on retrieve, on update, cancel fails as it should (coz mock server always return active status)
Will try to play around with TS config and service code...
are you using ts-jest?
In real project - yes
then it should be fine if you're using the right configuration