Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

javascript - 如何在Jest中测试诺言的诺言?(How to test a promise of promises in Jest?)

Using Jest, Typescript, React, MobX and I have the following function that I am trying to test in Jest:

(使用Jest,Typescript,React,MobX,我尝试在Jest中测试以下功能:)

  search(searchText: string) : Promise<void> {
    this.searchInputCache = searchText;
    this.searchLoading = true;
    const promises = [] as Promise<any>[];
    const results = {} as {[itemType in DatabaseItemType]: DatabaseItemRef[]};
    for (const [item, checked] of this.filterChecks.entries()) {
      if (checked) {
        promises.push(SearchService.searchForItem(this._rootStore.databaseStore, searchText, item as DatabaseItemType, SearchTypeFlags.nxSearchType_Name)
           .then(values => results[item] = values));
      }
    }
    return Promise.all(promises).then(() => this.populateFilterTreeStore(results));

I've tried testing it with await, but I am trying to test that the inner function is properly called as such:

(我已经尝试过等待测试它,但是我正在尝试测试内部函数是否正确地这样调用:)


 it('searches', () => {
    filterTreeStore.filterChecks.set(DatabaseItemType.Signal, true);
    filterTreeStore.filterChecks.set(DatabaseItemType.Frame, true);
    filterTreeStore.filterChecks.set(DatabaseItemType.ECU, false);
    filterTreeStore.filterChecks.set(DatabaseItemType.PDU, false);
    filterTreeStore.filterChecks.set(DatabaseItemType.Cluster, false);

    const searchText = 'a'; 
    filterTreeStore.search('a').then(() => expect(searchMock).toBeCalledWith(databaseStore, searchText, DatabaseItemType.Signal,SearchTypeFlags.nxSearchType_Name))
  });

Ultimately I keep getting TypeError: Cannot read property 'then' of undefined no matter what I have tried.

(最终,我不断收到TypeError:无论我尝试了什么,都无法读取未定义的属性'then'。)

  79 |     for (const [item, checked] of this.filterChecks.entries()) {
  80 |       if (checked) {
> 81 |         return promises.push(SearchService.searchForItem(this._rootStore.databaseStore, searchText, item as DatabaseItemType, SearchTypeFlags.nxSearchType_Name)
     |                              ^
  82 |            .then(values => results[item] = values));
  83 |       }
  84 |     } `

Any suggestion on how to test this functionality is appreciated!

(任何有关如何测试此功能的建议,不胜感激!)

  ask by Roxana Karami translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...