我正在尝试获取 ts-node 选项--experimental-loader
与摩卡一起工作,但运气不佳。在我开始尝试编译 ES6 模块之前,我曾经能够通过这种方式运行 mocha 测试:
"test": "nyc --reporter=html mocha --require ts-node/register src/**/*.spec.ts"
当生成 ES6 模块时,这不再起作用。
我会用TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }'
测试解决方案,但这对我来说不起作用,因为另一个复杂性:我正在生成 ES6 模块作为构建的第一步,但是also使用 webpack 和 babel 生成 ES5/CommonJS 模块。除非我添加,否则最后一步不起作用.js
到我本地 TypeScript 的末尾import
声明。
但添加那些.js
扩展结果打破了TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }'
解决方案,但是,如果我返回并删除所有.js
扩展。我显然不想要一个测试和构建过程,我必须在添加和删除这些扩展之间不断来回。
为了暂时简化,我已经取出了 nyc,并且我正在尝试运行这样的测试:
mocha -r ts-node/register --experimental-loader ./ts-loader.mjs src/**/*.spec.ts
我这样没有得到任何错误,但也没有发生任何事情。就像src/**/*.spec.ts
不存在。
我的(目前)什么都不做的虚拟加载程序如下所示:
console.log('ts-loader loaded');
export async function resolve(specifier, context, defaultResolve) {
console.log('resolve');
return defaultResolve(specifier, context, defaultResolve);
}
export async function getFormat(url, context, defaultGetFormat) {
console.log('getFormat');
return defaultGetFormat(url, context, defaultGetFormat);
}
export async function getSource(url, context, defaultGetSource) {
console.log('getSource');
return defaultGetSource(url, context, defaultGetSource);
}
export async function transformSource(source, context, defaultTransformSource) {
console.log('transformSource');
return defaultTransformSource(source, context, defaultTransformSource);
}
export function getGlobalPreloadCode() {
console.log('getGlobalPreloadCode');
return '';
}
我可以告诉它已加载,因为'ts-loader loaded'
出现消息,但没有任何函数被调用。
我尝试过其他排列,但只是得到类似的错误src/**/*.spec.ts
被视为文字文件名而不是 glob,或者有关未找到模块的错误。
我希望看到我的加载程序被调用import
正在处理,然后弄清楚如何操作文件扩展名,但我还没有做到这一点。有什么建议么?
我使用的是节点 v14.15.1。我的项目的完整代码(具有有效的构建,但测试已损坏)可以在这里找到:https://github.com/kshetline/tubular_math https://github.com/kshetline/tubular_math