【发布时间】:2016-11-20 23:16:51
【问题描述】:
我正在尝试使用 Jest 测试 Typescript 类。由于我需要使用 es6 的async/await,我需要先将 typescript 类编译为 es6,然后再使用 babel 编译为 es5。为了实现这一点,我需要向预处理器添加什么。
我当前的预处理器如下所示:
const tsc = require('typescript');
module.exports = {
process: function(src, path) {
if (path.endsWith('.ts') || path.endsWith('.tsx')) {
return tsc.transpile(
src,
{
module: tsc.ModuleKind.CommonJS,
jsx: tsc.JsxEmit.React
},
path,
[]
);
}
return src;
}
};
需要加target: tsc.ScriptTarget.ES6吗?
当我这样做时,我在处理的代码中得到一个unexpected identifier = 错误,它看起来像我的.ts 类的转译版本。我从中收集到的是我的预处理器正在将数据编译为 es6,但我的 es6 没有被转译为 es5。
还有任何现成的预处理器可以做到这一点吗?
【问题讨论】:
标签: typescript ecmascript-6 jestjs