Urgent.News

What's breaking now, across thousands of outlets.

Tech

How to Mock AWS SDK v3 in Vitest Without Extra Dependencies

You need to mock AWS SDK clients in your tests, but the traditional approach with aws-sdk-client-mock requires special treatment and can be cumbersome.

How to Mock AWS SDK v3 in Vitest Without Extra Dependencies

Mocking the AWS SDK v3 in Vitest without extra dependencies is possible by creating a small fake client constructor and method mock. This approach allows for easy assertion of commands and responses without the need for a separate mocking library like aws-sdk-client-mock. The traditional way involves creating a mock for the client class, implementing responses, and resetting it before each test.

However, this approach may not work well in projects that already use request interceptors or have other dependencies being mocked. By using Vitest's vi.mock function, you can replace the real client constructor with a handwritten mock, preserving the original exports and only swapping the part that talks to the internet. This approach keeps the project's original dependencies and exports intact, making testing more manageable and efficient.

Written by urgent.news from HackerNoon's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at hackernoon.com →

More in Tech

More from Wednesday 21 October →