Currently, I am creating a test for a method within NestJs that is responsible for initiating a Paypal Payment intent. When I execute either the yarn test:watch
or simply yarn test
command, the test described below runs successfully and passes. However, upon completion of the test, an error is thrown as shown below.
TEST
describe("PaymentsService", () => {
let paymentsService: PaymentsService
let paymentRepository
let ordersService
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
StripeModule.forRoot({
apiKey: "stripe.apiKey",
apiVersion: "2020-08-27",
}),
],
providers: [
PaymentsService,
{ provide: PaymentRepository, useFactory: mockPaymentRepository },
{ provide: UsersService, useFactory: mockUsersService },
{ provide: OrdersService, useFactory: mockOrdersService },
{
provide: Stripe,
useFactory: () => ({
checkout: {
sessions: {
create: jest.fn(),
},
},
}),
},
],
}).compile()
paymentsService = module.get<PaymentsService>(PaymentsService)
paymentRepository = module.get<PaymentRepository>(PaymentRepository)
ordersService = module.get<OrdersService>(OrdersService)
})
describe("initiatePaypalPayment", () => {
it("will initiate paypal payment", async () => {
ordersService.getOrder.mockResolvedValue(mockOrder)
ordersService.calculateOrderPrice.mockResolvedValue(mockOrder.price)
jest.doMock("paypal-rest-sdk", () => {
return jest.fn(() => ({
payment: {
create: jest.fn(() => Promise.resolve({})),
},
}))
})
expect(
await paymentsService.initiatePaypalPayment(
{ orderId: 10, voucherCode: "string" },
mockedResponse
)
).toEqual(undefined)
})
})
})
TEST RESULTS
PASS src/payments/payments.service.spec.ts
PaymentsService
initiatePaypalPayment
✓ will initiate paypal payment (117ms)
console.log src/payments/payments.service.ts:72
initiatePaypalPayment
console.log src/payments/payments.service.ts:86
totalPrice: 1000
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 4.926s, estimated 6s
Ran all test suites related to changed files.
Watch Usage
› Press a to run all tests.
› Press f to run only failed tests.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Error in paypap Error: Response Status : 401
at IncomingMessage.<anonymous> (/home/user/Desktop/app/node_modules/paypal-rest-sdk/lib/client.js:130:23)
at IncomingMessage.emit (events.js:326:22)
at IncomingMessage.EventEmitter.emit (domain.js:483:12)
at endReadableNT (_stream_readable.js:1241:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
response: {
error: 'invalid_client',
error_description: 'Client Authentication failed',
httpStatusCode: 401
},
httpStatusCode: 401
}".
91 | payment_method: 'paypal',
92 | },
> 93 | redirect_urls: {
| ^
94 | return_url: 'www.example.com',
95 | cancel_url: 'www.example.com',
96 | },
at IncomingMessage.<anonymous> (../node_modules/paypal-rest-sdk/lib/client.js:130:23)
at processTicksAndRejections (../internal/process/task_queues.js:84:21) {
response: {
error: 'invalid_client',
error_description: 'Client Authentication failed',
httpStatusCode: 401
},
httpStatusCode: 401
}".
at console.log (../node_modules/@jest/console/build/CustomConsole.js:183:10)
at payments/payments.service.ts:93:25
at ../node_modules/paypal-rest-sdk/lib/api.js:102:13
at ../node_modules/paypal-rest-sdk/lib/api.js:87:9
at IncomingMessage.<anonymous> (../node_modules/paypal-rest-sdk/lib/client.js:140:13)
/home/user/Desktop/app/src/payments/payments.service.ts:94
throw new common_1.InternalServerErrorException();
^
InternalServerErrorException: Internal Server Error
at /home/user/Desktop/app/src/payments/payments.service.ts:115:15
at /home/user/Desktop/app/node_modules/paypal-rest-sdk/lib/api.js:102:13
at /home/user/Desktop/app/node_modules/paypal-rest-sdk/lib/api.js:87:9
at IncomingMessage.<anonymous> (/home/user/Desktop/app/node_modules/paypal-rest-sdk/lib/client.js:140:13)
at IncomingMessage.emit (events.js:326:22)
at IncomingMessage.EventEmitter.emit (domain.js:483:12)
at endReadableNT (_stream_readable.js:1241:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
response: { statusCode: 500, message: 'Internal Server Error' },
status: 500
}
error Command failed with exit code 1.
The issue could be related to not correctly mocking the paypal-rest-sdk
, but there may also be other reasons for this error occurrence. Any assistance on this matter would be greatly appreciated.