Skip to content
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

fix: using addContext inside a before or after hook should work as expected #286

Merged
merged 1 commit into from
Jun 15, 2019
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# mochawesome changelog

## [Unreleased]
### Fixed
- Issue where using `addContext` inside a `before` or `after` hook would incorrectly apply context to the test [#284](https://github.com/adamgruber/mochawesome/issues/284)

## [4.0.0] - 2019-06-04
### Changed
Expand Down
15 changes: 12 additions & 3 deletions src/addContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,19 @@ const addContext = function (...args) {
}

/* Context is valid, now get the test object
* If `addContext` is called from inside a `beforeEach` or `afterEach`
* the test object will be `.currentTest`, otherwise just `.test`
* If `addContext` is called from inside a hook the test object
* will be `.currentTest`, and the hook will be `.test`.
* Otherwise the test is just `.test` and `.currentTest` is undefined.
*/
const test = args[0].currentTest || args[0].test;
const currentTest = args[0].currentTest;
const activeTest = args[0].test;

/* For `before` and `after`, add the context to the hook,
* otherwise add it to the actual test.
*/
const isEachHook = currentTest
&& /^"(?:before|after)\seach"/.test(activeTest.title);
const test = isEachHook ? currentTest : activeTest;

if (!test) {
log(ERRORS.INVALID_TEST, 'error');
Expand Down
91 changes: 86 additions & 5 deletions test/addContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,40 @@ const addContext = require('../src/addContext');

describe('addContext', () => {
let testObj;
let activeTest;
let test;
let origConsoleError;

const makeHook = type => ({
title: `"${type}" hook`,
body: 'function () {\n addContext(this, "i\'m in a before hook");\n }',
async: 0,
sync: true,
timedOut: false,
pending: false,
type: 'hook',
parent: '#<Suite>',
ctx: '#<Context>',
file: '/test.js',
uuid: '9f4e292c-8668-4008-9dc4-589909f94054'
});

beforeEach(() => {
origConsoleError = console.error;
console.error = function () {};

activeTest = {
title: 'sample test',
body: 'function () {\n addContext(this, "i\'m in a test");\n assert(true);\n }',
async: 0,
sync: true,
timedOut: false,
pending: false,
type: 'test',
file: '/test.js',
parent: '#<Suite>',
ctx: '#<Context>'
};
});

afterEach(() => {
Expand All @@ -17,7 +45,10 @@ describe('addContext', () => {
function contextTests() {
it('as a string', () => {
addContext(testObj, 'test context');
test.should.eql({ context: 'test context' });
test.should.eql({
...test,
context: 'test context'
});
});

it('as an object', () => {
Expand All @@ -26,6 +57,7 @@ describe('addContext', () => {
value: true
});
test.should.eql({
...test,
context: {
title: 'context title',
value: true
Expand All @@ -39,6 +71,7 @@ describe('addContext', () => {
value: undefined
});
test.should.eql({
...test,
context: {
title: 'context title',
value: 'undefined'
Expand All @@ -51,30 +84,78 @@ describe('addContext', () => {
addContext(testObj, 'test context 2');
addContext(testObj, { title: 'test context 3', value: true });
test.should.eql({
context: [ 'test context 1', 'test context 2', { title: 'test context 3', value: true } ]
...test,
context: [
'test context 1',
'test context 2',
{ title: 'test context 3', value: true }
]
});
});
}

describe('when run inside a test', () => {
beforeEach(() => {
testObj = { test: {} };
testObj = {
currentTest: undefined,
test: activeTest
};
test = testObj.test;
});
contextTests();
});

describe('when run inside a before', () => {
beforeEach(() => {
testObj = {
currentTest: activeTest,
test: makeHook('before all')
};
test = testObj.test;
});
contextTests();
});

describe('when run inside a beforeEach', () => {
beforeEach(() => {
testObj = { currentTest: {} };
testObj = {
currentTest: activeTest,
test: makeHook('before each')
};
test = testObj.currentTest;
});
contextTests();
});

describe('when run inside an after', () => {
beforeEach(() => {
testObj = {
currentTest: activeTest,
test: makeHook('after all')
};
test = testObj.test;
});
contextTests();
});

describe('when run inside an afterEach', () => {
beforeEach(() => {
testObj = {
currentTest: activeTest,
test: makeHook('after each')
};
test = testObj.currentTest;
});
contextTests();
});

describe('No context is added when', () => {
beforeEach(() => {
testObj = { test: {} };
testObj = {
test: {
title: 'sample test'
}
};
test = testObj.test;
});

Expand Down