1

我尝试测试这个类

class Scraper {
  async run() {
    return await nightmare
      .goto(this.url)
      .wait('...')
      .evaluate(()=>{...})
      .end
  }
}

我的测试看起来像这样:

test('Scraper test', t => {
  new Scraper().run().then(() => {
    t.is('test', 'test')
  })
})

测试失败:

测试完成,没有运行任何断言

编辑

github上的存储库:https ://github.com/epyx25/test

测试文件:https ://github.com/epyx25/test/blob/master/src/test/scraper/testScraper.test.js#L12

4

2 回答 2

7

你需要回报承诺。不需要断言计划:

test('Scraper test', t => {
  return new Scraper().run().then(() => {
    t.is('test', 'test')
  })
})

或者更好的是,使用异步测试:

test('Scraper test', async t => {
  await new Scraper().run()
  t.is('test', 'test')
})
于 2017-06-25T08:55:12.753 回答
-1

您必须使用assert-planning来阻止测试,直到 lambda 被通知Promise,例如:

test('Scraper test', t => {
   t.plan(1);
   return new Scraper().run().then(() => {
     t.is('test', 'test')
   })
})

或者

test.cb('Scraper test', t => {
   t.plan(1);
   new Scraper().run().then(() => {
     t.is('test', 'test')
     t.end()
   })
})
于 2017-06-24T17:30:20.633 回答