@@ -4,7 +4,6 @@ const magicpen = require('magicpen');
4
4
const extend = utils . extend ;
5
5
const ukkonen = require ( 'ukkonen' ) ;
6
6
const makePromise = require ( './makePromise' ) ;
7
- const addAdditionalPromiseMethods = require ( './addAdditionalPromiseMethods' ) ;
8
7
const wrapPromiseIfNecessary = require ( './wrapPromiseIfNecessary' ) ;
9
8
const oathbreaker = require ( './oathbreaker' ) ;
10
9
const UnexpectedError = require ( './UnexpectedError' ) ;
@@ -1321,6 +1320,112 @@ expectPrototype.setErrorMessage = function (err) {
1321
1320
err . serializeMessage ( this . outputFormat ( ) ) ;
1322
1321
} ;
1323
1322
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
+
1324
1429
expectPrototype . _createWrappedExpect = function (
1325
1430
assertionRule ,
1326
1431
subject ,
@@ -1336,7 +1441,7 @@ expectPrototype._createWrappedExpect = function (
1336
1441
if ( arguments . length === 0 ) {
1337
1442
throw new Error ( 'The expect function requires at least one parameter.' ) ;
1338
1443
} else if ( arguments . length === 1 ) {
1339
- return addAdditionalPromiseMethods (
1444
+ return wrappedExpect . addAdditionalPromiseMethods (
1340
1445
makePromise . resolve ( subject ) ,
1341
1446
wrappedExpect ,
1342
1447
subject
@@ -1474,7 +1579,7 @@ expectPrototype._executeExpect = function (
1474
1579
testDescriptionString ,
1475
1580
...args ,
1476
1581
] ) ,
1477
- expect : wrappedExpect ,
1582
+ wrappedExpect,
1478
1583
} ;
1479
1584
}
1480
1585
@@ -1560,7 +1665,7 @@ expectPrototype._expect = function (context, args, forwardedFlags) {
1560
1665
} else {
1561
1666
result = makePromise . resolve ( result ) ;
1562
1667
}
1563
- return addAdditionalPromiseMethods (
1668
+ return wrappedExpect . addAdditionalPromiseMethods (
1564
1669
result ,
1565
1670
this ,
1566
1671
subject ,
@@ -1834,7 +1939,7 @@ expectPrototype._callInNestedContext = function (callback) {
1834
1939
} else {
1835
1940
result = makePromise . resolve ( result ) ;
1836
1941
}
1837
- return addAdditionalPromiseMethods ( result , this . execute , this . subject ) ;
1942
+ return this . addAdditionalPromiseMethods ( result , this . execute , this . subject ) ;
1838
1943
} catch ( e ) {
1839
1944
if ( e && e . _isUnexpected ) {
1840
1945
const wrappedError = new UnexpectedError ( this , e ) ;
0 commit comments