【发布时间】:2018-11-09 05:55:57
【问题描述】:
我正在尝试对我的 aurelia 自定义元素进行单元测试,如下所示。
// 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 的值,翻译后的文本会显示在元素中。为了测试这一点,我编写了以下测试用例。
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的初始化有关,并且在初始化完成之前测试用例正在运行。虽然我不太确定这个假设。
我应该改变什么,以使这个测试用例成为确定性的?
其他信息:
- 如果此测试用例在所有其他 view related test cases 之前运行,则此测试用例成功。
- 我创建了一个GitHub repo,以便感兴趣的读者/用户可以重现该问题。请记住,您可能已经多次运行测试来重现问题。
【问题讨论】:
标签: javascript unit-testing jasmine i18next aurelia