@@ -17,6 +17,11 @@ const ValidationError = require('..').ValidationError;
17
17
18
18
const UUID_REGEXP = / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 1 - 5 ] [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / i;
19
19
20
+ const throwingSetter = ( value ) => {
21
+ if ( ! value ) return ; // no-op
22
+ throw new Error ( 'Intentional error triggered from a property setter' ) ;
23
+ } ;
24
+
20
25
describe ( 'manipulation' , function ( ) {
21
26
before ( function ( done ) {
22
27
db = getSchema ( ) ;
@@ -28,8 +33,11 @@ describe('manipulation', function() {
28
33
age : { type : Number , index : true } ,
29
34
dob : Date ,
30
35
createdAt : { type : Date , default : Date } ,
36
+ throwingSetter : { type : String , default : null } ,
31
37
} , { forceId : true , strict : true } ) ;
32
38
39
+ Person . setter . throwingSetter = throwingSetter ;
40
+
33
41
db . automigrate ( [ 'Person' ] , done ) ;
34
42
} ) ;
35
43
@@ -111,6 +119,11 @@ describe('manipulation', function() {
111
119
. catch ( done ) ;
112
120
} ) ;
113
121
122
+ it ( 'should return rejected promise when model initialization failed' , async ( ) => {
123
+ await Person . create ( { name : 'Sad Fail' , age : 25 , throwingSetter : 'something' } ) . should
124
+ . be . rejectedWith ( 'Intentional error triggered from a property setter' ) ;
125
+ } ) ;
126
+
114
127
it ( 'should instantiate an object' , function ( done ) {
115
128
const p = new Person ( { name : 'Anatoliy' } ) ;
116
129
p . name . should . equal ( 'Anatoliy' ) ;
@@ -1563,7 +1576,9 @@ describe('manipulation', function() {
1563
1576
Post = db . define ( 'Post' , {
1564
1577
title : { type : String , length : 255 } ,
1565
1578
content : { type : String } ,
1579
+ throwingSetter : { type : String , default : null } ,
1566
1580
} , { forceId : true } ) ;
1581
+ Post . setter . throwingSetter = throwingSetter ;
1567
1582
db . automigrate ( 'Post' , done ) ;
1568
1583
} ) ;
1569
1584
@@ -1598,11 +1613,19 @@ describe('manipulation', function() {
1598
1613
id : created . id ,
1599
1614
title : 'Draft' ,
1600
1615
content : 'a content' ,
1616
+ throwingSetter : null ,
1601
1617
} ) ;
1602
1618
1603
1619
// Verify that no warnings were triggered
1604
1620
Object . keys ( Post . _warned ) . should . be . empty ( ) ;
1605
1621
} ) ;
1622
+
1623
+ it ( 'should return rejected promise when model initialization failed' , async ( ) => {
1624
+ const firstNotFailedPost = await Post . create ( { title : 'Sad Post' } ) ; // no property with failing setter
1625
+ await Post . replaceById ( firstNotFailedPost . id , {
1626
+ title : 'Sad Post' , throwingSetter : 'somethingElse' ,
1627
+ } ) . should . be . rejectedWith ( 'Intentional error triggered from a property setter' ) ;
1628
+ } ) ;
1606
1629
} ) ;
1607
1630
1608
1631
describe ( 'findOrCreate' , function ( ) {
0 commit comments