typescript - TypeError:无法要求`.d.ts`文件
问题描述
堆
- Vue 3.2.19
- @vue/test-utils 2.0.0-rc.15
- 打字稿4.1.6
问题
运行命令时出现问题vue-cli-service test:unit --no-cache
。
请检查类型错误
(链接到 CI https://github.com/baronkoko/vue-unit-tests-type-error/runs/3728921894?check_suite_focus=true)
在 Vue 组件中使用时发生,从节点模块导入 typescript 文件,其中又存在另一个 typescript 文件导入
例子.spec.ts
import { shallowMount } from "@vue/test-utils";
import HelloWorld from "@/components/HelloWorld.vue";
describe("HelloWorld.vue", () => {
it("should render full name", () => {
const firstName = "Tailor";
const lastName = "Cox";
const wrapper = shallowMount(HelloWorld, {
props: { firstName, lastName },
});
expect(wrapper.text()).toMatch(
`${firstName.toUpperCase()} ${lastName.toUpperCase()}`
);
});
});
你好世界.vue
<template>
<div class="hello">
<h1>{{ fullName }}</h1>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import { FakeUtil } from "fake-utils-lib/src/fake-util";
export default defineComponent({
name: "HelloWorld",
props: {
firstName: {
type: String,
default: "",
},
lastName: {
type: String,
default: "",
},
},
computed: {
fullName() {
const util = new FakeUtil();
return util.getFullNameCapitalized(this.firstName, this.lastName);
},
},
});
</script>
fake-util.ts
import { Helpers } from "./helpers";
export class FakeUtil {
getFullNameCapitalized(firstName: string, lastName: string): string {
return `${Helpers.capitalize(firstName)} ${Helpers.capitalize(lastName)}`;
}
}
助手.ts
export class Helpers {
static capitalize(text: string): string {
return text.toUpperCase();
}
}
完整示例https://github.com/baronkoko/vue-unit-tests-type-error
可能的解决方案:
将扩展名添加到导入的文件中,但它看起来像这样
// @ts-忽略
从“./helpers.ts”导入{ Helpers };
为 jest.config.js ts-jest 启用 isolatedModules
全局变量:{“ts-jest”:{ isolatedModules: true, }, },
但是随后我们会得到很多 SyntaxError,例如,如果存在可选链接SyntaxError
请问有人可以帮忙吗?
解决方案
通过使用 Rollupesm
格式构建所有外部模块并使用它们来解决此问题
推荐阅读
- c# - 无法处理绑定模板
- batch-file - 在消息中报告目录中第一个文件的文件名的批处理文件
- c - NodeJS 退出代码向左移动 8 位
- windows - 从同一 winforms 程序的不同编译版本生成多个设置(例如 InstallShield LE)
- elasticsearch - elasticsearch脚本中的地图问题地图参数
- vuetify.js - Vuetify 网格系统不允许嵌套网格中的不同顺序
- mysql - Vapor 3 Fluent MySQL: save on model adds 10 to auto_increment
- asp.net-mvc - 在实体框架中执行Sql查询
- java - Apache SolrCloud 中的多值字段值计数
- list - 如何检查一个项目是否在列表中?