@@ -194,17 +194,112 @@ describe('Matchers', () => {
194194 expectFailure ( matcher , { foo : 'bar' , baz : 'qux' } , [ / U n e x p e c t e d k e y a t \/ b a z / ] ) ;
195195 } ) ;
196196 } ) ;
197+
198+ describe ( 'not()' , ( ) => {
199+ let matcher : Matcher ;
200+
201+ test ( 'literal' , ( ) => {
202+ matcher = Match . not ( 'foo' ) ;
203+ expectPass ( matcher , 'bar' ) ;
204+ expectPass ( matcher , 3 ) ;
205+
206+ expectFailure ( matcher , 'foo' , [ 'Found unexpected match: "foo"' ] ) ;
207+ } ) ;
208+
209+ test ( 'object' , ( ) => {
210+ matcher = Match . not ( { foo : 'bar' } ) ;
211+ expectPass ( matcher , 'bar' ) ;
212+ expectPass ( matcher , 3 ) ;
213+ expectPass ( matcher , { foo : 'baz' } ) ;
214+ expectPass ( matcher , { bar : 'foo' } ) ;
215+
216+ const msg = [
217+ 'Found unexpected match: {' ,
218+ ' "foo": "bar"' ,
219+ '}' ,
220+ ] . join ( '\n' ) ;
221+ expectFailure ( matcher , { foo : 'bar' } , [ msg ] ) ;
222+ } ) ;
223+
224+ test ( 'array' , ( ) => {
225+ matcher = Match . not ( [ 'foo' , 'bar' ] ) ;
226+ expectPass ( matcher , 'foo' ) ;
227+ expectPass ( matcher , [ ] ) ;
228+ expectPass ( matcher , [ 'bar' ] ) ;
229+ expectPass ( matcher , [ 'foo' , 3 ] ) ;
230+
231+ const msg = [
232+ 'Found unexpected match: [' ,
233+ ' "foo",' ,
234+ ' "bar"' ,
235+ ']' ,
236+ ] . join ( '\n' ) ;
237+ expectFailure ( matcher , [ 'foo' , 'bar' ] , [ msg ] ) ;
238+ } ) ;
239+
240+ test ( 'as a nested matcher' , ( ) => {
241+ matcher = Match . exact ( {
242+ foo : { bar : Match . not ( [ 1 , 2 ] ) } ,
243+ } ) ;
244+
245+ expectPass ( matcher , {
246+ foo : { bar : [ 1 ] } ,
247+ } ) ;
248+ expectPass ( matcher , {
249+ foo : { bar : [ 'baz' ] } ,
250+ } ) ;
251+
252+ const msg = [
253+ 'Found unexpected match: [' ,
254+ ' 1,' ,
255+ ' 2' ,
256+ '] at /foo/bar' ,
257+ ] . join ( '\n' ) ;
258+ expectFailure ( matcher , {
259+ foo : { bar : [ 1 , 2 ] } ,
260+ } , [ msg ] ) ;
261+ } ) ;
262+
263+ test ( 'with nested matcher' , ( ) => {
264+ matcher = Match . not ( {
265+ foo : { bar : Match . arrayWith ( [ 1 ] ) } ,
266+ } ) ;
267+
268+ expectPass ( matcher , {
269+ foo : { bar : [ 2 ] } ,
270+ } ) ;
271+ expectPass ( matcher , 'foo' ) ;
272+
273+ const msg = [
274+ 'Found unexpected match: {' ,
275+ ' "foo": {' ,
276+ ' "bar": [' ,
277+ ' 1,' ,
278+ ' 2' ,
279+ ' ]' ,
280+ ' }' ,
281+ '}' ,
282+ ] . join ( '\n' ) ;
283+ expectFailure ( matcher , {
284+ foo : { bar : [ 1 , 2 ] } ,
285+ } , [ msg ] ) ;
286+ } ) ;
287+ } ) ;
197288} ) ;
198289
199290function expectPass ( matcher : Matcher , target : any ) : void {
200291 expect ( matcher . test ( target ) . hasFailed ( ) ) . toEqual ( false ) ;
201292}
202293
203- function expectFailure ( matcher : Matcher , target : any , expected : ( string | RegExp ) [ ] ) : void {
204- const actual = matcher . test ( target ) . toHumanStrings ( ) ;
294+ function expectFailure ( matcher : Matcher , target : any , expected : ( string | RegExp ) [ ] = [ ] ) : void {
295+ const result = matcher . test ( target ) ;
296+ expect ( result . failCount ) . toBeGreaterThan ( 0 ) ;
297+ const actual = result . toHumanStrings ( ) ;
298+ if ( expected . length > 0 ) {
299+ expect ( actual . length ) . toEqual ( expected . length ) ;
300+ }
205301 for ( let i = 0 ; i < expected . length ; i ++ ) {
206302 const e = expected [ i ] ;
207303 expect ( actual [ i ] ) . toMatch ( e ) ;
208304 }
209- expect ( expected . length ) . toEqual ( actual . length ) ;
210305}
0 commit comments