Muthu Kumar
6 years ago
3 changed files with 201 additions and 238 deletions
File diff suppressed because it is too large
@ -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…
Reference in new issue