Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest expect.any() not working as expected

So when testing one of my reducers in a Preact(not much different to React while testing with JEST) based project, I got bumped into this issue:

Following output comes up when running jest test -

● should setup       expect(received).toEqual(expected)      Expected value to equal:       {"ID": Any<String>, "active": true, "data": Any<Array>}     Received:       {"ID": "BysnEuMlm", "active": true, "data": [{"ID": "Hy7wMAz1lm", "code": "dat.fle", "label": "CRES/datum14.cdata", "name": "File", "status": "READY", "value": {"format": "cdata", "name": "datum14.cdata", "path": "CRES"}}, {"ID": "rkB7RMkeX", "code": "prp.kcv", "label": "3 folds", "name": "k-Fold Cross-Validation", "status": "READY", "value": "3"}, {"ID": "ByCmRfygQ", "code": "ats", "label": undefined, "name": " Best First +  Cfs Subset Eval", "status": "READY", "value": {"evaluator": {"name": "CfsSubsetEval"}, "search": {"name": "BestFirst", "options": ["-D", "1", "-N", "5"]}, "use": true}}, {"ID": "HkmVAM1l7", "code": "lrn", "label": undefined, "name": "Naive Bayes", "status": "READY", "value": {"label": "Naive Bayes", "name": "bayes.NaiveBayes", "use": true}}], "output": {"format": "pipeline", "name": "jestReact.cpipe", "path": "/home/rupav/opensource/candis/CRES"}}      Difference:      - Expected     + Received        Object {     -   "ID": Any<String>,     +   "ID": "BysnEuMlm",         "active": true,     -   "data": Any<Array>,     +   "data": Array [     +     Object {     +       "ID": "Hy7wMAz1lm",     +       "code": "dat.fle",     +       "label": "CRES/datum14.cdata",     +       "name": "File",     +       "status": "READY",     +       "value": Object {     +         "format": "cdata",     +         "name": "datum14.cdata",     +         "path": "CRES",     +       },     +     },     +     Object {     +       "ID": "rkB7RMkeX",     +       "code": "prp.kcv",     +       "label": "3 folds",     +       "name": "k-Fold Cross-Validation",     +       "status": "READY",     +       "value": "3",     +     },     +     Object {     +       "ID": "ByCmRfygQ",     +       "code": "ats",     +       "label": undefined,     +       "name": " Best First +  Cfs Subset Eval",     +       "status": "READY",     +       "value": Object {     +         "evaluator": Object {     +           "name": "CfsSubsetEval",     +         },     +         "search": Object {     +           "name": "BestFirst",     +           "options": Array [     +             "-D",     +             "1",     +             "-N",     +             "5",     +           ],     +         },     +         "use": true,     +       },     +     },     +     Object {     +       "ID": "HkmVAM1l7",     +       "code": "lrn",     +       "label": undefined,     +       "name": "Naive Bayes",     +       "status": "READY",     +       "value": Object {     +         "label": "Naive Bayes",     +         "name": "bayes.NaiveBayes",     +         "use": true,     +       },     +     },     +   ],     +   "output": Object {     +     "format": "pipeline",     +     "name": "jestReact.cpipe",     +     "path": "/home/rupav/opensource/candis/CRES",     +   },       } 

Following is the test case:

test('should setup ', () => {     const state = documentProcessor(         undefined,         {             type: ActionType.Asynchronous.READ_SUCCESS,             payload: dokuments.active     })     // expect(state.active.ID).toEqual(expect.any(String)) - Test case passes iff I run this test with this command only.     expect(state.active).toEqual({         data: expect.any(Array),         active: true,         ID: expect.any(String),     })  }) 

Since state gets changed while calling that reducer, I needed to use expect.any function, but as per the output, although types are same, test is not getting passed. Rather in expected its showing up Any<String>.

like image 690
rupav jain Avatar asked Jun 04 '18 08:06

rupav jain


People also ask

How do you expect a function in Jest?

The expect function is used every time you want to test a value. You will rarely call expect by itself. Instead, you will use expect along with a "matcher" function to assert something about a value. expect(bestLaCroixFlavor()).

What is expect ()?

Expect. assertions(number) verifies that a certain number of assertions are called during a test. This is often useful when testing asynchronous code, in order to make sure that assertions in a callback actually got called. So to put in other words, expect.

Which assertion used to test a value is with exact equality in Jest?

Jest documentation reads: toBe just checks that a value is what you expect. It uses === to check strict equality.

How do you check for errors in Jest?

In Jest you have to pass a function into expect(function). toThrow(<blank or type of error>) . Example: test("Test description", () => { const t = () => { throw new TypeError(); }; expect(t).


1 Answers

expect.toEqual checks for equality of state.active in your case. To achieve what you want, you have to make multiple expect statements:

expect(state.active.active).toEqual(true) expect(state.active.data).toEqual(expect.any(Array)) expect(state.active.ID).toEqual(expect.any(String)) 
like image 109
lipp Avatar answered Oct 01 '22 00:10

lipp