无法测试具有 Typescript 类型的 Vue 组件?
Typescript 使 Vue 单文件组件 (SFC) 能够拥有定义良好的接口,例如知道允许哪些 prop 名称和类型。 令人兴奋!
但是,您可能想测试组件,这就是框架似乎有问题的地方。您必须将类型化的 Vue 组件从 .vue
文件导入到 Typescript 文件中才能对其进行测试。
是否有任何机制可以作为预设集成到 jest 中,或者加载器或类似的机制,以方便从 test.ts
文件中的 .vue
文件导入。
您可以推出自己的工具来维护冗余的 .d.ts
文件以及 .vue
源。一个示例是使用 vue-tsc
工具和专用 tsconfig.test.json 在每次测试运行之前(重新)创建 .d.ts 文件(Jeff Bowman 在下面链接)。然而,即使在小型代码库中,每次重新构建的速度也非常慢。此外,没有明显的机制让它成为笑话管道的一部分(例如,可以尊重对编辑源文件的更改作为现代 TDD 工作流程中笑话 --watch 的一部分)
相比之下,对于纯打字稿,像 这样的预设ts-jest
为用打字稿编写的库和测试提供集成工具。不需要独立的构建步骤,源树周围不会散布额外的非源文件,并且对源文件的更改会立即反映在 jest --watch
进程中。
是否有一个面向笑话的工具链允许将 .vue
文件作为类型化组件导入到用打字稿编写的 Jest 测试中?
以这个例子(开玩笑)测试为例。
// MyComponent.test.ts
import { mount } from '@vue/test-utils';
import MyComponent from './MyComponent.vue';
test("MyComponent click behaviour", () => {
const wrapper = mount(MyComponent, {
propsData: {
message: 'Here is something to show to the user.',
},
});
const label = wrapper.find('[class="my-component"]');
await label.trigger('click');
expect(wrapper.element).toMatchSnapshot();
})
理想情况下,这个 jest .test.ts
文件应该能够导入 .vue
SFC 并从其中声明的组件的类型中受益。
后备方法是与 jest 集成的任何工具,确保在运行时维护类型,例如 jest --watch
Typescript enables Vue Single File Components (SFCs) to have a well-defined interface, such as knowing what prop names and types are allowed. Exciting!
However, you might want to test a component, and that's where the framework seems to have trouble. You have to import your typed Vue component from the .vue
file into a Typescript file in order to test it.
Is there any mechanism that can be integrated into jest as a preset, or a loader or similar to facilitate imports from .vue
files in test.ts
files.
You can roll your own tooling to maintain redundant .d.ts
files alongside the .vue
source. An example is using the vue-tsc
tool and a dedicated tsconfig.test.json to (re)create .d.ts files before every test run (linked below by Jeff Bowman). However, this is incredibly slow to re-build each time even in a small codebase. Also there is no obvious mechanism for this to be part of a jest pipeline (e.g. that could respect changes to edited source files as part of jest --watch within a modern TDD workflow)
By contrast for pure typescript, a preset like ts-jest
provides integrated tooling for libraries AND tests to be written in typescript. No standalone build step is needed, no extra non-source files are strewn around the source tree, and changes to source files are immediately reflected within a jest --watch
process.
Is there a jest-oriented toolchain that would permit a .vue
file to be imported as a typed component into a Jest test written in typescript?
Take this example (jest) test.
// MyComponent.test.ts
import { mount } from '@vue/test-utils';
import MyComponent from './MyComponent.vue';
test("MyComponent click behaviour", () => {
const wrapper = mount(MyComponent, {
propsData: {
message: 'Here is something to show to the user.',
},
});
const label = wrapper.find('[class="my-component"]');
await label.trigger('click');
expect(wrapper.element).toMatchSnapshot();
})
Ideally this jest .test.ts
file should be able to import a .vue
SFC and benefit from the typings of the component declared in it.
A fallback approach would be any tooling integrated with jest which ensures that types are maintained when running e.g. jest --watch
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论