Skip to content

Commit 773c119

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 (cherry picked from commit 194ac56)
1 parent 7419e4e commit 773c119

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

lib/createTopLevelExpect.js

+32-21
Original file line numberDiff line numberDiff line change
@@ -1584,30 +1584,32 @@ expectPrototype.clone = function () {
15841584

15851585
expectPrototype.child = function () {
15861586
this._assertTopLevelExpect();
1587-
const childExpect = createTopLevelExpect({
1588-
assertions: {},
1589-
types: [],
1590-
typeByName: {},
1591-
output: this.output.clone(),
1592-
format: this.outputFormat(),
1593-
installedPlugins: [],
1594-
});
1595-
const parent = (childExpect.parent = this);
1587+
const childExpect = createTopLevelExpect(
1588+
{
1589+
assertions: {},
1590+
types: [],
1591+
typeByName: {},
1592+
output: this.output.clone(),
1593+
format: this.outputFormat(),
1594+
installedPlugins: [],
1595+
},
1596+
this
1597+
);
15961598

15971599
childExpect.exportAssertion = function (testDescription, handler) {
1598-
parent.addAssertion(testDescription, handler, childExpect);
1600+
childExpect.parent.addAssertion(testDescription, handler, childExpect);
15991601
return this;
16001602
};
16011603
childExpect.exportType = function (type) {
16021604
if (childExpect.getType(type.name) !== type) {
16031605
childExpect.addType(type);
16041606
}
16051607

1606-
parent.addType(type, childExpect);
1608+
childExpect.parent.addType(type, childExpect);
16071609
return this;
16081610
};
16091611
childExpect.exportStyle = function (name, handler, allowRedefinition) {
1610-
parent.addStyle(
1612+
childExpect.parent.addStyle(
16111613
name,
16121614
function (...args) {
16131615
const childOutput = childExpect.createOutput(this.format);
@@ -1907,18 +1909,26 @@ Object.defineProperty(expectPrototype, 'argTypes', {
19071909
},
19081910
});
19091911

1910-
function createTopLevelExpect({
1911-
assertions = {},
1912-
typeByName = { any: anyType },
1913-
types = [anyType],
1914-
output,
1915-
format = magicpen.defaultFormat,
1916-
installedPlugins = [],
1917-
} = {}) {
1912+
function createTopLevelExpect(
1913+
{
1914+
assertions = {},
1915+
typeByName = { any: anyType },
1916+
types = [anyType],
1917+
output,
1918+
format = magicpen.defaultFormat,
1919+
installedPlugins = [],
1920+
} = {},
1921+
parentExpect
1922+
) {
19181923
const expect = function (...args) {
19191924
return expect._expect(new Context(), args);
19201925
};
1921-
utils.setPrototypeOfOrExtend(expect, expectPrototype);
1926+
if (parentExpect) {
1927+
expect.parent = parentExpect;
1928+
utils.setPrototypeOfOrExtend(expect, parentExpect);
1929+
} else {
1930+
utils.setPrototypeOfOrExtend(expect, expectPrototype);
1931+
}
19221932

19231933
if (!output) {
19241934
output = magicpen();
@@ -1928,6 +1938,7 @@ function createTopLevelExpect({
19281938
return extend(expect, {
19291939
_topLevelExpect: expect,
19301940
_outputFormat: format,
1941+
_frozen: false,
19311942
assertions,
19321943
typeByName,
19331944
installedPlugins,

0 commit comments

Comments
 (0)