微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

babel-plugin-rewire 在 Typescript 项目中不起作用

如何解决babel-plugin-rewire 在 Typescript 项目中不起作用

注意:我的目标是为项目带来 Rewire 之类的功能。无论是使用 Rewire 包、babel-plugin-rewire 还是任何其他满足我目标的库。考虑到这一点,以下是详细信息:

我正在尝试使用 Mocha 和 Chai 设置一个新的 Typescript 项目。其中一项单元测试要求我使用 rewire,这不适用于 ES6 导入。所以,我最终使用了 babel-plugin-rewire。但是,我无法让它工作。例如,以下行:

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

TypeError: _get__(...).__get__ is not a function 失败。

我在此处设置了一个简约的可重现公共存储库:https://github.com/naishe/typescript-babel 如果您想使用它。

这是最小的项目设置:

src/hello.ts

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

function privateHello(name: string) {
  return `Not exported 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");
  });

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

  it("should check if private function exists",() => {
    //@ts-ignore
    const privateHello = hello.__get__("privateHello");
    expect(privateHello).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"
}

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 发出这个:

  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)

我在 babel-plugin-rewire 上有 raised the concern,但它似乎非常安静。所以,我想知道是否还有其他方法可以实现这一目标?

解决方法

您只需要声明导出名称,作为函数。 因为您将其导入为:

import hello from "../src/hello";

只需使用:

const jwtVerify = hello;
expect(jwtVerify).to.be.a("function");

原因是因为您试图查看返回的对象是否是一个函数(并且您导出了一个函数)。所以你只需要检查整个导入名称。


补充说明:

如果 hello.ts 是这样制作的:

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

然后调用您刚刚调用的函数:

console.log(hello('John'));

输出:Hello John

因为按照你导出的方式,导入就是函数。


问题更新后编辑:

我不熟悉 Rewire,但 Using Rewire with TypeScript 上有一个相关问题可能会有所帮助。

如果您需要使用带有 Typescript 的 Babel,请尝试使用 Microsoft - Typescript-Babel-Starter 中显示的配置开始,以确保它们都配置为相互理解。

这也是我发现的一个很好的内容Typescript and Babel Article

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?