Hone logo
Hone
Problems

Implementing a Custom transform Function for Jest

This challenge focuses on extending Jest's capabilities by implementing a custom transformer. Jest transformers allow you to pre-process files before they are executed by Jest, enabling support for various languages and syntaxes. This exercise will help you understand how Jest handles file transformations and how to integrate custom logic.

Problem Description

Your task is to create a Jest transformer that simulates a simple string transformation. This transformer will take a TypeScript file, read its content, and append a specific string to the end of every line within that file. This is a simplified model of what real-world transformers do (e.g., Babel for JavaScript, ts-jest for TypeScript).

Key Requirements:

  1. Create a Transformer Module: You need to create a JavaScript or TypeScript module that exports the necessary functions for Jest to recognize it as a transformer.
  2. Implement process Function: The core of the transformer is the process function. This function will receive the source code of the file and its path.
  3. Line-by-Line Transformation: Your transformer should iterate through each line of the input source code and append a predefined string (e.g., " // transformed") to it.
  4. Return Transformed Code: The process function must return the modified source code.
  5. Support for TypeScript: While the transformation is simple, the transformer should be able to handle .ts files.

Expected Behavior:

Given a .ts file with content:

console.log("Hello");
let x = 10;

Your transformer should output:

console.log("Hello"); // transformed
let x = 10; // transformed

Edge Cases:

  • Empty Files: An empty file should remain empty.
  • Files with only whitespace: Lines consisting solely of whitespace should still have the transformation appended.
  • Files with empty lines: Empty lines should be preserved and have the transformation appended (resulting in " // transformed").

Examples

Example 1:

Input File Content:
console.log("Hello, world!");
const PI = 3.14159;

function add(a: number, b: number): number {
  return a + b;
}
Output Transformed Content:
console.log("Hello, world!"); // transformed
const PI = 3.14159; // transformed

function add(a: number, b: number): number { // transformed
  return a + b; // transformed
} // transformed

Explanation: Each line from the input file has been processed by appending " // transformed" to it.

Example 2:

Input File Content:

    // This is a comment
Output Transformed Content:
 // transformed
    // This is a comment // transformed

Explanation: Even empty lines and lines with leading whitespace are transformed.

Example 3: (Edge Case - Empty File)

Input File Content:
Output Transformed Content:

Explanation: An empty input file results in an empty output.

Constraints

  • The transformer must be implemented in TypeScript.
  • The transformation logic should be relatively simple and focus on string manipulation.
  • The transformer should be compatible with Jest's standard transformer interface.
  • The appended string is fixed: " // transformed".

Notes

  • You will need to configure Jest to use your custom transformer. Refer to the Jest documentation on transformers for guidance.
  • Your transformer module should export at least a process function. You might also need to export getCacheKey for efficient caching, but for this challenge, process is the primary focus.
  • Consider how to handle line endings (e.g., \n, \r\n). The standard string manipulation should handle this correctly.
  • The process function typically receives src: string, filename: string, and config: object. The src is what you need to transform.
  • The output of process should be an object with a code property containing the transformed source code.
Loading editor...
typescript