与i18next相关的间歇性单元测试失败 [英] Intermittent unit-test failure associated with i18next

查看:165
本文介绍了与i18next相关的间歇性单元测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对我的aurelia自定义元素进行单元测试,如下所示。

I am trying to unit test my aurelia custom element, which looks like below.

// BaseText.ts
import { bindable } from "aurelia-framework";
import { BaseI18N } from "aurelia-i18n";

export class BaseText extends BaseI18N {
    @bindable public value: string;
    @bindable public i18nKey: string;
}

// NormalText.ts
export class NormalTextCustomElement extends BaseText {}

// NormalText.html
<template>
    <span t.bind="i18nKey">${value}</span>
</template>

现在,我想测试我是否更改了 i18nKey的值,翻译的文本显示在元素中。为了测试这一点,我写了下面的测试用例。

Now, I want to test if I change the value of i18nKey, the translated text is shown in the element. To test that, I have written the below test case.

describe("i18n specs", () => {

    let component;
    beforeEach(() => {
        component = StageComponent
            .withResources("NormalText/NormalText")
            .inView("<normal-text id='i18n1' value='Ignored Text' i18n-key='test'></normal-text>");

        component.bootstrap((aurelia: Aurelia) => aurelia.use
            .standardConfiguration()
            .plugin(PLATFORM.moduleName("aurelia-i18n"), (instance) => {
                const aliases = ["t"];
                TCustomAttribute.configureAliases(aliases);

                return instance.setup({
                    attributes: aliases,
                    fallbackLng: "en",
                    lng: "en",
                    resources: {         //<-- translation resources
                        en: {
                            translation: {
                                test: "English test"
                            }
                        }
                    }
                });
            }));

    });

    it("Should render the translated text with a i18nKey", (done) => {
        component
            .create(bootstrap)
            .then(() => {
                const spanElement = document.querySelector('normal-text#i18n1>span');
                expect(spanElement.textContent.trim()).toBe('English test');
            })
            .catch(e => { console.log(e.toString()) })
            .finally(() => {
                component.dispose();
                done();
            });
    });
});

现在的问题是这个测试用例间歇性地失败,这肯定会成为CI的一个问题。我怀疑它有一些事情要做i18next的初始化,并且测试用例在初始化完成之前运行。虽然我对这个假设不太确定。

Now the problem is that this test case fails intermittently, which surely is going to be a problem with CI. I suspect that it has something to do it the initialization of i18next, and the test case is running before the initialization is complete. Though I am not very sure about this hypothesis.

我应该改变什么,以便这个测试用例变得确定?

What should I change, so that this test case becomes deterministic?

其他信息:


  1. 此测试用例如果它在每个其他之前运行成功查看相关的测试用例

  2. 我创建了一个 GitHub repo ,让感兴趣的读者/用户可以重现问题。请记住,您可能多次运行测试以复制问题。

  1. This test case succeeds if it runs before every other view related test cases.
  2. I have created a GitHub repo, so that interested reader/user can reproduce the problem. Please keep in mind that you might have run the tests multiple times to replicate the issue.


推荐答案

事实证明,问题实际上与我编写测试用例的方式有关。

Turned out that the issue is actually related to how I was writing the test cases.

之前我的结构如下所示。

Previously I had structured it as below.

describe("View specs", ()=> {
  // here goes other test suite and/or test cases
  describe("i18n specs", () => {

    let component;
    beforeEach(() => {
      // here goes the custom initialization code of aurelia, as shown in question
    });
  });
});

我认为这个自定义初始化代码干扰了默认自举的其他测试用例。我只是从 i18n规格中取出 beforeEach 并将其直接放在查看规格之下测试套件。这意味着 View specs 测试套件下的所有测试用例都使用相同的aurelia bootstrapping设置。实际上这确实解决了我的问题。

I think this custom initialization code was interfering with the other test cases with default bootstrapping. I simply pulled out the beforeEach from the i18n specs and put it directly under the View specs test suite. Which means that all of the test cases under View specs test suite are using same aurelia bootstrapping setup. And actually that did solve my problem.

这篇关于与i18next相关的间歇性单元测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆