Skip to content

Commit d896334

Browse files
fix(billing): wrap production NODE_ENV test in try/finally for safe cleanup
1 parent b263e7b commit d896334

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

modules/billing/tests/billing.checkout.unit.tests.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,27 @@ describe('Billing service unit tests:', () => {
124124

125125
test('should reject mismatched hostname in production when config.domain is set', async () => {
126126
const originalEnv = process.env.NODE_ENV;
127-
process.env.NODE_ENV = 'production';
128-
129-
jest.unstable_mockModule('../../../config/index.js', () => ({
130-
default: { stripe: { secretKey: 'sk_test_prodcheck' }, domain: 'https://myapp.com' },
131-
}));
132-
133-
mockSubscriptionRepository.findByOrganization.mockResolvedValue({
134-
stripeCustomerId: 'cus_existing',
135-
});
136-
137-
const mod = await import('../services/billing.service.js');
138-
BillingService = mod.default;
139-
140-
await expect(
141-
BillingService.createCheckout(mockOrganization, 'price_starter_m', 'https://evil.com/success', 'https://myapp.com/cancel'),
142-
).rejects.toThrow('Invalid redirect URL');
143-
144-
process.env.NODE_ENV = originalEnv;
127+
try {
128+
process.env.NODE_ENV = 'production';
129+
130+
jest.unstable_mockModule('../../../config/index.js', () => ({
131+
default: { stripe: { secretKey: 'sk_test_prodcheck' }, domain: 'https://myapp.com' },
132+
}));
133+
134+
mockSubscriptionRepository.findByOrganization.mockResolvedValue({
135+
stripeCustomerId: 'cus_existing',
136+
});
137+
138+
const mod = await import('../services/billing.service.js');
139+
BillingService = mod.default;
140+
141+
await expect(
142+
BillingService.createCheckout(mockOrganization, 'price_starter_m', 'https://evil.com/success', 'https://myapp.com/cancel'),
143+
).rejects.toThrow('Invalid redirect URL');
144+
} finally {
145+
if (originalEnv === undefined) delete process.env.NODE_ENV;
146+
else process.env.NODE_ENV = originalEnv;
147+
}
145148
});
146149

147150
test('should throw when priceId is not in allowed plans', async () => {

0 commit comments

Comments
 (0)