Notice: 函数 WP_Scripts::localize 的调用方法不正确$l10n 参数必须是一个数组。若要将任意数据传递给脚本,请改用 wp_add_inline_script() 函数。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 5.7.0 版本添加的。) in /data/www/appblog/wp-includes/functions.php on line 6131

Node.js 测试

安装与准备

$ npm init -y
$ npm i mocha -g
$ mkdir test
$ mocha

在项目目录下安装:

$ npm install mocha --save-dev
"scripts": {
  "test": "./node_modules/mocha/bin/mocha"
}

mocha:测试框架

创建:test.js

describe('TestDemo', function() {
  describe('方法 1', function() {
    context('情境 1', function() {
      it('测试 1', function() {

      })
      it('测试 2', function() {

      })
    })
  })
})
$ mocha
$ mocha run test

mocha:安排测试之前与之后要做的事

describe('TestDemo', function() {
  describe('方法 1', function() {

    before(function() {
      console.log('--- 测试之前 ---')
    })
    before(function() {
      console.log('--- 测试之后 ---')
    })

    beforeEach(function() {
      console.log('--- 每条测试之前 ---')
    })
    beforeEach(function() {
      console.log('--- 每条测试之后 ---')
    })

    context('情境 1', function() {
      it('测试 1', function() {

      })
      it('测试 2', function() {

      })
    })
  })
})

chai:断言库

安装

npm i chai --save-dev

chai:assert风格的断言

const chai = require('chai')
const assert = chai.assert

describe('TestDemo', function() {
  it('使用 assert 风格的断言测试', function() {
    //var value = 'hello'
    var value = 123
    assert.typeOf(value, 'string')
    assert.equal(value, 'hello')
    assert.lengthOf(value, 5)
  })
})
上一篇 Node.js 入门
下一篇 Nodejs crypto密码模块