使用node.js,Mocha和mocha-jsdom测试Typescript生成的类? [英] Testing Typescript Generated Classes with node.js, Mocha and mocha-jsdom?

查看:181
本文介绍了使用node.js,Mocha和mocha-jsdom测试Typescript生成的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从bash运行单元测试,以使用mocha和mocha-jsdom测试我的Typescript生成的Javascript类.我使用mocha-jsdom是因为我想在测试中模拟dom,但要从bash运行单元测试.

I would like to run unit tests from bash to test my Typescript generated Javascript classes using mocha and mocha-jsdom. I'm using mocha-jsdom because I want to emulate the dom in my tests but run unit tests from bash.

我已经看到了一些示例,它们使用前端测试框架在浏览器中进行摩卡,但是我还没有看到它们在bash中运行的任何地方.

I've seen a few examples where they use the frontend testing framework for mocha in a browser, but I haven't seen any where they are run from bash.

我无法在Mocha单元测试中包含从Typescript生成的Javascript类.

It appears that I am unable to include the Javascript classes generated from Typescript in my mocha unit tests.

var jsdom = require('../../index'); 
require('../../src/js/acct/AccountManager.js');

describe('GenerateHTML', function() {
  describe('#generateHTML()', function() {

    jsdom()

    it('should generate a table', function() {
        var vahm = new VerticalAccountHTMLManager(document, "testId");

        expect($("#testId")[0]).eql("table");   
    }); 
  });
});

有什么方法可以将仍在bash中运行的创建类的函数导入到mocha单元测试中吗?

Is there any way to import the functions that create the classes into the mocha unit test while still running it from bash?

推荐答案

因此,让我们从执行测试开始. 为了执行测试,您有几种选择:

So let's start from executing the test. For executing the tests you have several options:

  • 使用karma.业力(Karma)是JavaScript测试人员.实际上,您在karma.config.js文件中指定了要执行的所有测试,它将执行所有测试.您还具有watch功能,该功能可让您每次对代码进行更改时都执行测试
  • 使用node.js.在package.json文件的script属性中,指定命令test并将该命令与该脚本关联,以便针对所有测试执行mocha.然后,您只需输入npm test
  • 就可以运行测试
  • 如果您想使用bash,我的建议是使用它来触发npm test命令或karma命令.这是通过bash启动测试的最简单方法.您还可以在Win上设置.bat文件,该文件始终通过npm/karma命令运行测试
  • 将任务运行程序用作GulpGrunt等...您可以指定一个任务,在其中触发测试,然后运行该任务,例如提供命令gulp mytask
  • Use karma. Karma is a JavaScript test runner. Actually you specify inside a karma.config.js file all the tests you want to execute, and it will execute all your tests. You have also a watch functionality which allows you to execute the test every time you make changes to your code
  • Use node.js. Inside the script property of your package.json file, you specify the command test and you associate to that script the command in order to execute mocha against all your tests. Then you can run the test just typing npm test
  • If you want to use a bash, my suggestion is to use it in order to trigger the npm test command or the karma command. That's the easiest way you can use for starting tests through bash. You can also setup a .bat file on Win which runs your tests always through the npm/karma commands
  • Use a task runner as Gulp, Grunt etc... You can specify a task where you trigger your tests and then run the task providing for example the command gulp mytask

我的建议是目前直接使用node.js.它使事情变得更容易.

My suggestion is to use node.js directly at the moment. It makes things easier.

关于TypeScript,您可以直接将Mocha与TypeScrit结合使用,而无需使用JavaScript.我建议您将摩卡咖啡与chai,chai-promise和sinon集成. Chai是一个断言库. Chai-as-promise可让您测试promise和sinon以测试功能和方法.

About TypeScript, you can directly use mocha with TypeScrit, no need of JavaScript. I suggest you to integrate mocha with chai, chai-as-promised and sinon. Chai is an assertion library. Chai-as-promised allows you to test promises and sinon to test functions and methods.

这是package.json的示例,上面有我刚才说过的测试命令:

This is an example of package.json with the test command I was saying above:

"scripts": {
    "test": "find ./src -name '*spec.ts' | xargs mocha -r ts-node/register"
}

在这一行中,您将执行所有以*spec.ts结尾的文件. ts-node是一个非常有用的节点插件,它允许您直接通过命令ts-node myfile.ts

With this line, you will execute all the files which end for *spec.ts. The ts-node is a really useful node plugin which allows you to run ts directly through the command ts-node myfile.ts

关于要在package.json中包含的依赖项的有用列表:

About the dependencies to include in your package.json here a useful list:

"devDependencies": {
        "@types/chai": "^3.5.0",
        "@types/chai-as-promised": "^0.0.30",
        "@types/mocha": "^2.2.40",
        "@types/node": "^7.0.12",
        "@types/sinon": "^2.1.2",
        "chai": "^3.5.0",
        "chai-as-promised": "^6.0.0",
        "husky": "^0.13.3",
        "mocha": "^3.2.0",
        "sinon": "^2.1.0",
        "ts-node": "^3.0.2",
        "typings": "^2.1.1"
}

关于如何将Mocha集成到测试中,这里是一个规范文件的声明,您可以将其用作模式:

About how to integrate mocha inside your tests, here a declaration of a spec file that you can use as patter:

// imports of testing libraries 
import * as mocha from 'mocha';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import sinon = require('sinon');
// import of the source files to be tested
import Main from './Main'; // an example

// testing inits
chai.use(chaiAsPromised);
const expect = chai.expect;
const assert = chai.assert;
const should = chai.should();

您可以尝试查看此存储库.有一个非常简单的项目,它带有TypeScript和我上面说过的所有测试库.它仍在进行中,我还没有时间完成它,因此主README.md也没有更新. 但是,如果您克隆项目或下载源代码,则可以完全了解我上面所说的所有内容,并可以举例说明可以在测试中使用的所有方法:

You can try to take a look to this repo. There is a really simple project with TypeScript and all the testing libraries I said above. It's still in progress and I didn't have time to finish it yet, so also the main README.md is not updated. But if you clone the project or download the source, you can have a really panoramic of all the things I was saying above and an illustration of all the methods you can use inside your tests:

https://github.com/quirimmo/testing-typescript-mocha -chai-sinon

希望获得帮助!如有任何疑问,请通知我!

Hope this help! for any query let me know!

这篇关于使用node.js,Mocha和mocha-jsdom测试Typescript生成的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆