speedskater / babel-plugin-rewire

A babel plugin adding the ability to rewire module dependencies. This enables to mock modules for testing purposes.
843 stars 90 forks source link

babel-plugin-rewire does not work in a Typescript project #237

Open naishe opened 3 years ago

naishe commented 3 years ago

I am trying to setup a new Typescript project with Mocha, and Chai. One of the unit tests require me to use rewire, which does not work with ES6 imports. So, I ended up using babel-plugin-rewire. But, I can't get it to work. For example, the following line:

    const jwtVerify = hello.__get__("hello");

Fails with TypeError: _get__(...).__get__ is not a function.

I have setup a minimalistic reproducible public repo here: https://github.com/naishe/typescript-babel if you want to play with it.

Here is the minimal project setup:

src/hello.ts

export default function(name: string) {
  return `Hello ${name}`;
}

test/index.spec.ts

import hello from "../src/hello";
import { expect } from "chai";

describe("Typescript + Babel usage suite", () => {
  // This works!
  it("should return string correctly", () => {
    expect(hello("mocha")).to.be.equal("Hello mocha");
  });

  // This fails
  it("should check if jwtVerify function exists", () => {
    //@ts-ignore
    const jwtVerify = hello.__get__("hello");
    expect(jwtVerify).to.be.a("function");
  });
});

test/babel-register.js

const register = require('@babel/register').default;

register({ extensions: ['.ts', '.tsx', '.js', '.jsx'] });

babelrc.json

{
  "plugins": ["rewire"]
}

babel.config.js

module.exports = (api) => {
  // Cache configuration is a required option
  api.cache(false);

  const presets = [
    "@babel/preset-typescript",
    "@babel/preset-env"
  ];

  return { presets };
};

mocharc.json

{
  "extension": ["ts"],
  "spec": "test/**/*.spec.ts",
  "require": "test/babel-register.js"
}

Relevant part of package.json

"scripts": {
    "test": "mocha"
  },
  "devDependencies": {
    "@babel/cli": "^7.12.10",
    "@babel/core": "^7.12.10",
    "@babel/preset-env": "^7.12.11",
    "@babel/preset-typescript": "^7.12.7",
    "@babel/register": "^7.12.10",
    "@types/chai": "^4.2.14",
    "@types/mocha": "^8.2.0",
    "@typescript-eslint/eslint-plugin": "^4.13.0",
    "@typescript-eslint/parser": "^4.13.0",
    "chai": "^4.2.0",
    "eslint": "^7.17.0",
    "mocha": "^8.2.1",
    "ts-node": "^9.1.1",
    "typescript": "^4.1.3"
  },
  "dependencies": {
    "babel-core": "^6.26.3",
    "babel-plugin-rewire": "^1.2.0"
  }

npm test emmits this:

  Typescript + Babel usage suite
    ✓ should return string correctly
    1) should check if jwtVerify function exists

  1 passing (4ms)
  1 failing

  1) Typescript + Babel usage suite
       should check if jwtVerify function exists:
     TypeError: _get__(...).__get__ is not a function
      at Context.<anonymous> (test/index.spec.ts:10:29)
      at processImmediate (internal/timers.js:456:21)

Relevant StackOverflow entry: https://stackoverflow.com/questions/65725761/babel-plugin-rewire-does-not-work-in-a-typescript-project

stefanpl commented 3 years ago

Hi 👋

Any updates on TypeScript support @naishe @speedskater ? Couldn't get this to run with TypeScript neither, I'm getting:

The exported identifier "_RewireAPI__" is not declared in Babel's scope tracker
as a JavaScript value binding, and "@babel/plugin-transform-typescript"
never encountered it as a TypeScript type declaration.
It will be treated as a JavaScript value.

This problem is likely caused by another plugin injecting
"_RewireAPI__" without registering it in the scope tracker. If you are the author
 of that plugin, please use "scope.registerDeclaration(declarationPath)".

You can see the setup here (it's very minimal, 2 webpack loaders and 2 babel plugins): https://github.com/stefanpl/nodejs-typescript-starter/tree/install-babel-plugin-rewire

I also tried this repo, but it's archived and doesn't work for me neither.

Any input very much appreciated!

naishe commented 3 years ago

@stefanpl -- I got it working for the client back then. It was not as convoluted. I need to go back and check how. If you can wait until early next week, I'd probably be able to prepare a PoC/

stefanpl commented 3 years ago

@naishe I would very much appreciate it! I can definitely wait until then, just at some point it would be nice to have it for proper testing in a TS environment.

kibebr commented 3 years ago

@naishe Sorry, any chance you could write a quick guide on how to do it?

lbineau commented 2 years ago

Same question here. It does not seems to prevent to work with TS though but is an ugly error everytime I run the testing tools.