Skip to content

Commit 7299a38

Browse files
authored
Merge pull request #785 from unexpectedjs/feature/childInheritsFromParentExpect
Add the parent expect to the prototype chain of a child expect
2 parents 64aeaad + 16e4b28 commit 7299a38

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
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,

test/api/hook.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('hook', () => {
4646
});
4747

4848
describe('with expect.child', () => {
49-
it('should not affect child instances made before installing the hook', () => {
49+
it('should affect child instances made before installing the hook', () => {
5050
const parentExpect = expect.clone();
5151
const childExpect = parentExpect.child();
5252

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

6161
childExpect(123, 'to equal', 123);
62-
expect(called, 'to be false');
62+
expect(called, 'to be true');
6363
});
6464

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

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

0 commit comments

Comments
 (0)