Skip to content

A base class that can be used to implement a transformer for use with ts-jest

License

Notifications You must be signed in to change notification settings

joscha/ts-jest-transformer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ts-jest transformer base class

Build Status Greenkeeper badge

A base class that can be used to implement a transformer for use with ts-jest.

Usage

Run

yarn add ts-jest-transformer --dev

then create your custom transformer:

// my-transformer.js

const path = require('path');
const TsJestTransformer = require('ts-jest-transformer');

class MyFileBaseNameTransformer extends TsJestTransformer {
    process(src, filename, config, options) {
        // Write TS here
        const source = 'export default ' + JSON.stringify(path.basename(filename)) + ';';
        return super.process(source, filename, config, options);
    }
}

module.exports = new MyFileBaseNameTransformer();

and finally add the according jest config:

{
    "transform": {
        "\\.(svg)$": "<rootDir>/my-transformer.js",
        "\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "moduleFileExtensions": [
        "ts",
        "tsx",
        "js"
    ]
}

Example

For a more complete example, you can have a look at the ts-jest-transformer-example repository.

About

A base class that can be used to implement a transformer for use with ts-jest

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published