首页 > 解决方案 > 在玩笑测试中模拟“元素”

问题描述

我有一个帮手用于排毒测试,它包含最常见动作的抽象。像这样。

/**
 * 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"
      },

...解决这个问题?

标签: jestjsdetox

解决方案


推荐阅读