问题描述
注意:我的目标是为项目带来 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。