Skip to content

stepancar/jest-ifdef-transform

Repository files navigation

npm node size

jest-ifdef-transform

Jest transformer mimicking webpack-ifdef-loader's functionality

Install

$ npm install --save-dev jest-ifdef-transform

Usage

Use jest's transform configuration options to use this package in your unit tests.

For example, use the following to load different implementations of class for browser and node

/// #if NODE
/// #code import {NodeModel as Model} from "./NodeModel";
/// #code export {Model};
/// #else
/// #code import {Model as Model} from "./Model";
/// #code export {Model};
/// #endif

import type {
    Model as ModelType,
} from './Model';

declare module './' {
    export const Model: typeof ModelType;
    export type Model = ModelType;
}

jest.config.ts

transform: {
    'myClass/index.ts$': ['jest-ifdf-transform', {
        'ifdef-verbose': true,
        'ifdef-triple-slash': true,
        'ifdef-fill-with-blanks': true,
        'ifdef-uncomment-prefix': '/// #code ',
        'NODE': true, // variable, which you can use in code
    }],
},

License

MIT