我想使用svelte-testing-library和svelte-htm来测试传递给自定义Svelte组件的属性的双向绑定。在测试期间尝试渲染组件时,会抛出此异常:
TypeError: Component is not a constructor
9 | it("should generate first page", async () => {
10 | let pageRows = writable([]);
> 11 | const { getByRole } = render(
| ^
12 | html`<${Pagination} rows=${[1, 2, 3]} bind:pageRows=${pageRows} />`
13 | );
14 | const button = getByRole("div");
at create_fragment (node_modules/svelte-hyperscript/src/h.js:132:21)
at Object.init (node_modules/svelte/internal/index.js:1454:37)
at new Component (node_modules/svelte-hyperscript/src/h.js:78:7)
at render (node_modules/@testing-library/svelte/dist/pure.js:71:21)
at Object.<anonymous> (Pagination.test.js:11:25)
我制作了一个codeandbox以重现该问题。我根本无法在codeandbox中运行测试,但是您可以将代码下载为zip,然后执行npm install && npm run test
。
https://codesandbox.io/s/nostalgic-snyder-1k8g0?file=/Pagination.test.js