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

Add the parent expect to the prototype chain of a child expect #785

Merged
merged 3 commits into from
Apr 26, 2022
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
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,
sunesimonsen marked this conversation as resolved.
Show resolved Hide resolved
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