【问题标题】:How to use react-native-i18n in detox[react-native]如何在排毒中使用 react-native-i18n[react-native]
【发布时间】:2019-02-17 21:46:06
【问题描述】:

我想在 detox 中测试 alert 消息,消息使用 i18n。

const i18n = require("react-native-i18n");

describe("Example", () => {
  beforeEach(async () => {
    await device.reloadReactNative();
  });

  it("should show hello screen after tap", async () => {
    await element(by.id("btnLogin")).tap();
    I18n.t(LocaleKeys.errorMsg_invalidUsername);
    await expect(element(by.text(I18n.t(LocaleKeys.errorMsg_invalidUsername)))).toBeVisible();
    // await expect(element(by.text("Please input the email and password."))).toBeVisible();
  });

});

运行测试并得到以下错误。

测试套件无法运行

/Users/leogeng/Desktop/studentREP/student-app/node_modules/react-native-i18n/index.js:14
export const getLanguages = () => RNI18n.getLanguages();
^^^^^^

SyntaxError: Unexpected token export

  at ScriptTransformer._transformAndBuildScript (../node_modules/jest-runtime/build/script_transformer.js:305:17)
  at Object.<anonymous> (firstTest.spec.js:1:114)
      at Generator.next (<anonymous>)

然后我为玩笑添加以下代码:

{
  "preset": "react-native",
  "transformIgnorePatterns": [
    "/node_modules/(?!(react-native(.*)?/|native-base(.*)?/|react-navigation/))"
  ]
}

又报错:

 Validation Error:

  Module <rootDir>/node_modules/react-native/jest/setup.js in the setupFiles option was not found.

实际上,我确认 node_modules/react-native/jest 中存在“setup,js”。 我不知道为什么会发生错误,有人可以帮助我吗? 谢谢

【问题讨论】:

    标签: reactjs react-native jestjs e2e-testing detox


    【解决方案1】:

    很可能是因为您使用的是旧版本的node,请尝试更新,看看是否能解决问题。此外,它与Jest 完全无关,如果您对 Jest 单元测试没有任何问题,您可能应该恢复修改 Jest 设置的尝试;无论如何,它不会解决排毒问题。

    如果您有某些要求或原因迫使您将node 保留在特定的旧版本,您可以通过不同的方式执行测试来绕过它:只为 e2e 测试提供演示屏幕(甚至创建一个整体仅用于 e2e 的演示项目),在演示屏幕中,您可以有一个按钮来执行您所需要的 i18n(更改语言环境或其他),在 detox 测试中,您在测试之前点击此“演示”按钮真的想要。

    【讨论】:

      【解决方案2】:

      我也遇到了同样的问题。我通过导入 i18n-js 而不是 react-native-i18n 来解决它。

      因为react-native-i18n 不是纯javascript 框架,所以Detox 无法导入。

      但是react-native-i18n 使用的是i18n-js,所以您可以毫无问题地访问您的翻译

      const I18n = require('i18n-js')
      
      // and then you can use it for your tests
      ...
      await element(by.text( I18n.t('hello') )).tap()
      

      【讨论】:

        猜你喜欢
        • 2020-02-21
        • 1970-01-01
        • 1970-01-01
        • 2021-02-21
        • 1970-01-01
        • 2018-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多