Skip to content

Add context support on before hook #1657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export interface TestInterface<Context = {}> {
(title: string, macro: Macro<Context> | Macro<Context>[], ...args: Array<any>): void;
(macro: Macro<Context> | Macro<Context>[], ...args: Array<any>): void;

after: AfterInterface<null>;
after: AfterInterface<Context>;
afterEach: AfterInterface<Context>;
before: BeforeInterface<null>;
before: BeforeInterface<Context>;
beforeEach: BeforeInterface<Context>;
cb: CbInterface<Context>;
failing: FailingInterface<Context>;
Expand Down Expand Up @@ -187,9 +187,9 @@ declare const test: TestInterface;
export default test;

export {test};
export const after: AfterInterface<null>;
export const after: AfterInterface;
export const afterEach: AfterInterface;
export const before: BeforeInterface<null>;
export const before: BeforeInterface;
export const beforeEach: BeforeInterface;
export const cb: CbInterface;
export const failing: FailingInterface;
Expand Down
8 changes: 4 additions & 4 deletions index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ export interface TestInterface<Context = {}> {
(title: string, macro: Macro<Context> | Macro<Context>[], ...args: Array<any>): void;
(macro: Macro<Context> | Macro<Context>[], ...args: Array<any>): void;

after: AfterInterface<null>;
after: AfterInterface<Context>;
afterEach: AfterInterface<Context>;
before: BeforeInterface<null>;
before: BeforeInterface<Context>;
beforeEach: BeforeInterface<Context>;
cb: CbInterface<Context>;
failing: FailingInterface<Context>;
Expand Down Expand Up @@ -189,9 +189,9 @@ export type TodoDeclaration = {(title: string): void};
declare export default TestInterface<>;

declare export var test: TestInterface<>;
declare export var after: AfterInterface<null>;
declare export var after: AfterInterface<>;
declare export var afterEach: AfterInterface<>;
declare export var before: BeforeInterface<null>;
declare export var before: BeforeInterface<>;
declare export var beforeEach: BeforeInterface<>;
declare export var cb: CbInterface<>;
declare export var failing: FailingInterface<>;
Expand Down
79 changes: 56 additions & 23 deletions lib/test-collection.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
'use strict';
const EventEmitter = require('events');
const clone = require('lodash.clone');
const Concurrent = require('./concurrent');
const Sequence = require('./sequence');
const Test = require('./test');

class ContextRef {
constructor() {
this.value = {};
}

get() {
return this.value;
}

set(newValue) {
this.value = newValue;
}

copy() {
return new LateBinding(this); // eslint-disable-line no-use-before-define
}
}

class LateBinding extends ContextRef {
constructor(ref) {
super();
this.ref = ref;
this.bound = false;
}

get() {
if (!this.bound) {
this.set(clone(this.ref.get()));
}
return super.get();
}

set(newValue) {
this.bound = true;
super.set(newValue);
}
}

class TestCollection extends EventEmitter {
constructor(options) {
super();
Expand Down Expand Up @@ -98,9 +137,9 @@ class TestCollection extends EventEmitter {
this.emit('test', result);
}

_buildHooks(hooks, testTitle, context) {
_buildHooks(hooks, testTitle, contextRef) {
return hooks.map(hook => {
const test = this._buildHook(hook, testTitle, context);
const test = this._buildHook(hook, testTitle, contextRef);

if (hook.metadata.skipped || hook.metadata.todo) {
return this._skippedTest(test);
Expand All @@ -117,10 +156,6 @@ class TestCollection extends EventEmitter {
title += ` for ${testTitle}`;
}

if (!contextRef) {
contextRef = null;
}

const test = new Test({
contextRef,
failWithoutAssertions: false,
Expand All @@ -135,10 +170,6 @@ class TestCollection extends EventEmitter {
}

_buildTest(test, contextRef) {
if (!contextRef) {
contextRef = null;
}

test = new Test({
contextRef,
failWithoutAssertions: this.failWithoutAssertions,
Expand All @@ -152,26 +183,26 @@ class TestCollection extends EventEmitter {
return test;
}

_buildTestWithHooks(test) {
_buildTestWithHooks(test, contextRef) {
if (test.metadata.skipped || test.metadata.todo) {
return new Sequence([this._skippedTest(this._buildTest(test))], true);
}

const context = {context: {}};
const copiedRef = contextRef.copy();

const beforeHooks = this._buildHooks(this.hooks.beforeEach, test.title, context);
const afterHooks = this._buildHooks(this.hooks.afterEach, test.title, context);
const beforeHooks = this._buildHooks(this.hooks.beforeEach, test.title, copiedRef);
const afterHooks = this._buildHooks(this.hooks.afterEach, test.title, copiedRef);

let sequence = new Sequence([].concat(beforeHooks, this._buildTest(test, context), afterHooks), true);
let sequence = new Sequence([].concat(beforeHooks, this._buildTest(test, copiedRef), afterHooks), true);
if (this.hooks.afterEachAlways.length > 0) {
const afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterEachAlways, test.title, context));
const afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterEachAlways, test.title, copiedRef));
sequence = new Sequence([sequence, afterAlwaysHooks], false);
}
return sequence;
}

_buildTests(tests) {
return tests.map(test => this._buildTestWithHooks(test));
_buildTests(tests, contextRef) {
return tests.map(test => this._buildTestWithHooks(test, contextRef));
}

_hasUnskippedTests() {
Expand All @@ -182,22 +213,24 @@ class TestCollection extends EventEmitter {
}

build() {
const serialTests = new Sequence(this._buildTests(this.tests.serial), this.bail);
const concurrentTests = new Concurrent(this._buildTests(this.tests.concurrent), this.bail);
const contextRef = new ContextRef();

const serialTests = new Sequence(this._buildTests(this.tests.serial, contextRef), this.bail);
const concurrentTests = new Concurrent(this._buildTests(this.tests.concurrent, contextRef), this.bail);
const allTests = new Sequence([serialTests, concurrentTests]);

let finalTests;
// Only run before and after hooks when there are unskipped tests
if (this._hasUnskippedTests()) {
const beforeHooks = new Sequence(this._buildHooks(this.hooks.before));
const afterHooks = new Sequence(this._buildHooks(this.hooks.after));
const beforeHooks = new Sequence(this._buildHooks(this.hooks.before, null, contextRef));
const afterHooks = new Sequence(this._buildHooks(this.hooks.after, null, contextRef));
finalTests = new Sequence([beforeHooks, allTests, afterHooks], true);
} else {
finalTests = new Sequence([allTests], true);
}

if (this.hooks.afterAlways.length > 0) {
const afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterAlways));
const afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterAlways, null, contextRef));
finalTests = new Sequence([finalTests, afterAlwaysHooks], false);
}

Expand Down
12 changes: 2 additions & 10 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,11 @@ class ExecutionContext {
}

get context() {
const contextRef = this._test.contextRef;
return contextRef && contextRef.context;
return this._test.contextRef.get();
}

set context(context) {
const contextRef = this._test.contextRef;

if (!contextRef) {
this._test.saveFirstError(new Error(`\`t.context\` is not available in ${this._test.metadata.type} tests`));
return;
}

contextRef.context = context;
this._test.contextRef.set(context);
}

_throwsArgStart(assertion, file, line) {
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"is-observable": "^1.1.0",
"is-promise": "^2.1.0",
"last-line-stream": "^1.0.0",
"lodash.clone": "^4.5.0",
"lodash.clonedeepwith": "^4.5.0",
"lodash.debounce": "^4.0.3",
"lodash.difference": "^4.3.0",
Expand Down
20 changes: 15 additions & 5 deletions test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,26 +454,36 @@ test('shared context', t => {
const runner = new Runner();

runner.chain.before(a => {
a.is(a.context, null);
a.deepEqual(a.context, {});
a.context.arr = ['a'];
a.context.prop = 'before';
});

runner.chain.after(a => {
a.is(a.context, null);
a.deepEqual(a.context.arr, ['a', 'b', 'c', 'd']);
a.is(a.context.prop, 'before');
});

runner.chain.beforeEach(a => {
a.context.arr = ['a'];
a.deepEqual(a.context.arr, ['a']);
a.context.arr.push('b');
a.is(a.context.prop, 'before');
a.context.prop = 'beforeEach';
});

runner.chain('test', a => {
a.pass();
a.context.arr.push('b');
a.deepEqual(a.context.arr, ['a', 'b']);
a.context.arr.push('c');
a.is(a.context.prop, 'beforeEach');
a.context.prop = 'test';
});

runner.chain.afterEach(a => {
a.context.arr.push('c');
a.deepEqual(a.context.arr, ['a', 'b', 'c']);
a.context.arr.push('d');
a.is(a.context.prop, 'test');
a.context.prop = 'afterEach';
});

return runner.run({}).then(() => {
Expand Down
41 changes: 21 additions & 20 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ const Test = require('../lib/test');
const failingTestHint = 'Test was expected to fail, but succeeded, you should stop marking the test as failing';
const noop = () => {};

class ContextRef {
constructor() {
this.value = {};
}
get() {
return this.value;
}
set(newValue) {
this.value = newValue;
}
}

function ava(fn, contextRef, onResult) {
return new Test({
contextRef,
contextRef: contextRef || new ContextRef(),
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: false},
Expand All @@ -21,7 +33,7 @@ function ava(fn, contextRef, onResult) {

ava.failing = (fn, contextRef, onResult) => {
return new Test({
contextRef,
contextRef: contextRef || new ContextRef(),
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: false, failing: true},
Expand All @@ -32,7 +44,7 @@ ava.failing = (fn, contextRef, onResult) => {

ava.cb = (fn, contextRef, onResult) => {
return new Test({
contextRef,
contextRef: contextRef || new ContextRef(),
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: true},
Expand All @@ -43,7 +55,7 @@ ava.cb = (fn, contextRef, onResult) => {

ava.cb.failing = (fn, contextRef, onResult) => {
return new Test({
contextRef,
contextRef: contextRef || new ContextRef(),
failWithoutAssertions: true,
fn,
metadata: {type: 'test', callback: true, failing: true},
Expand Down Expand Up @@ -615,7 +627,11 @@ test('no crash when adding assertions after the test has ended', t => {

test('contextRef', t => {
new Test({
contextRef: {context: {foo: 'bar'}},
contextRef: {
get() {
return {foo: 'bar'};
}
},
failWithoutAssertions: true,
fn(a) {
a.pass();
Expand All @@ -628,21 +644,6 @@ test('contextRef', t => {
}).run();
});

test('it is an error to set context in a hook', t => {
let result;
const avaTest = ava(a => {
a.context = 'foo';
}, null, r => {
result = r;
});
avaTest.metadata.type = 'foo';

const passed = avaTest.run();
t.is(passed, false);
t.match(result.reason.message, /`t\.context` is not available in foo tests/);
t.end();
});

test('failing tests should fail', t => {
const passed = ava.failing('foo', a => {
a.fail();
Expand Down