Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nshimiye/test-prep
Run mocha programmatically
https://github.com/nshimiye/test-prep
mocha test-automation test-runner
Last synced: 27 days ago
JSON representation
Run mocha programmatically
- Host: GitHub
- URL: https://github.com/nshimiye/test-prep
- Owner: nshimiye
- Created: 2017-04-26T05:02:05.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-04-26T05:28:17.000Z (over 7 years ago)
- Last Synced: 2024-10-24T22:43:16.522Z (2 months ago)
- Topics: mocha, test-automation, test-runner
- Language: JavaScript
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# test-prep
Run mocha programmatically## Step by Step
* Create the test
```javascript
// source: /test-prep/src/tests/t1.js
// nodejs = v6.9.4
'use strict';
var should = require('should');var q1 = require('../questions/q1.js'); // q1 = question 1
var a1 = require('../answers/a1.js'); // a1 = answer 1// @TODO fetch user input
var userInput = 3;describe('Add', function() {
it('should return value that same as [user input]', function() {
a1().should.equal(userInput);
});
});
```* Create a test runner
```javascript
// source: /test-prep/src/runners/r1.js
// nodejs = v6.9.4
'use strict';
var Mocha = require('mocha');var mocha = new Mocha({
reporter: 'json'
});
mocha.addFile('/test-prep/src/tests/t1.js');/**
* run the test and print 0 for failed test and 1 for passed
* @returns Promise<{ status: string, score: number }>
*/
function main() {
return new Promise(resolve => {mocha.run()
.on('pass', function (test, success) {
// I want to resolve the promise with the result generated by mocha:
let status = 'Succeded', score = 1;
return resolve({ status, score });
})
.on('fail', function (test, error) {
// I want to resolve the promise with the result generated by mocha:
let status = 'Failed', score = 0;
return resolve({ status, score });
});});
}```
* Run the test
```javascript
// source: /test-prep/src/runners/r1.js
main().then(result => console.log(result));
```
```sh
node src/runners/r1.js
```