在玩笑测试中模拟“元素”

时间:2019-07-12 06:51:57

标签: jestjs detox

我有一个用于排毒测试的助手,该助手保存最常见动作的抽象。这样。

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

...解决这个问题?

0 个答案:

没有答案