首页 > 解决方案 > 如何在排毒中使用 react-native-i18n[react-native]

问题描述

我想在 detox 中测试警报消息,并且消息使用 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”。我不知道为什么会发生错误,有人可以帮助我吗?谢谢

标签: reactjsreact-nativejestjse2e-testingdetox

解决方案


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

如果您有某些要求或原因迫使您保留node特定的旧版本,您可以通过不同的方式执行测试来绕过它:仅为 e2e 测试创建一个演示屏幕(或者甚至为 e2e 创建一个完整的演示项目) ,在演示屏幕中,您可以有一个按钮来执行您需要的i18n操作(更改语言环境或其他),并在detox测试中点击此“演示”按钮,然后再测试您真正想要的内容。


推荐阅读