问题
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.
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.
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");
});
});
});
Is there any way to import the functions that create the classes into the mocha unit test while still running it from bash?
回答1:
So let's start from executing the test. For executing the tests you have several options:
- Use
karma
. Karma is a JavaScript test runner. Actually you specify inside akarma.config.js
file all the tests you want to execute, and it will execute all your tests. You have also awatch
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 commandtest
and you associate to that script the command in order to execute mocha against all your tests. Then you can run the test just typingnpm 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 commandgulp mytask
My suggestion is to use node.js directly at the moment. It makes things easier.
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.
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"
}
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
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"
}
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();
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!
来源:https://stackoverflow.com/questions/44077455/testing-typescript-generated-classes-with-node-js-mocha-and-mocha-jsdom