美文网首页
1-5 Provide Testing Helper Funct

1-5 Provide Testing Helper Funct

作者: 香草拿铁去冰不加糖 | 来源:发表于2019-03-29 10:18 被阅读0次

Provide Testing Helper Functions as Globals in JavaScript

These testing utilities that we built are pretty useful. We want to be able to use them throughout our application in every single one of our test files.

Some testing frameworks provide their helpers as global variables. Let’s implement this functionality to make it easier to use our testing framework and assertion library. We can do this by exposing our test and expect functions on the global object available throughout the application.

提取码:acg6

观看视频

Code

Transcript

These testing utilities are pretty useful. We want to be able to use them throughout our application in every single one of our test files.

We could put these into a module that we would require an import into every single one of our test files, but many testing frameworks embrace the fact that you're going to be using these in every single one of your test files, and so they just make them available globally.

I am going to cut this out of our testing file. I am going to go to setup-global.js file, and I will paste it into here, and then I will say global.test = test, and global.expect = expect.

setup-globals.js

async function test(title, callback) {
  try {
    await callback()
    console.log(`✓ ${title}`)
  } catch (error) {
    console.error(`✕ ${title}`)
    console.error(error)
  }
}

function expect(actual) {
  return {
    toBe(expected) {
      if (actual !== expected) {
        throw new Error(`${actual} is not equal to ${expected}`)
      }
    }
  }
}

global.test = test
global.expect = expect

I can run

node --require ./setup-globals.js lessons/globals.js

and then our test file.

We get the same result as we did before. Now, we can use this setup-globals in every single one of our test files. With that setup, all of our test files can use the test and expect global variables.

star该项目

相关文章

网友评论

      本文标题:1-5 Provide Testing Helper Funct

      本文链接:https://www.haomeiwen.com/subject/xoikbqtx.html