jest-raw-loader
raw JSON → 1.0.1 verified Sat Apr 25 auth: no javascript maintenance
Jest transformer that imports files as raw strings, mimicking webpack-contrib/raw-loader for testing. Current stable version 1.0.1 (last release Jul 22, 2018; long-term maintenance unclear). It reads file content and returns it as a module exporting the string. Simple configuration via Jest's transform option. Unlike other loaders, it directly replaces file imports with their raw content, useful for testing components that import .graphql, .md, or .txt files. No updates since 2018, so may not support newer Jest versions.
Common errors
error Cannot find module 'jest-raw-loader' ↓
cause Package not installed or not in devDependencies.
fix
Run 'npm install --save-dev jest-raw-loader'
error TypeError: Transformer is not a function ↓
cause Jest version incompatible (Jest 27+ expects async transformer).
fix
Upgrade to a compatible transformer or downgrade Jest to <=26.
error Invalid regular expression: /\\.graphql/: \ at end of pattern ↓
cause Mis-escaped regex in JSON configuration.
fix
Use double backslashes: "\\.graphql$"
Warnings
breaking jest-raw-loader may not work with Jest 27+ due to changes in transformer API (sync vs async). ↓
fix Use a more maintained transformer like jest-transform-raw, or configure Jest custom transformer manually.
deprecated Package has not been updated since 2018; consider it in maintenance mode. ↓
fix Evaluate alternatives if you need active maintenance or newer Jest support.
gotcha Transform configuration uses regex patterns, not globs. Backslashes in JSON must be double-escaped. ↓
fix Use "\\.graphql$" (escaped backslash and dot) instead of ".graphql".
Install
npm install jest-raw-loader yarn add jest-raw-loader pnpm add jest-raw-loader Imports
- jest-raw-loader wrong
const rawLoader = require('jest-raw-loader')correct// package used as Jest transformer in config, not directly imported - jest.transform wrong
"jest": { "transform": { "\\.graphql$": "jest-raw-loader/index.js" } }correct"jest": { "transform": { "\\.graphql$": "jest-raw-loader" } } - Raw file import wrong
const content = require('./example.graphql'); // works but CJScorrectimport content from './example.graphql'; // content is a string
Quickstart
// package.json
{
"jest": {
"transform": {
"\\.graphql$": "jest-raw-loader",
"\\.md$": "jest-raw-loader"
}
},
"devDependencies": {
"jest-raw-loader": "^1.0.1"
}
}
// example.test.js
import graphqlContent from './schema.graphql';
test('returns raw string', () => {
expect(typeof graphqlContent).toBe('string');
expect(graphqlContent).toContain('type Query');
});