因此,当在Preact中测试我的 reducer 之一(与使用JEST进行测试时与React差别不大)时,我遇到了这个问题:
运行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",
+ },
}
以下是测试用例:
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),
})
})
由于在调用reducer时状态发生了变化,我需要使用expect.any
函数,但是根据输出,尽管类型相同,但测试没有通过。
而是期望它出现Any<String>
。
答案 0 :(得分:4)
您可以使用toMatchObject
:
expect(state.active).toMatchObject({
active: true,
data: expect.any(Array),
ID: expect.any(String)
});
请参阅官方文档中的更多示例:https://jestjs.io/docs/en/expect#tomatchobjectobject
答案 1 :(得分:0)
expect.toEqual
检查您案件中state.active
的相等性。要实现您的目标,您必须制作多个expect
语句:
expect(state.active.active).toEqual(true)
expect(state.active.data).any(Array)
expect(state.active.ID).any(String)