Jest expect.any() 没有按预期工作
Jest expect.any() not working as expected
因此,当我在一个基于 Preact 的项目中测试我的一个 reducer 时(在使用 JEST 测试时与 React 没有太大区别),我遇到了这个问题:
运行 玩笑测试时出现以下输出 -
● 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>
.
expect.toEqual
检查 state.active
在你的情况下是否相等。要实现您想要的效果,您必须进行多个 expect
语句:
expect(state.active.active).toEqual(true)
expect(state.active.data).toEqual(expect.any(Array))
expect(state.active.ID).toEqual(expect.any(String))
您可以使用 toMatchObject
:
expect(state.active).toMatchObject({
active: true,
data: expect.any(Array),
ID: expect.any(String)
});
更多例子见官方文档:https://jestjs.io/docs/en/expect#tomatchobjectobject
因此,当我在一个基于 Preact 的项目中测试我的一个 reducer 时(在使用 JEST 测试时与 React 没有太大区别),我遇到了这个问题:
运行 玩笑测试时出现以下输出 -
● 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>
.
expect.toEqual
检查 state.active
在你的情况下是否相等。要实现您想要的效果,您必须进行多个 expect
语句:
expect(state.active.active).toEqual(true)
expect(state.active.data).toEqual(expect.any(Array))
expect(state.active.ID).toEqual(expect.any(String))
您可以使用 toMatchObject
:
expect(state.active).toMatchObject({
active: true,
data: expect.any(Array),
ID: expect.any(String)
});
更多例子见官方文档:https://jestjs.io/docs/en/expect#tomatchobjectobject