An open API service indexing awesome lists of open source software.

https://github.com/joscha/ts-jest-transformer

A base class that can be used to implement a transformer for use with ts-jest
https://github.com/joscha/ts-jest-transformer

jest testing transformer ts-jest typescript

Last synced: 5 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# `ts-jest` transformer base class

[![Build Status](https://travis-ci.org/joscha/ts-jest-transformer.svg?branch=master)](https://travis-ci.org/joscha/ts-jest-transformer)
[![Greenkeeper badge](https://badges.greenkeeper.io/joscha/ts-jest-transformer.svg)](https://greenkeeper.io/)

A base class that can be used to implement a transformer for use with [ts-jest](https://github.com/kulshekhar/ts-jest).

## Usage

Run

```console
yarn add ts-jest-transformer --dev
```

then create your custom transformer:

```js
// 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:

```json
{
"transform": {
"\\.(svg)$": "/my-transformer.js",
"\\.(ts|tsx)$": "/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](https://github.com/joscha/ts-jest-transformer-example) repository.