繁体   English   中英

用摩卡和蔡测试

[英]Testing with mocha and chai

这是我第一次尝试使用mocha和chai,我创建了这个小的测试文件test.spec.js ,并在其中进行了一些测试。

var expect = require('chai').expect;

var foo = 'bar';

describe('Test', function () {
it('Should exist', function () {
    expect(foo).to.exist;
});
it('Should be a string', function () {
    expect(foo).to.be.a('string');
});
it('Should equal bar', function () {
    expect(foo).to.equal('bar');
});
it('Should be at least 3 chars', function () {
    expect(foo).to.have.length.above(2);
});
});

这是与“主”代码所在的单独文件。
但是,如何测试我在脚本文件中编写的实际相关代码?

我没有使用ES6,因此无法导入或要求。 但是我正在使用npm,这就是我需要chai的方式。

所以我的专案看起来像这样

A folder
 - myscript.js
 - test.spec.js

如何测试myscript?

像这样分配测试脚本:

var myscript = require('./myscript.js');

这是一个例子:

'use strict'

const expect = require('chai').expect
const myscript = require('./myscript.js')

describe('myscript', function () {
  it('should be a function', function() {
    expect(typeof myscript).to.equal('function')
  })

  it("should return an object with toString method", function() {
    expect(myscript().hasOwnProperty("toString")).to.be.true
  })
}) // etc...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM