简体   繁体   English

使用笑话来测试数据是否与TypeScript类型相对应时避免警告

[英]Avoid warnings when using jest to test that data corresponds to TypeScript types

In the example below I want to verify that the data actually gives med a project with a property id . 在下面的示例中,我想验证数据是否确实为med提供了一个具有属性idproject

test('getActiveProject => Project', async () => {
    let previouslyOpenProject = await client.getActiveProject()
    if (!previouslyOpenProject) {
        previouslyOpenProject = await client.createProject();
    }
    let project = await client.getActiveProject();
    expect(project.id).toBeGreaterThanOrEqual(1);
});

I get the following ts error message 我收到以下ts错误消息

[ts] Object is possibly 'null' (referring to project and the use of project.id) [ts]对象可能为'null'(指project和project.id的使用)

What is a good course of action from here: 从这里开始有什么好的做法:

  • Should I write my tests in js instead of ts? 我应该用js而不是ts编写测试吗?
  • Should I find a testing framework with a TypeScript pre-processor that will allow me to use TypeScript types in test expectations? 我是否应该找到带有TypeScript预处理程序的测试框架,该框架将允许我在测试期望中使用TypeScript类型? (I don't think this exists) (我认为这不存在)
  • Should these kinds of checks be made by some kind of json validation scheme instead of jest tests? 这些检查是否应该通过某种json验证方案而不是开玩笑的测试来进行?

Result type fo getActiveProject is Project | null getActiveProject结果类型是Project | null Project | null . Project | null If you are sure that let project is not null you can map it by let project = await client.getActiveProject() as Project; 如果确定let project不为null,则可以通过let project = await client.getActiveProject() as Project;来映射它let project = await client.getActiveProject() as Project; .

Also you can do something like this 你也可以做这样的事情

let project = await client.getActiveProject(); // without type mapping
if (project !== null) {
  // here project will be Project type, not a Project | null
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Jest 中使用 Typescript 时如何测试超级调用? - How do I test super calls when using Typescript with Jest? 将 Array.prototype.includes 与 Typescript 一起使用时开玩笑测试失败? - Jest test failing when using Array.prototype.includes with Typescript? 如何在 mocking 功能时允许部分 TypeScript 类型 - 开玩笑,TypeScript - How to allow partial TypeScript types when mocking functions - Jest, TypeScript 如何使用 jest 和 typescript 测试基本反应功能组件的类型 - How to test with jest and typescript with types a basic react function component 使用Jest测试打字稿异步功能 - Test typescript async function using Jest Jest 在尝试使用打字稿使用故事书中的故事时抛出“属性 'id' 的类型不兼容”错误 - Jest throws "Types of property 'id' are incompatible" error when it is trying to use stories from storybook using typescript 使用angular2或打字稿输入时如何避免重复数据 - How to avoid repeat of data when entered using angular2 or typescript TypeError:使用笑话,酶和react-16,打字稿时,plugin.test不是函数 - TypeError: plugin.test is not a function when using jest, enzyme and react-16, typescript 使用正确类型使用 Jest 和 Typescript 模拟 Express 请求 - Mocking Express Request with Jest and Typescript using correct types 使用玩笑模拟时的打字稿错误 - Typescript errors when using jest mocks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM