在浏览器和Node.JS中导入用Mocha/Chai/TypeScript编写的测试文件
Importing files for tests written on Mocha/Chai/TypeScript both in browser and Node.JS
我使用Mocha和Chai在TypeScript上编写了测试。我希望它的工作从浏览器和NodeJS。
下面是我的测试用例片段:
/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
/// <reference path="../typings/node/node.d.ts" />
/// <reference path="../../src/scripts/mymodule/mymodule.ts"/>
module mymodule.DecryptSequenceBuilder.Tests {
var expect = chai.expect;
var should = chai.should;
var assert = chai.assert;
describe('DecryptSequenceBuilder', function() {
describe('Builder tests', function() {
it('gets all files together with one cert', function() {
var files = [
{
filename: 'file1.xml',
encrypted: true,
encryptedOn: ['cert1', 'cert2', 'cert3'],
compressed: false,
contentType: "xml"
},
{
filename: 'file2.xml',
encrypted: true,
encryptedOn: ['cert4', 'cert5', 'cert3'],
compressed: false,
contentType: "xml"
}
];
var steps = mymodule.DecryptSequenceBuilder.build(files, ['cert1', 'cert2', 'cert3', 'cert4', 'cert5'], []);
var trueSteps = [
{
'certSerial': 'cert3',
'provider': 'toolbox',
'files': [files[0], files[1]]
}
];
assert(steps.length === trueSteps.length);
});
................
这里是test。html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="stylesheets/mocha.css" />
<style>
#fixture {
position: absolute;
top: -9999;
left: -9999;
}
</style>
<script src="node_modules/chai/chai.js"></script>
<script src="node_modules/mocha/mocha.js"></script>
<script> mocha.setup('bdd');</script>
<script>
window.onload = function() {
mocha.run();
};
</script>
<script src="../src/scripts/mymodule/mymodule.js"></script>
<script src="test/testcase.js"></script>
</head>
<body>
<div id="mocha"></div>
</body>
</html>
所以我在HTML中包含所有构建的JS文件,在浏览器中没有问题。但是如何在TypeScript中包含我的库呢?当我在纯JS上编写测试时,在测试用例的开头有这样一段代码:
if (typeof require !== 'undefined') {
chai = require('chai');
mymodule = require('./../../src/scripts/mymodule/mymodule.js');
mymodule = mymodule.mymodule;
}
但是现在它在TypeScript中不起作用,因为TypeScript认为我已经包含了模块"mymodule",我试图初始化变量"mymodule"。我如何正确地包含NodeJS文件? 如何正确包含NodeJS文件?
使用基于文件的模块到处和编译--module commonjs
这将为node.js开箱即用。对于前端,您可以使用webpack之类的东西来获取单个.js
文件。
相关文章:
- Foreach无法在Typescript中工作
- 扩展SVGTextElement时出现Typescript Uncaught TypeError
- 为什么无法在TypeScript中导出类实例
- 如何将TypeScript对象转换为普通对象
- Angular 2.0 with JavaScript or TypeScript?
- "实例范围”;TypeScript类的getter/setter
- 如何在大型nodeJS代码的基础上逐步引入typescript
- 从HTML调用typescript文件中的函数
- 如何将js库与typescript一起使用
- 理解typescript中的构造函数接口
- 在gump和nodejs中使用Typescript时,未定义对require和exports的引用
- Typescript导入导出的类会发出require(..),从而导致浏览器错误
- 如何在Mocha/Chai中测试JS原型(非模块)
- 为其他JS生成定义“;编译器”;来自TypeScript定义文件
- Typescript angularjs$http获取响应类型(避免使用<any>)
- Typescript编译器错误,Angular 1.5组件应为分号
- 应该如何在typescript中键入lodash流函数
- 在浏览器和Node.JS中导入用Mocha/Chai/TypeScript编写的测试文件
- 正在导入Typescript中的Chai
- 让TypeScript、Karma、RequireJS和Chai一起工作