Browse Source

Pass options object to Test constructor

There are too many parameters.
master
Mark Wubben 8 years ago
parent
commit
e81d3f2818
  1. 30
      lib/test-collection.js
  2. 14
      lib/test.js
  3. 18
      test/observable.js
  4. 18
      test/promise.js
  5. 50
      test/test.js

30
lib/test-collection.js

@ -114,25 +114,39 @@ class TestCollection extends EventEmitter {
return test;
});
}
_buildHook(hook, testTitle, context) {
_buildHook(hook, testTitle, contextRef) {
let title = hook.title;
if (testTitle) {
title += ` for ${testTitle}`;
}
if (!context) {
context = null;
if (!contextRef) {
contextRef = null;
}
return new Test(hook.metadata, title, hook.fn, false, context, this._emitTestResult);
return new Test({
contextRef,
failWithoutAssertions: false,
fn: hook.fn,
metadata: hook.metadata,
onResult: this._emitTestResult,
title
});
}
_buildTest(test, context) {
if (!context) {
context = null;
_buildTest(test, contextRef) {
if (!contextRef) {
contextRef = null;
}
return new Test(test.metadata, test.title, test.fn, this.failWithoutAssertions, context, this._emitTestResult);
return new Test({
contextRef,
failWithoutAssertions: this.failWithoutAssertions,
fn: test.fn,
metadata: test.metadata,
onResult: this._emitTestResult,
title: test.title
});
}
_buildTestWithHooks(test) {
if (test.metadata.skipped || test.metadata.todo) {

14
lib/test.js

@ -88,13 +88,13 @@ Object.defineProperty(ExecutionContext.prototype, 'context', {enumerable: true})
}
class Test {
constructor(metadata, title, fn, failWithoutAssertions, contextRef, onResult) { // eslint-disable-line max-params
this.metadata = metadata;
this.title = title;
this.fn = isGeneratorFn(fn) ? co.wrap(fn) : fn;
this.failWithoutAssertions = failWithoutAssertions;
this.contextRef = contextRef;
this.onResult = onResult;
constructor(options) {
this.contextRef = options.contextRef;
this.failWithoutAssertions = options.failWithoutAssertions;
this.fn = isGeneratorFn(options.fn) ? co.wrap(options.fn) : options.fn;
this.metadata = options.metadata;
this.onResult = options.onResult;
this.title = options.title;
this.assertCount = 0;
this.assertError = undefined;

18
test/observable.js

@ -4,11 +4,25 @@ const Test = require('../lib/test');
const Observable = require('zen-observable'); // eslint-disable-line import/order
function ava(fn, onResult) {
return new Test({type: 'test', callback: false}, '[anonymous]', fn, true, null, onResult);
return new Test({
contextRef: null,
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: false},
onResult,
title: '[anonymous]'
});
}
ava.cb = function (fn, onResult) {
return new Test({type: 'test', callback: true}, '[anonymous]', fn, true, null, onResult);
return new Test({
contextRef: null,
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: true},
onResult,
title: '[anonymous]'
});
};
test('returning an observable from a legacy async fn is an error', t => {

18
test/promise.js

@ -5,11 +5,25 @@ const formatValue = require('../lib/format-assert-error').formatValue;
const Test = require('../lib/test');
function ava(fn, onResult) {
return new Test({type: 'test', callback: false}, '[anonymous]', fn, true, null, onResult);
return new Test({
contextRef: null,
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: false},
onResult,
title: '[anonymous]'
});
}
ava.cb = function (fn, onResult) {
return new Test({type: 'test', callback: true}, '[anonymous]', fn, true, null, onResult);
return new Test({
contextRef: null,
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: true},
onResult,
title: '[anonymous]'
});
};
function pass() {

50
test/test.js

@ -9,19 +9,47 @@ const failingTestHint = 'Test was expected to fail, but succeeded, you should st
const noop = () => {};
function ava(fn, contextRef, onResult) {
return new Test({type: 'test', callback: false}, '[anonymous]', fn, true, contextRef, onResult || noop);
return new Test({
contextRef,
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: false},
onResult: onResult || noop,
title: '[anonymous]'
});
}
ava.failing = (fn, contextRef, onResult) => {
return new Test({type: 'test', callback: false, failing: true}, '[anonymous]', fn, true, contextRef, onResult || noop);
return new Test({
contextRef,
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: false, failing: true},
onResult: onResult || noop,
title: '[anonymous]'
});
};
ava.cb = (fn, contextRef, onResult) => {
return new Test({type: 'test', callback: true}, '[anonymous]', fn, true, contextRef, onResult || noop);
return new Test({
contextRef,
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: true},
onResult: onResult || noop,
title: '[anonymous]'
});
};
ava.cb.failing = (fn, contextRef, onResult) => {
return new Test({type: 'test', callback: true, failing: true}, '[anonymous]', fn, true, contextRef, onResult || noop);
return new Test({
contextRef,
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: true, failing: true},
onResult: onResult || noop,
title: '[anonymous]'
});
};
test('run test', t => {
@ -604,16 +632,18 @@ test('assertions return promises', t => {
});
test('contextRef', t => {
new Test({type: 'test'}, 'foo',
a => {
new Test({
contextRef: {context: {foo: 'bar'}},
failWithoutAssertions: true,
fn(a) {
a.pass();
t.strictDeepEqual(a.context, {foo: 'bar'});
t.end();
},
true,
{context: {foo: 'bar'}},
() => {}
).run();
metadata: {type: 'test'},
onResult() {},
title: 'foo'
}).run();
});
test('it is an error to set context in a hook', t => {

Loading…
Cancel
Save