#ahsan-khan_api
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/1237412782245413005
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- ahsan-khan_testing, 25 minutes ago, 14 messages
Hi again ๐ do you have more context?
Hi actully error is realted to stripe pls look its stay cannot read property of reterive undefined
Where it points to your res.json variable?
let me show u
Please do
// Import the function to be tested
import { CancelSubscriptionAPI } from '../../controllers/stripe/stripe-subscription';
import dotenv from "dotenv";
dotenv.config();
describe('CancelSubscriptionAPI', () => {
let req;
let res;
beforeEach(() => {
req = {
body: {
sub: 'some_sub_id',
subscriptionId: 'sub_12345',
},
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};
});
afterEach(() => {
jest.clearAllMocks();
});
it('should return status 200 if subscription retrieval is successful', async () => {
// Mock stripe.subscriptions.retrieve
jest.mock('stripe'); // Mock only the subscriptions functionality
const stripe = require('stripe'); // Import the mocked Stripe library
console.log('stripe=', stripe);
const mockSubscription = {
id: 'sub_12345',
customer: 'cus_abc123',
};
(stripe.subscriptions.retrieve as jest.Mock).mockResolvedValue(mockSubscription);
// Call the function
await CancelSubscriptionAPI(req, res);
expect((stripe.subscriptions.retrieve as jest.Mock)).toHaveBeenCalledWith('sub_12345');
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).not.toHaveBeenCalled(); // No JSON data sent on success
});
// Add more test cases as needed
});
In above unit test where we getting res:
// beforeEach(() => {
req = {
body: {
sub: 'some_sub_id',
subscriptionId: 'sub_12345',
},
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};
});
And the error was pointing to this line?
expect(res.json).not.toHaveBeenCalled(); // No JSON data sent on success
let me retunr fresh case without this
// Import the function to be tested
import { CancelSubscriptionAPI } from '../../controllers/stripe/stripe-subscription';
import dotenv from "dotenv";
dotenv.config();
describe('CancelSubscriptionAPI', () => {
let req;
let res;
beforeEach(() => {
req = {
body: {
sub: 'some_sub_id',
subscriptionId: 'sub_12345',
},
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};
});
afterEach(() => {
jest.clearAllMocks();
});
it('should return status 200 if subscription retrieval is successful', async () => {
// Mock stripe.subscriptions.retrieve
jest.mock('stripe'); // Mock only the subscriptions functionality
const stripe = require('stripe'); // Import the mocked Stripe library
console.log('stripe=', stripe);
const mockSubscription = {
id: 'sub_12345',
customer: 'cus_abc123',
};
(stripe.subscriptions.retrieve as jest.Mock).mockResolvedValue(mockSubscription);
expect((stripe.subscriptions.retrieve as jest.Mock)).toHaveBeenCalledWith('sub_12345');
});
// Add more test cases as needed
});
now this is my test case
see the error
TypeError: Cannot read properties of undefined (reading 'retrieve')
Wait, are you importing our actual library, or are you mocking it? (I'm not familiar with Jest)
mocking
can u provide me any example because i ahve research alot but not find sutiable example
If you aren't using our actual library, then I'm likely not familiar with what library you're using and may not be able to provide advice on how to use it. How are you mocking it, did you create your own mock library to pull in? Are you using our prebuilt stripe-mock server?
https://github.com/stripe/stripe-mock
I'm also thinking this is a typing issue, since the error seems to be a TypeError.
might be m not sure
I'd recommend getting that clarity. It's going to be hard for my teammates or I to provide any guidance if you can't tell us what you're working with.
m working with node.js and using jest for unit test
How are the Stripe portions handled? Are you using a library to mock those requests, is that our library? In your code I see you have this line:
const stripe = require('stripe'); // Import the mocked Stripe library
is that pulling in the full stripe library, or is that pointing to some other library within your environment?
alreadu have
I know you have it, I pulled it from the code you shared, I'm asking what it does in your code
If you're pulling in our actual Stripe library, then this doesn't seem like a mock scenario at all. Which is why I'm still trying to understand what you're doing here.
jest.mock("stripe", () => {
return jest.fn().mockImplementation(function {
return {
charges: {
create: () => "fake stripe response",
},
};
});
});
write now m trying this
That seems to be diving into the Jest side of things, which I'm not familiar with.