jest-transform-stealthy-require

Transforms stealthy require calls into jest.isolateModules()

Usage no npm install needed!

<script type="module">
  import jestTransformStealthyRequire from 'https://cdn.skypack.dev/jest-transform-stealthy-require';
</script>

README

jest-transform-stealthy-require

Node.js CI

Transforms stealthy-require calls into jest.isolateModules()

Motivation

The purpose of this module is to address Jest + stealthy-require compatibility issue which causes issues like Unable to expose method "then" on modules that depend on stealthy-require.

Getting started

Installation

npm install --save-dev jest-transform-stealthy-require

Jest configuration

For request-promise/request-promise-native issue

Jest

jest.config.js

const { requestPromise } = require('jest-transform-stealthy-require/dist/presets');

module.exports = {
  // ...
  transform: {
    ...requestPromise.transform
  },
  transformIgnorePatterns: [requestPromise.transformIgnorePattern]
};

Jest + babel-jest

jest.config.js

const { requestPromise } = require('jest-transform-stealthy-require/dist/presets');

module.exports = {
  // ...
  transform: {
    ...requestPromise.transform,
    "^.+\\.[t|j]sx?quot;: "babel-jest"
  },
  transformIgnorePatterns: [requestPromise.transformIgnorePattern]
}

Jest + ts-jest

jest.config.js

const { requestPromise } = require('jest-transform-stealthy-require/dist/presets');
const { defaults: tsjPreset } = require('ts-jest/presets');
// [OR] const { jsWithTs: tsjPreset } = require('ts-jest/presets');
// [OR] const { jsWithBabel: tsjPreset } = require('ts-jest/presets');

module.exports = {
  // ...
  transform: {
    ...requestPromise.transform,
    ...tsjPreset.transform
  },
  transformIgnorePatterns: [requestPromise.transformIgnorePattern]
}

For other cases

The transformer can be used to replace stealthy-require calls in arbitrary modules. Simply specify the file pattern to for the modules that you would like to transform in your Jest config:

jest.config.js


module.exports = {
  // ...
  transform: {
    'my_pattern_to_module_to_transform': 'jest-transform-stealthy-require'
  }
};