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

javascript - testing the catch block using jest - Stack Overflow

programmeradmin3浏览0评论

how can i test the catch block in the below snippet of code where i am using class

// Sample.js

class Sample{
 constructor(data){
  this.resolvedData = this.retrieveData(data) 
 }

 retrieveData(data){
   try{
     const resolvedData = data.map(o => o.name);
  }catch(error){
    throw error
  }
 }

}

// Sample.test.js

const Sample = require('./Sample');


describe('Sample File test cases', () => {
    test('should return the resolvedData', () => {
        const resolvedSample = [{name: "John", id: 123}, {name: "Doe", id: 3432}]
        const model = new Sample(resolvedSample);
        const expectedResolvedSample = [{name: "John"}, {name: "Doe"}]
        expect(model).toEqual(expectedResolvedSample)
    })
    test('should throw an error', () => {
        const resolvedSample = {}
        const model = new Sample(resolvedSample) // failing here map method since i am passing an object
        expect(model).toThrow(TypeError);
    })
})

I should fail then only it will e to the catch block and give me full coverage. What am i doing wrong here.

Any help appreciated.

how can i test the catch block in the below snippet of code where i am using class

// Sample.js

class Sample{
 constructor(data){
  this.resolvedData = this.retrieveData(data) 
 }

 retrieveData(data){
   try{
     const resolvedData = data.map(o => o.name);
  }catch(error){
    throw error
  }
 }

}

// Sample.test.js

const Sample = require('./Sample');


describe('Sample File test cases', () => {
    test('should return the resolvedData', () => {
        const resolvedSample = [{name: "John", id: 123}, {name: "Doe", id: 3432}]
        const model = new Sample(resolvedSample);
        const expectedResolvedSample = [{name: "John"}, {name: "Doe"}]
        expect(model).toEqual(expectedResolvedSample)
    })
    test('should throw an error', () => {
        const resolvedSample = {}
        const model = new Sample(resolvedSample) // failing here map method since i am passing an object
        expect(model).toThrow(TypeError);
    })
})

I should fail then only it will e to the catch block and give me full coverage. What am i doing wrong here.

Any help appreciated.

Share Improve this question edited Mar 20, 2019 at 9:29 skyboyer 23.7k7 gold badges62 silver badges71 bronze badges asked Mar 20, 2019 at 5:48 BeginnerBeginner 9,12511 gold badges49 silver badges91 bronze badges 1
  • please check my answer for a detailed explanation. Also please mark an answer as answered if some one properly answers the question. – 0DDC0 Commented Mar 20, 2019 at 6:29
Add a ment  | 

2 Answers 2

Reset to default 6

Try wrapping the exception-throwing code in a function:

expect(() => {
   const model = new Sample(resolvedSample)
}).toThrow(TypeError);

As per the jest docs,

Note: You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail.

Reference: https://jestjs.io/docs/en/expect#tothrowerror

describe('Dummy test', () => {
  class Sample {
      constructor(data){
        this.resolvedData = this.retrieveData(data) 
      }

      retrieveData(data){
        try{
          const resolvedData = data.map(o => o.name);
        }catch(error){
          throw error
        }
      }   
  }

  test('should fail', () => {
      expect(() => {
          new Sample({});
      }).toThrowError(TypeError);
  });
});
发布评论

评论列表(0)

  1. 暂无评论