Jest Text File Transformer

1.0.4 · active · verified Sun Apr 19

jest-text-transformer is a specialized Jest transformer designed to allow developers to directly import the content of text files (`.txt` files) into their Jest test suites. This utility simplifies testing scenarios where file content, rather than module exports, needs to be directly accessible within tests, such as testing parsers, content processors, or mock file system interactions. The package is currently at version 1.0.4. As a focused utility, its release cadence is typically slow, with updates primarily driven by Jest's own API changes or critical bug fixes, rather than frequent feature additions. Its key differentiator lies in its singular focus on plain text files, providing a straightforward solution without the overhead of more general asset transformers that handle various file types like images or CSS. It integrates seamlessly into Jest's configuration, requiring only a simple entry in the `transform` option of the Jest configuration object in `package.json` or `jest.config.js`. This allows for direct `import` statements for `.txt` files within test files, receiving the raw text content as a string.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure Jest to use `jest-text-transformer` and how to import a `.txt` file directly into a test file, verifying its content.

npm install -D jest-text-transformer

// In your package.json
{
  "jest": {
    "transform": {
      "^.+\.txt$": "jest-text-transformer"
    }
  }
}

// Example test file (my-test.spec.ts)
// Make sure to create a dummy.txt file in the same directory
// with some content, e.g., 'Hello, Jest!'

import content from './dummy.txt';

describe('Text File Transformer', () => {
  it('should load the text file content as a string', () => {
    expect(typeof content).toBe('string');
    expect(content).toBe('Hello, Jest!');
  });

  it('should handle multi-line text files', () => {
    // Create another file, e.g., multi-line.txt:
    // Line 1
    // Line 2
    import multiLineContent from './multi-line.txt';
    expect(multiLineContent).toContain('Line 1');
    expect(multiLineContent).toContain('Line 2');
  });
});

view raw JSON →