Browse Source

[rewrite] WIP gunner.js

0.7.0-breaking-rewrite
Muthu Kumar 6 years ago
parent
commit
ef738f7771
  1. 8
      package.json
  2. 303
      shrinkwrap.yaml
  3. 128
      src/gunner.js

8
package.json

@ -1,6 +1,6 @@
{ {
"name": "@klenty/gunner", "name": "@klenty/gunner",
"version": "0.6.7", "version": "0.7.0",
"description": "Zero magic, fast test-runner and assertion framework. No magic globals.", "description": "Zero magic, fast test-runner and assertion framework. No magic globals.",
"main": "index.js", "main": "index.js",
"repository": { "repository": {
@ -23,9 +23,9 @@
"dependencies": { "dependencies": {
"@codefeathers/iseq": "^1.2.1", "@codefeathers/iseq": "^1.2.1",
"@codefeathers/promise.object": "^0.9.5", "@codefeathers/promise.object": "^0.9.5",
"bluebird": "^3.5.1",
"chalk": "^2.4.1",
"eslint": "^5.2.0",
"json-stringify-safe": "^5.0.1" "json-stringify-safe": "^5.0.1"
},
"devDependencies": {
"eslint": "^5.2.0"
} }
} }

303
shrinkwrap.yaml

File diff suppressed because it is too large

128
src/gunner.js

@ -1,126 +1,96 @@
'use strict'; 'use strict';
const { EOL } = require('os'); const { arrayOrPush } = require('./util');
const chalk = require('chalk'); const caller = require('./lib/unit/caller');
const Promise = require('bluebird'); const testrunner = require('./lib/testrunner');
Promise.object = require('@codefeathers/promise.object'); const { expect, expectMany } = require('./lib/expect');
const _runTests = require('./lib/runTests');
const _expect = require('./lib/expect');
const logger = require('./lib/logger');
const symbols = require('./util/symbols'); const symbols = require('./util/symbols');
class Gunner { class Gunner {
constructor (options = {}) { constructor (name) {
this.__hooks__ = { this.name = name;
before: { this.__suite__ = {
tests: [],
beforeHooks: {
[symbols.Start]: [], [symbols.Start]: [],
[symbols.End]: [], [symbols.End]: [],
'*': [], '*': [],
}, },
after: { afterHooks: {
[symbols.Start]: [], [symbols.Start]: [],
[symbols.End]: [], [symbols.End]: [],
'*': [], '*': [],
}, }
}; };
this.__state__ = [];
this.__tests__ = [];
this.name = options.name;
} }
test (description, test) { test (description, test) {
const existing = ( const existing = (
this.__tests__ this.__suite__.tests
.find(x => x.description === description) .find(x => x.description === description)
); );
if (existing) if (existing)
throw new Error(`Test '${description}' already exists!`); throw new Error(`Test '${description}' already exists!`);
this.__tests__.push({ const unit = {
description, description,
test: state => { type: 'test',
try { run: state => caller(test, state),
return test(_expect, state); };
} catch (e) { this.__suite__.tests.push(unit);
// If errors are thrown, reject them
return Promise.reject(e);
}
},
});
return this; return this;
} }
before (description, run) { before (description, run, label) {
const hook = { const unit = {
description, description,
run, label,
type: 'hook',
run: state => caller(run, state),
}; };
arrayOrPush(this.__suite__.beforeHooks, description, unit);
this.__hooks__.before[description]
? this.__hooks__.before[description].push(hook)
: this.__hooks__.before[description] = [ hook ];
return this; return this;
} }
after (description, run) { after (description, run, label) {
const hook = { const unit = {
description, description,
run, label,
type: 'hook',
run: state => caller(run, state),
}; };
arrayOrPush(this.__suite__.afterHooks, description, unit);
this.__hooks__.after[description]
? this.__hooks__.after[description].push(hook)
: this.__hooks__.after[description] = [ hook ];
return this; return this;
} }
run (options = {}) { run (options = {}) {
return _runTests(this, options) return testrunner(this, options);
.then(results => { // .then(results => {
const success = results.filter(r => r.result === 'pass'); // const success = results.filter(r => r.result === 'pass');
const successPercent = Math.floor( // const successPercent = Math.floor(
success.length/results.length * 100 // success.length/results.length * 100
); // );
const beforeAfterLine = // if((successPercent !== 100) && typeof process !== 'undefined')
successPercent === 100 // process.exitCode = 1;
? chalk`{green ------------------------------------}`
: chalk`{red ------------------------------------}`; // return results;
// })
const log = logger.create(options); // .then(results => {
log( // if (options.exit && typeof process !== 'undefined')
EOL, // process.exit();
beforeAfterLine, // return results;
EOL, EOL, // });
chalk`{green ${success.length}}`,
`tests passed of ${results.length}`,
`[${successPercent}% success]`,
EOL, EOL,
beforeAfterLine
);
if((successPercent !== 100) && typeof process !== 'undefined')
process.exitCode = 1;
return results;
})
.then(results => {
if (options.exit && typeof process !== 'undefined')
process.exit();
return results;
});
} }
} }
module.exports = Gunner; module.exports = Gunner;
module.exports.expect = _expect; module.exports.Gunner = Gunner;
module.exports.expect = expect;
module.exports.expectMany = expectMany;
module.exports.Start = symbols.Start; module.exports.Start = symbols.Start;
module.exports.End = symbols.End; module.exports.End = symbols.End;

Loading…
Cancel
Save