Hone logo
Hone
Problems

Provider Verification Testing with Jest

Ensuring the correct provider is selected and utilized within your application is crucial for reliability and security. This challenge focuses on writing Jest tests to verify that a function correctly identifies and uses the appropriate provider based on a given configuration. You'll be testing a function that dynamically selects a provider based on a configuration object.

Problem Description

You are given a function getProvider that takes a configuration object as input. This configuration object contains a providerType property, which can be either "aws", "azure", or "gcp". The getProvider function should return a different provider object based on the providerType. You need to write Jest tests to verify that getProvider returns the correct provider object for each valid providerType and handles invalid providerType values gracefully.

Key Requirements:

  • The getProvider function should return an object with a name property corresponding to the provider type (e.g., "aws" for AWS provider).
  • The getProvider function should throw an error if the providerType is not "aws", "azure", or "gcp".
  • Your tests should cover all valid provider types and the invalid provider type case.
  • Tests should use expect assertions to verify the returned provider object and error handling.

Expected Behavior:

  • When providerType is "aws", getProvider should return { name: "aws" }.
  • When providerType is "azure", getProvider should return { name: "azure" }.
  • When providerType is "gcp", getProvider should return { name: "gcp" }.
  • When providerType is anything else, getProvider should throw an error with a descriptive message.

Edge Cases to Consider:

  • Empty configuration object.
  • providerType being null or undefined.
  • providerType being a case-sensitive string (e.g., "AWS" should not be accepted).
  • Invalid data types for providerType (e.g., a number).

Examples

Example 1:

Input: { providerType: "aws" }
Output: { name: "aws" }
Explanation: The function correctly identifies the provider type as "aws" and returns the corresponding provider object.

Example 2:

Input: { providerType: "azure" }
Output: { name: "azure" }
Explanation: The function correctly identifies the provider type as "azure" and returns the corresponding provider object.

Example 3:

Input: { providerType: "invalid" }
Output: Error: Invalid provider type. Supported types are: aws, azure, gcp.
Explanation: The function correctly throws an error because "invalid" is not a supported provider type.

Constraints

  • The getProvider function is already defined (provided below). You only need to write the Jest tests.
  • You must use Jest for testing.
  • Tests should be written in TypeScript.
  • The error message should be exactly as specified in the expected behavior.

Notes

Consider using expect.throws to test for error conditions. Think about how to structure your tests to be readable and maintainable. Focus on testing the core functionality of the getProvider function and its error handling.

function getProvider(config: { providerType: string }): { name: string } {
  const providerType = config.providerType;

  if (!providerType) {
    throw new Error("Provider type is required.");
  }

  switch (providerType) {
    case "aws":
      return { name: "aws" };
    case "azure":
      return { name: "azure" };
    case "gcp":
      return { name: "gcp" };
    default:
      throw new Error(`Invalid provider type. Supported types are: aws, azure, gcp.`);
  }
}
Loading editor...
typescript