在浏览器和Node.JS中导入用Mocha/Chai/TypeScript编写的测试文件

Importing files for tests written on Mocha/Chai/TypeScript both in browser and Node.JS

本文关键字:TypeScript Chai 文件 测试 Mocha Node 浏览器 JS 导入      更新时间:2023-09-26

我使用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

更http://basarat.gitbooks.io/typescript/content/docs/project/external-modules.html

这将为node.js开箱即用。对于前端,您可以使用webpack之类的东西来获取单个.js文件。