diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:38:50 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:40:43 +0200 |
commit | 7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027 (patch) | |
tree | 6de9a1aebd150a23b7f8c273ec657a5d0a18fe3e /node_modules/ava/lib/runner.js | |
parent | 963b7a41feb29cc4be090a2446bdfe0c1f1bcd81 (diff) | |
download | wallet-core-7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027.tar.xz |
add linting (and some initial fixes)
Diffstat (limited to 'node_modules/ava/lib/runner.js')
-rw-r--r-- | node_modules/ava/lib/runner.js | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/node_modules/ava/lib/runner.js b/node_modules/ava/lib/runner.js new file mode 100644 index 000000000..5f0edacb2 --- /dev/null +++ b/node_modules/ava/lib/runner.js @@ -0,0 +1,221 @@ +'use strict'; +const EventEmitter = require('events'); +const path = require('path'); +const Bluebird = require('bluebird'); +const jestSnapshot = require('jest-snapshot'); +const optionChain = require('option-chain'); +const matcher = require('matcher'); +const TestCollection = require('./test-collection'); +const validateTest = require('./validate-test'); + +const chainableMethods = { + defaults: { + type: 'test', + serial: false, + exclusive: false, + skipped: false, + todo: false, + failing: false, + callback: false, + always: false + }, + chainableMethods: { + test: {}, + serial: {serial: true}, + before: {type: 'before'}, + after: {type: 'after'}, + skip: {skipped: true}, + todo: {todo: true}, + failing: {failing: true}, + only: {exclusive: true}, + beforeEach: {type: 'beforeEach'}, + afterEach: {type: 'afterEach'}, + cb: {callback: true}, + always: {always: true} + } +}; + +function wrapFunction(fn, args) { + return function (t) { + return fn.apply(this, [t].concat(args)); + }; +} + +class Runner extends EventEmitter { + constructor(options) { + super(); + + options = options || {}; + + this.file = options.file; + this.match = options.match || []; + this.serial = options.serial; + this.updateSnapshots = options.updateSnapshots; + + this.hasStarted = false; + this.results = []; + this.snapshotState = null; + this.tests = new TestCollection({ + bail: options.bail, + failWithoutAssertions: options.failWithoutAssertions, + getSnapshotState: () => this.getSnapshotState() + }); + + this.chain = optionChain(chainableMethods, (opts, args) => { + let title; + let fn; + let macroArgIndex; + + if (this.hasStarted) { + throw new Error('All tests and hooks must be declared synchronously in your ' + + 'test file, and cannot be nested within other tests or hooks.'); + } + + if (typeof args[0] === 'string') { + title = args[0]; + fn = args[1]; + macroArgIndex = 2; + } else { + fn = args[0]; + title = null; + macroArgIndex = 1; + } + + if (this.serial) { + opts.serial = true; + } + + if (args.length > macroArgIndex) { + args = args.slice(macroArgIndex); + } else { + args = null; + } + + if (Array.isArray(fn)) { + fn.forEach(fn => { + this.addTest(title, opts, fn, args); + }); + } else { + this.addTest(title, opts, fn, args); + } + }); + } + + addTest(title, metadata, fn, args) { + if (args) { + if (fn.title) { + title = fn.title.apply(fn, [title || ''].concat(args)); + } + + fn = wrapFunction(fn, args); + } + + if (metadata.type === 'test' && this.match.length > 0) { + metadata.exclusive = title !== null && matcher([title], this.match).length === 1; + } + + const validationError = validateTest(title, fn, metadata); + if (validationError !== null) { + throw new TypeError(validationError); + } + + this.tests.add({ + metadata, + fn, + title + }); + } + + addTestResult(result) { + const test = result.result; + const props = { + duration: test.duration, + title: test.title, + error: result.reason, + type: test.metadata.type, + skip: test.metadata.skipped, + todo: test.metadata.todo, + failing: test.metadata.failing + }; + + this.results.push(result); + this.emit('test', props); + } + + buildStats() { + const stats = { + failCount: 0, + knownFailureCount: 0, + passCount: 0, + skipCount: 0, + testCount: 0, + todoCount: 0 + }; + + for (const result of this.results) { + if (!result.passed) { + // Includes hooks + stats.failCount++; + } + + const metadata = result.result.metadata; + if (metadata.type === 'test') { + stats.testCount++; + + if (metadata.skipped) { + stats.skipCount++; + } else if (metadata.todo) { + stats.todoCount++; + } else if (result.passed) { + if (metadata.failing) { + stats.knownFailureCount++; + } else { + stats.passCount++; + } + } + } + } + + return stats; + } + + getSnapshotState() { + if (this.snapshotState) { + return this.snapshotState; + } + + const name = path.basename(this.file) + '.snap'; + const dir = path.dirname(this.file); + + const snapshotPath = path.join(dir, '__snapshots__', name); + const testPath = this.file; + const update = this.updateSnapshots; + + const state = jestSnapshot.initializeSnapshotState(testPath, update, snapshotPath); + this.snapshotState = state; + return state; + } + + saveSnapshotState() { + if (this.snapshotState) { + this.snapshotState.save(this.updateSnapshots); + } + } + + run(options) { + if (options.runOnlyExclusive && !this.tests.hasExclusive) { + return Promise.resolve(null); + } + + this.hasStarted = true; + this.tests.on('test', result => { + this.addTestResult(result); + }); + return Bluebird.try(() => this.tests.build().run()); + } + attributeLeakedError(err) { + return this.tests.attributeLeakedError(err); + } +} + +module.exports = Runner; |