Skip to content

Commit 194ac56

Browse files
committed
Make a child expect's prototype chain include the parent expect
... so that the expect.it equivalent of subsequently added assertions are available as childExpect.toWhatever
1 parent a055b97 commit 194ac56

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

lib/createTopLevelExpect.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,30 +1663,32 @@ expectPrototype.clone = function () {
16631663

16641664
expectPrototype.child = function () {
16651665
this._assertTopLevelExpect();
1666-
const childExpect = createTopLevelExpect({
1667-
assertions: {},
1668-
types: [],
1669-
typeByName: {},
1670-
output: this.output.clone(),
1671-
format: this.outputFormat(),
1672-
installedPlugins: [],
1673-
});
1674-
const parent = (childExpect.parent = this);
1666+
const childExpect = createTopLevelExpect(
1667+
{
1668+
assertions: {},
1669+
types: [],
1670+
typeByName: {},
1671+
output: this.output.clone(),
1672+
format: this.outputFormat(),
1673+
installedPlugins: [],
1674+
},
1675+
this
1676+
);
16751677

16761678
childExpect.exportAssertion = function (testDescription, handler) {
1677-
parent.addAssertion(testDescription, handler, childExpect);
1679+
childExpect.parent.addAssertion(testDescription, handler, childExpect);
16781680
return this;
16791681
};
16801682
childExpect.exportType = function (type) {
16811683
if (childExpect.getType(type.name) !== type) {
16821684
childExpect.addType(type);
16831685
}
16841686

1685-
parent.addType(type, childExpect);
1687+
childExpect.parent.addType(type, childExpect);
16861688
return this;
16871689
};
16881690
childExpect.exportStyle = function (name, handler, allowRedefinition) {
1689-
parent.addStyle(
1691+
childExpect.parent.addStyle(
16901692
name,
16911693
function (...args) {
16921694
const childOutput = childExpect.createOutput(this.format);
@@ -1986,18 +1988,26 @@ Object.defineProperty(expectPrototype, 'argTypes', {
19861988
},
19871989
});
19881990

1989-
function createTopLevelExpect({
1990-
assertions = {},
1991-
typeByName = { any: anyType },
1992-
types = [anyType],
1993-
output,
1994-
format = magicpen.defaultFormat,
1995-
installedPlugins = [],
1996-
} = {}) {
1991+
function createTopLevelExpect(
1992+
{
1993+
assertions = {},
1994+
typeByName = { any: anyType },
1995+
types = [anyType],
1996+
output,
1997+
format = magicpen.defaultFormat,
1998+
installedPlugins = [],
1999+
} = {},
2000+
parentExpect
2001+
) {
19972002
const expect = function (...args) {
19982003
return expect._expect(new Context(), args);
19992004
};
2000-
utils.setPrototypeOfOrExtend(expect, expectPrototype);
2005+
if (parentExpect) {
2006+
expect.parent = parentExpect;
2007+
utils.setPrototypeOfOrExtend(expect, parentExpect);
2008+
} else {
2009+
utils.setPrototypeOfOrExtend(expect, expectPrototype);
2010+
}
20012011

20022012
if (!output) {
20032013
output = magicpen();
@@ -2007,6 +2017,7 @@ function createTopLevelExpect({
20072017
return extend(expect, {
20082018
_topLevelExpect: expect,
20092019
_outputFormat: format,
2020+
_frozen: false,
20102021
assertions,
20112022
typeByName,
20122023
installedPlugins,

0 commit comments

Comments
 (0)