muttley
Version:
Monitor Unit Test Tool
114 lines • 5.53 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sinon_1 = __importDefault(require("sinon"));
const mocha_runner_1 = require("./mocha-runner");
const assert = require("assert");
const child_process_1 = __importDefault(require("child_process"));
describe('MochaTestRunner', function () {
describe('ctor', function () {
it('it can be created', function () {
const runner = new mocha_runner_1.MochaTestRunner();
assert.ok(runner, 'new runner is falsy');
});
});
describe('findTestsP', function () {
it('finds no tests in itself', async function () {
const runner = new mocha_runner_1.MochaTestRunner();
const expected = [];
const result = await runner.findTestsP('./src/mocha-runner.js');
assert.deepEqual(result, expected);
});
it('finds these tests in myself', async function () {
const runner = new mocha_runner_1.MochaTestRunner();
const expected = [
{ suite: 'ctor', name: 'it can be created' },
{ suite: 'findTestsP', name: 'finds no tests in itself' },
{ suite: 'findTestsP', name: 'finds these tests in myself' },
{ suite: 'runFileP', name: 'handles execFile having an error' },
{ suite: 'runFileP', name: 'handles execFile failing tests' },
{ suite: 'runFileP', name: 'handles execFile passing tests' },
];
const result = await runner.findTestsP('./src/mocha-runner.t.js');
assert.deepEqual(result, expected);
});
});
describe('runFileP', function () {
it('handles execFile having an error', async function () {
// logger.level = 'debug';
// logger.type = 'stdout';
// need the fake to call the callback
const fakeExecFile = (file, args, func) => {
func('', '', 'file not found');
return {};
};
sinon_1.default.replace(child_process_1.default, 'execFile', fakeExecFile);
const runner = new mocha_runner_1.MochaTestRunner();
const onStart = sinon_1.default.fake();
const onPass = sinon_1.default.fake();
const onFail = sinon_1.default.fake();
const onEnd = sinon_1.default.fake();
await runner.runFileP('non existant file', onStart, onPass, onFail, onEnd);
sinon_1.default.restore();
assert(onStart.calledOnce);
assert(onPass.notCalled);
assert(onFail.calledOnce); // the exec failure causes a fail testcase
assert(onEnd.calledOnceWith(0, 0));
});
it('handles execFile failing tests', async function () {
// logger.level = 'debug';
// logger.type = 'stdout';
const mochaStdout = `
<testsuite name="Mocha Tests" tests="1" failures="0" errors="0" skipped="0" time="0.022">
<testcase classname="MyTestClass" name="myTest1" time="0.00">
<failure>it failed at (file1.js:27)</failure>
</testcase>
</testsuite>`;
// need the fake to call the callback
const fakeExecFile = (file, args, func) => {
func('', mochaStdout, '');
return {};
};
sinon_1.default.replace(child_process_1.default, 'execFile', fakeExecFile);
const runner = new mocha_runner_1.MochaTestRunner();
const onStart = sinon_1.default.fake();
const onPass = sinon_1.default.fake();
const onFail = sinon_1.default.fake();
const onEnd = sinon_1.default.fake();
await runner.runFileP('has passing tests ', onStart, onPass, onFail, onEnd);
sinon_1.default.restore();
assert(onStart.calledOnce);
assert(onPass.notCalled);
assert(onFail.calledOnce);
assert(onEnd.calledOnceWith(0, 1));
});
it('handles execFile passing tests', async function () {
// logger.level = 'debug';
// logger.type = 'stdout';
const mochaStdout = `
<testsuite name="Mocha Tests" tests="1" failures="1" errors="0" skipped="0" time="0.022">
<testcase classname="MyTestClass" name="myTest1" time="0.00"/>
</testsuite>`;
// need the fake to call the callback
const fakeExecFile = (file, args, func) => {
func('', mochaStdout, '');
return {};
};
sinon_1.default.replace(child_process_1.default, 'execFile', fakeExecFile);
const runner = new mocha_runner_1.MochaTestRunner();
const onStart = sinon_1.default.fake();
const onPass = sinon_1.default.fake();
const onFail = sinon_1.default.fake();
const onEnd = sinon_1.default.fake();
await runner.runFileP('has failing tests ', onStart, onPass, onFail, onEnd);
sinon_1.default.restore();
assert(onStart.calledOnce);
assert(onPass.calledOnce);
assert(onFail.notCalled);
assert(onEnd.calledOnceWith(1, 0));
});
});
});
//# sourceMappingURL=mocha-runner.t.js.map