Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed May 30, 2022
1 parent 0cabe00 commit 4ef1622
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions lib/createTopLevelExpect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1322,20 +1322,16 @@ expectPrototype.setErrorMessage = function (err) {

expectPrototype.addAdditionalPromiseMethods = function (
promise,
expect,
subject,
args,
wrappedExpect
) {
const stack = new Error('hey').stack;
const wrappedExpect = this;
const expect = this._topLevelExpect;
// const expect = this;
promise.and = function (...args) {
function executeAnd() {
if (expect.findTypeOf(args[0]).is('expect.it')) {
return expect.addAdditionalPromiseMethods(
args[0](subject),
expect,
subject
);
return expect.addAdditionalPromiseMethods(args[0](subject), subject);
} else {
return expect(subject, ...args);
}
Expand All @@ -1344,11 +1340,7 @@ expectPrototype.addAdditionalPromiseMethods = function (
if (this.isFulfilled()) {
return executeAnd();
} else {
return expect.addAdditionalPromiseMethods(
this.then(executeAnd),
expect,
subject
);
return expect.addAdditionalPromiseMethods(this.then(executeAnd), subject);
}
};

Expand All @@ -1359,14 +1351,13 @@ expectPrototype.addAdditionalPromiseMethods = function (
if (expect.findTypeOf(args[0]).is('expect.it')) {
return expect.addAdditionalPromiseMethods(
args[0](shiftedSubject),
expect,
subject
);
} else {
if (wrappedExpect) {
if (wrappedExpect !== expect) {
return wrappedExpect.shifty(
shiftedSubject,
wrappedExpect.args || [],
wrappedExpect.args,
[
methodName.replace(/[A-Z]/g, ($0) => ` ${$0.toLowerCase()}`),
...args,
Expand All @@ -1389,11 +1380,7 @@ expectPrototype.addAdditionalPromiseMethods = function (
if (this.isFulfilled()) {
return execute(this.value());
} else {
return expect.addAdditionalPromiseMethods(
this.then(execute),
expect,
subject
);
return expect.addAdditionalPromiseMethods(this.then(execute), subject);
}
};

Expand All @@ -1403,7 +1390,6 @@ expectPrototype.addAdditionalPromiseMethods = function (
if (expect.findTypeOf(args[0]).is('expect.it')) {
return expect.addAdditionalPromiseMethods(
args[0](subject),
expect,
subject
);
} else {
Expand All @@ -1416,7 +1402,6 @@ expectPrototype.addAdditionalPromiseMethods = function (
} else {
return expect.addAdditionalPromiseMethods(
this.then(execute),
expect,
subject
);
}
Expand All @@ -1441,9 +1426,8 @@ expectPrototype._createWrappedExpect = function (
if (arguments.length === 0) {
throw new Error('The expect function requires at least one parameter.');
} else if (arguments.length === 1) {
return wrappedExpect.addAdditionalPromiseMethods(
return parentExpect.addAdditionalPromiseMethods(
makePromise.resolve(subject),
wrappedExpect,
subject
);
} else if (typeof testDescriptionString === 'function') {
Expand Down Expand Up @@ -1597,13 +1581,9 @@ expectPrototype._executeExpect = function (
} else {
result = makePromise.resolve(result);
}
// Her mangler vi wrappedExpect?
return wrappedExpect.addAdditionalPromiseMethods(
result,
this,
subject,
Array.prototype.slice.call(args, 0, 2),
wrappedExpect
);
};

Expand Down Expand Up @@ -1936,7 +1916,7 @@ expectPrototype._callInNestedContext = function (callback) {
} else {
result = makePromise.resolve(result);
}
return this.addAdditionalPromiseMethods(result, this.execute, this.subject);
return this.addAdditionalPromiseMethods(result, this.subject);
} catch (e) {
if (e && e._isUnexpected) {
const wrappedError = new UnexpectedError(this, e);
Expand Down

0 comments on commit 4ef1622

Please sign in to comment.