Hone logo
Hone
Problems

Testing with Virtual Modules in Jest (TypeScript)

Virtual modules allow you to mock or replace modules during testing without modifying the actual source files. This is incredibly useful for isolating units of code, controlling dependencies, and simulating different scenarios. This challenge will guide you through implementing virtual modules in Jest using TypeScript to test a component that relies on an external module.

Problem Description

You are tasked with writing a Jest test for a TypeScript component called MyComponent that imports and uses a module named externalModule. externalModule provides a function fetchData which is asynchronous and simulates fetching data from an API. You need to test MyComponent without actually making network requests. You will achieve this by using Jest's mock and createModuleMock features to create a virtual module that replaces externalModule with a mock implementation.

What needs to be achieved:

  1. Create a mock implementation of externalModule.fetchData that resolves with a predefined value.
  2. Use createModuleMock to replace the actual externalModule with your mock during the test.
  3. Assert that MyComponent correctly uses the mocked fetchData function and handles the returned data.

Key Requirements:

  • The test should not make any actual network requests.
  • The mock fetchData function should be called within the component.
  • The component should render correctly based on the mocked data.
  • The test should be written in TypeScript.

Expected Behavior:

The test should pass if the component correctly utilizes the mocked fetchData function and renders based on the mocked data. The test should fail if the component attempts to call the real fetchData or if it doesn't render correctly with the mocked data.

Edge Cases to Consider:

  • Ensure the mock fetchData function is properly mocked to resolve with a predictable value.
  • Consider how the component handles potential errors from fetchData (although this challenge focuses on the success case).

Examples

Example 1:

Input: MyComponent imports externalModule.fetchData and uses it to display data.
Output: The test passes, verifying that MyComponent uses the mocked fetchData and renders correctly with the mocked data.
Explanation: The virtual module replaces externalModule, and the test asserts that fetchData is called and the component renders with the expected data.

Example 2:

Input: MyComponent attempts to call the real externalModule.fetchData.
Output: The test fails, indicating that the virtual module was not correctly applied.
Explanation: The test should prevent the component from accessing the real externalModule.

Constraints

  • The fetchData function in externalModule is asynchronous and returns a Promise.
  • You must use Jest's createModuleMock function to create the virtual module.
  • The test should be written in TypeScript.
  • The mocked data should be a simple JavaScript object (e.g., { name: "Test User" }).
  • The component MyComponent is already defined and you should only focus on the testing part.

Notes

  • Think about how to isolate the component under test by replacing its dependencies.
  • createModuleMock allows you to define a module that Jest will use instead of the real module during testing.
  • Consider using jest.mock as an alternative, but createModuleMock provides more control in this scenario.
  • Focus on verifying that the component uses the mocked function, not on the internal implementation of the component itself.
  • The externalModule is assumed to be a standard CommonJS or ES module.
Loading editor...
typescript