#ahsan-khan_testing
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/1237397870571946034
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Hi
hi @worn ridge
I'm going to write unit tests for my developed function that uses the Stripe service in Node.js, and I'm using the Jest framework for unit testing. However, when I try to mock it, I'm getting an "undefined" error for Stripe.
?
ahsan-khan_testing
// 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
});
this is my unit test
And error i get
export const CancelSubscriptionAPI = async (req, res) => {
const { sub, subscriptionId } = req.body;
try {
const subs= await stripe.subscriptions.retrieve(subscriptionId);
return res.status(200);
} catch (err) {
return res.status(402).json({
status: "FAILED",
error: err.message,
});
}
};
This is my function need to be test.
Hi there ๐ taking a look