#ahsan-khan_api

1 messages ยท Page 1 of 1 (latest)

half forumBOT
#

๐Ÿ‘‹ 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.

ivory jewelBOT
#

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.

sturdy cloud
#

Hi again ๐Ÿ‘‹ do you have more context?

tacit dirge
#

Hi actully error is realted to stripe pls look its stay cannot read property of reterive undefined

sturdy cloud
#

Where it points to your res.json variable?

tacit dirge
#

let me show u

sturdy cloud
#

Please do

tacit dirge
#

// 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(),
};
});

sturdy cloud
#

And the error was pointing to this line?
expect(res.json).not.toHaveBeenCalled(); // No JSON data sent on success

tacit dirge
#

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')

sturdy cloud
#

Wait, are you importing our actual library, or are you mocking it? (I'm not familiar with Jest)

tacit dirge
#

mocking

#

can u provide me any example because i ahve research alot but not find sutiable example

sturdy cloud
#

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

GitHub

stripe-mock is a mock HTTP server that responds like the real Stripe API. It can be used instead of Stripe's testmode to make test suites integrating with Stripe faster and less brittle. - ...

#

I'm also thinking this is a typing issue, since the error seems to be a TypeError.

tacit dirge
#

might be m not sure

sturdy cloud
#

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.

tacit dirge
#

m working with node.js and using jest for unit test

sturdy cloud
#

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?

tacit dirge
#

alreadu have

sturdy cloud
#

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.

tacit dirge
#

jest.mock("stripe", () => {
return jest.fn().mockImplementation(function {
return {
charges: {
create: () => "fake stripe response",
},
};
});
});
write now m trying this

sturdy cloud
#

That seems to be diving into the Jest side of things, which I'm not familiar with.