如何在 async jest test.each 案例中传递 done() 参数
How to pass in the done() parameter on an async jest test.each case
我正在尝试编写一个测试异步方法的 jest 测试用例,我想传入 done()
参数,以便 jest 在结束测试之前等待它被触发,但是,我'我不知道该放在哪里。
有什么想法吗?
const testcases = [
[
'Crew',
[1,2,3],
Enum.Level1
],
[
'Staff',
[4,5,6],
Enum.Level2
]
];
test.each(testcases )(
'Should be able to load differing cases %p',
(
typeName: string,
initalVals: string[],
type: LevelType
) => {
// some call that updates mobx store state
when(
() => mobxstoreProperty.length == initalVals.length,
() => {
// my assertions
done();
}
);
}
);
对于一个笑话测试,我可以这样做:
test('my single test', done => {
// some call that updates mobx store state
when(
() => mobxstoreProperty.length == initalVals.length,
() => {
// my assertions
done();
}
);
});
只是不确定在使用 test.each
方法时该怎么做。
我使用命名参数,我可以添加 done()
方法作为最后一个函数参数。例如像这样:
const testcases: {
typeName: string;
initalVals: string[],
type: LevelType
}[] = [
{
typeName: 'Crew',
initalVals: [1,2,3],
type: Enum.Level1
},
{
typeName: 'Staff',
initalVals: [4,5,6],
type: Enum.Level2
},
];
test.each(testcases)(
'Should be able to load differing cases %p',
// Must use `any` for `done`, as TypeScript infers the wrong type:
({typeName, initalVals, type}, done: any) => {
// some call that updates mobx store state
when(
() => mobxstoreProperty.length == initalVals.length,
() => {
// my assertions
done();
}
);
}
);
我还没有测试过你是否可以将 done()
方法添加为带有数组参数的最后一个参数,但也许这也有效。
要传递和评估 done
,done
回调应该是测试用例参数函数中的最后一个参数。
还有,当你在 typescript 中使用 test.each
方法时,这里是处理打字的方法:
// found at https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34617
it.each<number | jest.DoneCallback>([1, 2, 3])(
'dummy: %d',
(num: number, done: jest.DoneCallback) => {
done();
},
);
目前没有完美的答案,因为 Jest 库中存在模板类型 test.each()
的问题
所以现在,所有实现您想要的解决方案都需要对类型做一些技巧。
复杂测试参数的解决方案,采用轻数组语法定义:
test.each<any>([
["Crew", [1, 2, 3], LevelType.Level1],
["Staff", [4, 5, 6], LevelType.Level2],
])(
"Should be able to load differing cases %p",
(
typeName: string,
initalVals: string[],
type: LevelType,
done: jest.DoneCallback
) => {
// some test code
}
);
此解决方案的技巧是使用 test.each<any>
绕过模板化类型问题。
any
类型的使用不是最好的,所以我在等待库更新。
有关详细信息,请参阅打开的问题 https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34617 and https://github.com/facebook/jest/issues/8518
我正在尝试编写一个测试异步方法的 jest 测试用例,我想传入 done()
参数,以便 jest 在结束测试之前等待它被触发,但是,我'我不知道该放在哪里。
有什么想法吗?
const testcases = [
[
'Crew',
[1,2,3],
Enum.Level1
],
[
'Staff',
[4,5,6],
Enum.Level2
]
];
test.each(testcases )(
'Should be able to load differing cases %p',
(
typeName: string,
initalVals: string[],
type: LevelType
) => {
// some call that updates mobx store state
when(
() => mobxstoreProperty.length == initalVals.length,
() => {
// my assertions
done();
}
);
}
);
对于一个笑话测试,我可以这样做:
test('my single test', done => {
// some call that updates mobx store state
when(
() => mobxstoreProperty.length == initalVals.length,
() => {
// my assertions
done();
}
);
});
只是不确定在使用 test.each
方法时该怎么做。
我使用命名参数,我可以添加 done()
方法作为最后一个函数参数。例如像这样:
const testcases: {
typeName: string;
initalVals: string[],
type: LevelType
}[] = [
{
typeName: 'Crew',
initalVals: [1,2,3],
type: Enum.Level1
},
{
typeName: 'Staff',
initalVals: [4,5,6],
type: Enum.Level2
},
];
test.each(testcases)(
'Should be able to load differing cases %p',
// Must use `any` for `done`, as TypeScript infers the wrong type:
({typeName, initalVals, type}, done: any) => {
// some call that updates mobx store state
when(
() => mobxstoreProperty.length == initalVals.length,
() => {
// my assertions
done();
}
);
}
);
我还没有测试过你是否可以将 done()
方法添加为带有数组参数的最后一个参数,但也许这也有效。
要传递和评估 done
,done
回调应该是测试用例参数函数中的最后一个参数。
还有,当你在 typescript 中使用 test.each
方法时,这里是处理打字的方法:
// found at https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34617
it.each<number | jest.DoneCallback>([1, 2, 3])(
'dummy: %d',
(num: number, done: jest.DoneCallback) => {
done();
},
);
目前没有完美的答案,因为 Jest 库中存在模板类型 test.each()
的问题所以现在,所有实现您想要的解决方案都需要对类型做一些技巧。
复杂测试参数的解决方案,采用轻数组语法定义:
test.each<any>([
["Crew", [1, 2, 3], LevelType.Level1],
["Staff", [4, 5, 6], LevelType.Level2],
])(
"Should be able to load differing cases %p",
(
typeName: string,
initalVals: string[],
type: LevelType,
done: jest.DoneCallback
) => {
// some test code
}
);
此解决方案的技巧是使用 test.each<any>
绕过模板化类型问题。
any
类型的使用不是最好的,所以我在等待库更新。
有关详细信息,请参阅打开的问题 https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34617 and https://github.com/facebook/jest/issues/8518