Skip to content

Commit

Permalink
Merge pull request #785 from unexpectedjs/feature/childInheritsFromPa…
Browse files Browse the repository at this point in the history
…rentExpect

Add the parent expect to the prototype chain of a child expect
  • Loading branch information
papandreou authored Apr 26, 2022
2 parents 64aeaad + 16e4b28 commit 7299a38
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
53 changes: 32 additions & 21 deletions lib/createTopLevelExpect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1584,30 +1584,32 @@ expectPrototype.clone = function () {

expectPrototype.child = function () {
this._assertTopLevelExpect();
const childExpect = createTopLevelExpect({
assertions: {},
types: [],
typeByName: {},
output: this.output.clone(),
format: this.outputFormat(),
installedPlugins: [],
});
const parent = (childExpect.parent = this);
const childExpect = createTopLevelExpect(
{
assertions: {},
types: [],
typeByName: {},
output: this.output.clone(),
format: this.outputFormat(),
installedPlugins: [],
},
this
);

childExpect.exportAssertion = function (testDescription, handler) {
parent.addAssertion(testDescription, handler, childExpect);
childExpect.parent.addAssertion(testDescription, handler, childExpect);
return this;
};
childExpect.exportType = function (type) {
if (childExpect.getType(type.name) !== type) {
childExpect.addType(type);
}

parent.addType(type, childExpect);
childExpect.parent.addType(type, childExpect);
return this;
};
childExpect.exportStyle = function (name, handler, allowRedefinition) {
parent.addStyle(
childExpect.parent.addStyle(
name,
function (...args) {
const childOutput = childExpect.createOutput(this.format);
Expand Down Expand Up @@ -1907,18 +1909,26 @@ Object.defineProperty(expectPrototype, 'argTypes', {
},
});

function createTopLevelExpect({
assertions = {},
typeByName = { any: anyType },
types = [anyType],
output,
format = magicpen.defaultFormat,
installedPlugins = [],
} = {}) {
function createTopLevelExpect(
{
assertions = {},
typeByName = { any: anyType },
types = [anyType],
output,
format = magicpen.defaultFormat,
installedPlugins = [],
} = {},
parentExpect
) {
const expect = function (...args) {
return expect._expect(new Context(), args);
};
utils.setPrototypeOfOrExtend(expect, expectPrototype);
if (parentExpect) {
expect.parent = parentExpect;
utils.setPrototypeOfOrExtend(expect, parentExpect);
} else {
utils.setPrototypeOfOrExtend(expect, expectPrototype);
}

if (!output) {
output = magicpen();
Expand All @@ -1928,6 +1938,7 @@ function createTopLevelExpect({
return extend(expect, {
_topLevelExpect: expect,
_outputFormat: format,
_frozen: false,
assertions,
typeByName,
installedPlugins,
Expand Down
7 changes: 4 additions & 3 deletions test/api/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('hook', () => {
});

describe('with expect.child', () => {
it('should not affect child instances made before installing the hook', () => {
it('should affect child instances made before installing the hook', () => {
const parentExpect = expect.clone();
const childExpect = parentExpect.child();

Expand All @@ -59,7 +59,7 @@ describe('hook', () => {
});

childExpect(123, 'to equal', 123);
expect(called, 'to be false');
expect(called, 'to be true');
});

it('should not affect child instances made after installing the hook', () => {
Expand Down Expand Up @@ -128,7 +128,8 @@ describe('hook', () => {

// Regression test for https://gitter.im/unexpectedjs/unexpected?at=5fb42b73747be107c1c76095
it('should not break `this` in clones created after installing the hook', function () {
expect.hook(function (next) {
const parentExpect = expect.clone();
parentExpect.hook(function (next) {
return function (context, ...rest) {
return next(context, ...rest);
};
Expand Down

0 comments on commit 7299a38

Please sign in to comment.