最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest typescript check for type - Stack Overflow

programmeradmin3浏览0评论

lets say i have the following interface:

export interface CMSData {
    id: number;
    url: string;
    htmlTag: string;
    importJSComponent: string;
    componentData: ComponentAttribute[];
}

Then i have a method that returns an array of this object type:

public async GetContent(url: string): Promise<CMSData[]>{
    const response = await super.get<ICMSContentData[]>(url, {});
    try {
        if (response?.parsedBody) {
            return this.ProcessResponse(response.parsedBody);
        } else {
            this.handleHTTPError(new Error("Error"));
            return [];
        }

    } catch (e) {
        this.handleHTTPError(e);
        return [];
    }

}

Then i want to test that this is the case so i write the following test:

import {ContentIOService} from "..";
import {CMSData} from "../IOServices/ContentIOService";

require('es6-promise').polyfill();
require('isomorphic-fetch');

test('Get Content', async () => {
    const service = ContentIOService.getInstance();
    const data = await service.GetContent(";);
    console.log(data)
    expect(data).toBeInstanceOf(CMSData[]);
});

However here i get the following error:

'CMSData' only refers to a type, but is being used as a value here.

So how can i test that the data i get back is valid and of the right type?

lets say i have the following interface:

export interface CMSData {
    id: number;
    url: string;
    htmlTag: string;
    importJSComponent: string;
    componentData: ComponentAttribute[];
}

Then i have a method that returns an array of this object type:

public async GetContent(url: string): Promise<CMSData[]>{
    const response = await super.get<ICMSContentData[]>(url, {});
    try {
        if (response?.parsedBody) {
            return this.ProcessResponse(response.parsedBody);
        } else {
            this.handleHTTPError(new Error("Error"));
            return [];
        }

    } catch (e) {
        this.handleHTTPError(e);
        return [];
    }

}

Then i want to test that this is the case so i write the following test:

import {ContentIOService} from "..";
import {CMSData} from "../IOServices/ContentIOService";

require('es6-promise').polyfill();
require('isomorphic-fetch');

test('Get Content', async () => {
    const service = ContentIOService.getInstance();
    const data = await service.GetContent("https://1c7207fb14fd3b428c70cc406f0c27d9.m.pipedream.net");
    console.log(data)
    expect(data).toBeInstanceOf(CMSData[]);
});

However here i get the following error:

'CMSData' only refers to a type, but is being used as a value here.

So how can i test that the data i get back is valid and of the right type?

Share Improve this question edited Sep 24, 2020 at 17:10 skyboyer 23.7k7 gold badges61 silver badges71 bronze badges asked Sep 23, 2020 at 14:42 Marc RasmussenMarc Rasmussen 20.6k83 gold badges221 silver badges382 bronze badges 3
  • It’s saying right since you can’t use CMSData as a instance of class here. It is just a type which won’t be included in bundle code. – tmhao2005 Commented Sep 23, 2020 at 14:48
  • instanceof is a JavaScript binary operator that takes two operands, both of which are values. It sounds like Java's instanceof but it is not even analogous. Read it as value instanceof anotherValue. – Aluan Haddad Commented Sep 23, 2020 at 15:26
  • 1 you don't need to test that, that is what typescript is for. If the return type would be something diffrent, typescript would let you know while coding. – philipp Commented Sep 23, 2020 at 15:30
Add a comment  | 

1 Answer 1

Reset to default 17

If the type you're looking for is a call, the .toBeInstanceOf(Class) method accepts a parameter which MUST BE a JavaScript class constructor instead of TS type.

You should let TSC check whether you receive the correct type of data at compile time. Code written inside test suites and test cases is executed at runtime, .toBeInstanceOf(Class) is a runtime check, NOT compiler time.

At runtime, you may want to use expect.objectContaining(object) matches any received object that recursively matches the expected properties.

发布评论

评论列表(0)

  1. 暂无评论