Aurelia 测试:无法在 'Node' 上执行 'replaceChild':参数 1 不是 'Node' 类型

Aurelia-testing: Failed to execute 'replaceChild' on 'Node': parameter 1 is not of type 'Node'

我第一次使用 aurelia-testing 包来测试我的 HTML 代码。

我似乎已经按照 the docs 设置我的测试如下:

describe('Contextual Menu HTML View test suite', function () {
  let component: ComponentTester;

  beforeEach(function () {
    component = StageComponent
      .withResources('../../src/components/modal/contextual-menu')
      .inView('<contextual-menu is-active.two-way="activateMenu"></contextual-menu>')
      .boundTo({ activateMenu: false });
  });

  it('should not add the is-active class to the root element', async function () {
    await component.create(bootstrap);
    const rootElement = await waitForDocumentElement('.qa-contextual-menu');
    expect(rootElement.classList.contains('is-active')).toBe(false);
  });

  afterEach(function () {
    component.dispose();
  });
});

我尝试只使用 bind 而不是 two-way,但也失败了。 我尝试了 document.querySelectorwaitForDocumentElement,这两种情况都失败了,但无论如何我认为错误来自较早的地方。

我收到一个错误,我不确定为什么。您能否追踪一下以确定以下问题的根本原因:

TypeError: Failed to execute 'replaceChild' on 'Node': parameter 1 is not of type 'Node'.
  at Object.convert (/Users/lemoustachiste/work/lm-frontend/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/generated/Node.js:573:11)
at HTMLDivElement.replaceChild (/Users/lemoustachiste/work/lm-frontend/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/generated/Node.js:292:31)
at NodeJsDom.replaceNode (/Users/lemoustachiste/work/lm-frontend/node_modules/aurelia-pal-nodejs/dist/nodejs-dom.js:95:29)
at makeElementIntoAnchor (/Users/lemoustachiste/work/lm-frontend/node_modules/aurelia-templating/dist/commonjs/aurelia-templating.js:2432:19)
at applyInstructions (/Users/lemoustachiste/work/lm-frontend/node_modules/aurelia-templating/dist/commonjs/aurelia-templating.js:2479:17)
at ViewFactory.create (/Users/lemoustachiste/work/lm-frontend/node_modules/aurelia-templating/dist/commonjs/aurelia-templating.js:2707:7)
at TemplatingEngine.enhance (/Users/lemoustachiste/work/lm-frontend/node_modules/aurelia-templating/dist/commonjs/aurelia-templating.js:5290:24)
at /Users/lemoustachiste/work/lm-frontend/node_modules/aurelia-framework/dist/commonjs/aurelia-framework.js:176:28
at new Promise (<anonymous>)
at Aurelia.enhance (/Users/lemoustachiste/work/lm-frontend/node_modules/aurelia-framework/dist/commonjs/aurelia-framework.js:174:12)

非常感谢

我不是开玩笑的用户。所以我简单地使用了 aurelia-cli (au new jest-skeleton --unattended --select typescript,jest,vscode) 的脚手架 TS jest 骨架应用程序。并发现这是有效的。

您的 jest.config.js 中似乎缺少以下配置。

testEnvironment: "node",

仅在添加之后,测试就开始工作了。