[英]Unable to import typescript module from global node_modules folder
[英]Import 'global' modules with TypeScript
我正在使用 Mocha 和 TypeScript 编写测试……这是一个简单的例子:
import {assert} from 'chai';
import Greeting from '../../../src/component/greeting/greeting';
describe('Greeting component', function () {
it('should greet correctly', function () {
let greeting = new Greeting();
assert(greeting.greeting === 'Hello World', 'should express the correct greeting');
});
});
我可以看到这些被正确编译。 我将它们作为 common-js 模块输出,并使用 system-js 作为浏览器内加载器。
System.import('component/greeting/greetingSpec.js')
.catch(console.log.bind(console))
.then(function() {
mocha.run();
})
我想创建一个列出所有“规范”文件的文件:
import * as greeting from './greeting/greetingSpec';
import * as foo from './greeting/fooSpec';
但是 TypeScript 编译器可以看到这些导入未使用,因此不会将它们包含在 JS 输出中。
如何为我的测试定义一个“入口点”,我可以通过 system-js 加载它?
尝试使用只有副作用的import
:
import './greeting/greetingSpec';
import './greeting/fooSpec';
这不会被省略。 有关更多信息,请参阅此讨论。
你的代码
import * as greeting from './greeting/greetingSpec';
import * as foo from './greeting/fooSpec';
但是 TypeScript 编译器可以看到这些导入未使用,因此不会将它们包含在 JS 输出中。
这是一个功能,支持例如延迟加载: https : //basarat.gitbooks.io/typescript/content/docs/project/external-modules.html
解决方法:
import * as greeting from './greeting/greetingSpec';
import * as foo from './greeting/fooSpec';
// use the imports in a dummy fashion to enforce that they get loaded
let _greeting = greeting;
let _foo = foo;
我一直在这里这样做:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.