@@ -4,7 +4,6 @@ const magicpen = require('magicpen');
44const extend = utils . extend ;
55const ukkonen = require ( 'ukkonen' ) ;
66const makePromise = require ( './makePromise' ) ;
7- const addAdditionalPromiseMethods = require ( './addAdditionalPromiseMethods' ) ;
87const wrapPromiseIfNecessary = require ( './wrapPromiseIfNecessary' ) ;
98const oathbreaker = require ( './oathbreaker' ) ;
109const UnexpectedError = require ( './UnexpectedError' ) ;
@@ -1321,6 +1320,112 @@ expectPrototype.setErrorMessage = function (err) {
13211320 err . serializeMessage ( this . outputFormat ( ) ) ;
13221321} ;
13231322
1323+ expectPrototype . addAdditionalPromiseMethods = function (
1324+ promise ,
1325+ expect ,
1326+ subject ,
1327+ args ,
1328+ wrappedExpect
1329+ ) {
1330+ // const expect = this;
1331+ promise . and = function ( ...args ) {
1332+ function executeAnd ( ) {
1333+ if ( expect . findTypeOf ( args [ 0 ] ) . is ( 'expect.it' ) ) {
1334+ return expect . addAdditionalPromiseMethods (
1335+ args [ 0 ] ( subject ) ,
1336+ expect ,
1337+ subject
1338+ ) ;
1339+ } else {
1340+ return expect ( subject , ...args ) ;
1341+ }
1342+ }
1343+
1344+ if ( this . isFulfilled ( ) ) {
1345+ return executeAnd ( ) ;
1346+ } else {
1347+ return expect . addAdditionalPromiseMethods (
1348+ this . then ( executeAnd ) ,
1349+ expect ,
1350+ subject
1351+ ) ;
1352+ }
1353+ } ;
1354+
1355+ const camelCaseMethods = expect . _camelCaser ( expect . context || new Context ( ) ) ;
1356+ Object . keys ( camelCaseMethods ) . forEach ( ( methodName ) => {
1357+ promise [ methodName ] = function ( ...args ) {
1358+ function execute ( shiftedSubject ) {
1359+ if ( expect . findTypeOf ( args [ 0 ] ) . is ( 'expect.it' ) ) {
1360+ return expect . addAdditionalPromiseMethods (
1361+ args [ 0 ] ( shiftedSubject ) ,
1362+ expect ,
1363+ subject
1364+ ) ;
1365+ } else {
1366+ if ( wrappedExpect ) {
1367+ return wrappedExpect . shifty (
1368+ shiftedSubject ,
1369+ wrappedExpect . args || [ ] ,
1370+ [
1371+ methodName . replace ( / [ A - Z ] / g, ( $0 ) => ` ${ $0 . toLowerCase ( ) } ` ) ,
1372+ ...args ,
1373+ ] ,
1374+ true // legacy mode (FIXME)
1375+ ) ;
1376+ } else {
1377+ // find ud af at fange konteksten
1378+ return expect . _executeExpect (
1379+ new Context ( ) ,
1380+ shiftedSubject ,
1381+ methodName . replace ( / [ A - Z ] / g, ( $0 ) => ` ${ $0 . toLowerCase ( ) } ` ) ,
1382+ args ,
1383+ { } // forwardedFlags
1384+ ) ;
1385+ }
1386+ }
1387+ }
1388+
1389+ if ( this . isFulfilled ( ) ) {
1390+ return execute ( this . value ( ) ) ;
1391+ } else {
1392+ return expect . addAdditionalPromiseMethods (
1393+ this . then ( execute ) ,
1394+ expect ,
1395+ subject
1396+ ) ;
1397+ }
1398+ } ;
1399+
1400+ promise [ `and${ methodName . replace ( / ^ [ a - z ] / , ( $0 ) => $0 . toUpperCase ( ) ) } ` ] =
1401+ function ( ...args ) {
1402+ function execute ( ) {
1403+ if ( expect . findTypeOf ( args [ 0 ] ) . is ( 'expect.it' ) ) {
1404+ return expect . addAdditionalPromiseMethods (
1405+ args [ 0 ] ( subject ) ,
1406+ expect ,
1407+ subject
1408+ ) ;
1409+ } else {
1410+ return camelCaseMethods [ methodName ] ( subject ) ( ...args ) ;
1411+ }
1412+ }
1413+
1414+ if ( this . isFulfilled ( ) ) {
1415+ return execute ( this . value ( ) ) ;
1416+ } else {
1417+ return expect . addAdditionalPromiseMethods (
1418+ this . then ( execute ) ,
1419+ expect ,
1420+ subject
1421+ ) ;
1422+ }
1423+ } ;
1424+ } ) ;
1425+
1426+ return promise ;
1427+ } ;
1428+
13241429expectPrototype . _createWrappedExpect = function (
13251430 assertionRule ,
13261431 subject ,
@@ -1336,7 +1441,7 @@ expectPrototype._createWrappedExpect = function (
13361441 if ( arguments . length === 0 ) {
13371442 throw new Error ( 'The expect function requires at least one parameter.' ) ;
13381443 } else if ( arguments . length === 1 ) {
1339- return addAdditionalPromiseMethods (
1444+ return wrappedExpect . addAdditionalPromiseMethods (
13401445 makePromise . resolve ( subject ) ,
13411446 wrappedExpect ,
13421447 subject
@@ -1474,7 +1579,7 @@ expectPrototype._executeExpect = function (
14741579 testDescriptionString ,
14751580 ...args ,
14761581 ] ) ,
1477- expect : wrappedExpect ,
1582+ wrappedExpect,
14781583 } ;
14791584 }
14801585
@@ -1560,7 +1665,7 @@ expectPrototype._expect = function (context, args, forwardedFlags) {
15601665 } else {
15611666 result = makePromise . resolve ( result ) ;
15621667 }
1563- return addAdditionalPromiseMethods (
1668+ return wrappedExpect . addAdditionalPromiseMethods (
15641669 result ,
15651670 this ,
15661671 subject ,
@@ -1834,7 +1939,7 @@ expectPrototype._callInNestedContext = function (callback) {
18341939 } else {
18351940 result = makePromise . resolve ( result ) ;
18361941 }
1837- return addAdditionalPromiseMethods ( result , this . execute , this . subject ) ;
1942+ return this . addAdditionalPromiseMethods ( result , this . execute , this . subject ) ;
18381943 } catch ( e ) {
18391944 if ( e && e . _isUnexpected ) {
18401945 const wrappedError = new UnexpectedError ( this , e ) ;
0 commit comments