code-docs/utils/jest-yaml-transform.md
Jest transformer for YAML file imports.
Enables importing YAML files directly in Jest tests:
import schema from './schema.yaml';
import examples from './examples.yaml';
Add to Jest configuration:
// jest.config.js
module.exports = {
transform: {
'\\.yaml$': '@lowdefy/jest-yaml-transform',
},
};
.yaml importconst transformer = {
getCacheKey(fileData, filePath, options),
process(sourceText)
};
Transform YAML source to JavaScript module:
// Input (schema.yaml):
// type: object
// properties:
// name:
// type: string
// Output (JavaScript module):
module.exports = {
type: 'object',
properties: {
name: { type: 'string' },
},
};
Generate cache key for Jest caching:
// Uses SHA1 hash of:
// - File content
// - Transformer options
// Returns unique cache key
yaml (2.3.4)@swc/core (for transpilation)// examples.yaml
// - id: basic
// type: Button
// properties:
// label: Click
import examples from './examples.yaml';
describe('Button', () => {
examples.forEach((example) => {
test(`renders ${example.id}`, () => {
// Test each example
});
});
});
// schema.yaml
// type: object
// properties:
// label:
// type: string
// disabled:
// type: boolean
// default: false
import schema from './schema.yaml';
import { validate } from '@lowdefy/ajv';
test('validates button config', () => {
const result = validate({
schema,
data: { label: 'Click' },
returnErrors: true,
});
expect(result.valid).toBe(true);
});
// fixtures.yaml
// users:
// - id: 1
// name: Alice
// - id: 2
// name: Bob
import fixtures from './fixtures.yaml';
describe('UserList', () => {
test('renders users', () => {
const { getAllByRole } = render(<UserList users={fixtures.users} />);
expect(getAllByRole('listitem')).toHaveLength(2);
});
});
// jest.config.js
module.exports = {
transform: {
'\\.yaml$': '@lowdefy/jest-yaml-transform',
'\\.yml$': '@lowdefy/jest-yaml-transform',
},
moduleFileExtensions: ['js', 'json', 'yaml', 'yml'],
};
If using TypeScript, add declaration:
// yaml.d.ts
declare module '*.yaml' {
const content: any;
export default content;
}
declare module '*.yml' {
const content: any;
export default content;
}
| File | Purpose |
|---|---|
src/index.js | Transformer implementation |
src/process.js | YAML processing logic |
src/getCacheKey.js | Cache key generation |