Coder Social home page Coder Social logo

jest-transform-stealthy-require's Introduction

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?$": "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'
  }
};

jest-transform-stealthy-require's People

Contributors

antonku avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jest-transform-stealthy-require's Issues

Is it possible to separate request from request-promise?

Hello,

I have troubles in bigger codebase, where we use both request and rp, but in jest environment regular request module gets extended with request-promise and promise interface, changing all regular requests (made by request) to be promises and causing unhandled rejections.

Simplest testcase I could come up with:

const request = require('request');
const rp = require('request-promise-native');

test('request does not have promise interface', () => {
	expect(Object.is(request.Request.prototype.init, rp.Request.prototype.init)).toBe(false);
})

When I run in node (outside of jest environment), assertion passes, meaning the init method is not replaced for regular request module.

const request = require('request');
const rp = require('request-promise-native');
const assert = require('assert');

assert.ok(Object.is(request.Request.prototype.init, rp.Request.prototype.init) === false);

Is it possible to fully separate request from request-promise in jest environment?

Usage in windows

Hi, our team started using this package but later found that this doesn't work on windows machines (the team has a mix of windows and mac users). "Unable to expose method "then"" still shows up. On a windows machine I have tried running the tests in this package, which shows the same problem:

$ npm run test

> [email protected] test
> jest

 PASS  __tests__/index.ts
 FAIL  __tests__/request-promise.ts
  โ— Test suite failed to run

    Unable to expose method "then"

      1 | import "request-promise";
    > 2 | import "request-promise-native";
        | ^
      3 |
      4 | it("should not fail miserably", () => {
      5 |     expect(true).toBeTruthy();

      at Object.plumbing.exposePromiseMethod (node_modules/request-promise-core/lib/plumbing.js:140:19)
      at Object.<anonymous> (__tests__/request-promise.ts:2:1)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       4 passed, 4 total
Snapshots:   4 passed, 4 total
Time:        1.407s
Ran all test suites.

What I did was list out the 2 request-promise packages in jest.config.js which makes the tests pass:

const { defaults: tsjPreset } = require('ts-jest/presets');

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  transform: {
    ...tsjPreset.transform,
    'request-promise': '<rootDir>/dist/index.js',
    'request-promise-native': '<rootDir>/dist/index.js',
  },
  transformIgnorePatterns: ["/node_modules(?!/request-promise(-native)?)/"]
};

In our own code we have the transform section written similarly:

transform: {
  'request-promise': 'jest-transform-stealthy-require',
  'request-promise-native': 'jest-transform-stealthy-require',
},

Is there any issue with doing it this way? If not, I could create a pull request to update the config.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.