jestjs - 在玩笑测试中模拟“元素”
问题描述
我有一个帮手用于排毒测试,它包含最常见动作的抽象。像这样。
/**
* Looks for a search input and inputs the query
*/
export const inputSearchQuery = async ({ query = '', placeholderText = '' }) => {
if (placeholderText) {
// look for search input
await expect(element(by.id(TestID.SEARCH_INPUT).withDescendant(by.text(placeholderText)))).toBeVisible();
// tap search input
await element(by.id(TestID.SEARCH_INPUT).withDescendant(by.text(placeholderText))).tap();
// type in query
await element(by.id(TestID.SEARCH_INPUT).withDescendant(by.text(placeholderText))).typeText(query);
} else {
// look for search input
await expect(element(by.id(TestID.SEARCH_INPUT))).toBeVisible();
// tap search input
await element(by.id(TestID.SEARCH_INPUT)).tap();
// type in query
await element(by.id(TestID.SEARCH_INPUT)).typeText(query);
}
};
我希望用 Jest 测试这个抽象层。所以我做了以下,
describe('e2e helper', () => {
describe('inputSearchQuery()', () => {
test('should check whether the search input is visible', async () => {
const mockToBeVisible = jest.fn();
await inputSearchQuery({});
expect(mockToBeVisible).toBeCalledTimes(1);
});
});
});
显然得到了错误,
ReferenceError:元素未定义
我明白这element
来自环境。如何配置我的 Jest 设置...
"jest": {
"preset": "react-native",
"snapshotSerializers": [
"./node_modules/enzyme-to-json/serializer"
],
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-navigation)"
],
"moduleNameMapper": {
"package.json": "<rootDir>/__mocks__/package.json"
},
"testPathIgnorePatterns": [
"/e2e/"
],
// I'm guessing something like this but doesn't work out of the box
"testEnvironment": "detox"
},
...解决这个问题?
解决方案
推荐阅读
- node.js - 在 Microsoft BotFramework v4 node.js 中使用 Luis 连接对话框
- laravel - Laravel 5.8 带密码重置的电子邮件验证
- scala - 将 Spark 数据帧转换为类似格式的 scala 字典
- spring-hateoas - 为什么 user.get() 方法给我错误?和 idk 如何谷歌
- python - 熊猫数据透视表上的数学计算
- powershell - 我需要在 PowerCLI 中显示一个值及其源主机的名称
- spring-boot - Spring Boot:如何根据配置文件更改注释的值?
- python - 如何正确关闭类打开的文件?
- php - PHP 处理 $_POST 数组
- reactjs - ComponentDidUpdate 使用 setState 调用无限次