javascript - Jest: Testing a nested promise - Stack Overflow

I'm new to jest, and having trouble determining how to test results nested inside promises. Specif

I'm new to jest, and having trouble determining how to test results nested inside promises. Specifically:

myMethod: function (e) {
  let self = this
  self.resetErrors()

  Parser.parseFile(this.form.uploadFile).then(res => {
    const hasErrors = self.validFile(res)
    if (!hasErrors) {
      self.processFile(res)
    }
  })
}

I'd like to test to ensure that, assuming hasErrors is false, self.processFile fires. Here's my current (failing) best effort:

describe("if the provided data is valid", () => {
  it('runs processFile', () => {
    const mockProcessFile = jest.fn()
    mockParser = jest.fn(() => {
      new Promise((resolve, reject) => {
        return ValidMockData
      }).then((loanData) => {
        expect(mockProcessFile).toBeCalled()
      })
    })

    CsvParser.parseFile = mockParser

    wrapper.vm.validFile = jest.fn(true)
    wrapper.vm.processFile = mockProcessFile
    wrapper.vm.store().resolve((data) => {
      expect(mockProcessFile).toBeCalled()
    })
  })
})

At present I'm getting a Cannot read property 'then' of undefined error - which makes sense, but I'm not sure how exactly I'm supposed to crack into expectations inside of a then() call. Any thoughts appreciated

I'm new to jest, and having trouble determining how to test results nested inside promises. Specifically:

myMethod: function (e) {
  let self = this
  self.resetErrors()

  Parser.parseFile(this.form.uploadFile).then(res => {
    const hasErrors = self.validFile(res)
    if (!hasErrors) {
      self.processFile(res)
    }
  })
}

I'd like to test to ensure that, assuming hasErrors is false, self.processFile fires. Here's my current (failing) best effort:

describe("if the provided data is valid", () => {
  it('runs processFile', () => {
    const mockProcessFile = jest.fn()
    mockParser = jest.fn(() => {
      new Promise((resolve, reject) => {
        return ValidMockData
      }).then((loanData) => {
        expect(mockProcessFile).toBeCalled()
      })
    })

    CsvParser.parseFile = mockParser

    wrapper.vm.validFile = jest.fn(true)
    wrapper.vm.processFile = mockProcessFile
    wrapper.vm.store().resolve((data) => {
      expect(mockProcessFile).toBeCalled()
    })
  })
})

At present I'm getting a Cannot read property 'then' of undefined error - which makes sense, but I'm not sure how exactly I'm supposed to crack into expectations inside of a then() call. Any thoughts appreciated

Share Improve this question edited May 9, 2018 at 9:13 Andreas Köberle 111k58 gold badges280 silver badges307 bronze badges asked May 9, 2018 at 8:36 PlankTonPlankTon 12.6k16 gold badges87 silver badges157 bronze badges 1
  • first thing I noticed new Promise((resolve, reject) => { return ValidMockData }) ... promises need to be resolved or rejected ... returning some ValidMockData doesn't do it – Jaromanda X Commented May 9, 2018 at 8:38
Add a ment  | 

1 Answer 1

Reset to default 7

You need to store the Promise you create in the test so you can use await to let the test finishing after it was resolved:

describe("if the provided data is valid", async() => {
  it('runs processFile', () => {
    const mockProcessFile = jest.fn()
    const p = Promise.resolve(ValidMockData)
    CsvParser.parseFile = jest.fn(() => p)


    wrapper.vm.validFile = jest.fn(true)
    wrapper.vm.processFile = mockProcessFile
    wrapper.vm.store()

    await p

    expect(mockProcessFile).toBeCalled()
    expect(mockProcessFile).toBeCalled()


  })
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742287390a4415567.html

相关推荐

  • javascript - Jest: Testing a nested promise - Stack Overflow

    I'm new to jest, and having trouble determining how to test results nested inside promises. Specif

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信