????initial
???????????????????????
????Testing in ES6 with Mocha and Babel 6
????Using Babel
????setup
????$ npm i mocha --save-dev
????$ npm i chai --save-dev
????Use with es6
????babel 6+
$ npm install --save-dev babel-register
$ npm install babel-preset-es2015 --save-dev
// package.json
{
"scripts": {
"test": "./node_modules/mocha/bin/mocha --compilers js:babel-register"
}??
"babel": {
"presets": [
"es2015"
]
}
}
????babel 5+
$ npm install --save-dev babel-core
// package.json
{
"scripts": {
"test": "./node_modules/mocha/bin/mocha --compilers js:babel-core/register"
}
}
????Use with coffeescript
$ npm install --save coffee-script
{
"scripts": {
"test": "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register"
}
}
????Use with es6+coffeescript
????After done both...
{
"scripts": {
"test": "./node_modules/mocha/bin/mocha --compilers js:babel-core/register??coffee:coffee-script/register"
}
}
# $ mocha
$ npm t
$ npm test
????chai
import chai from 'chai';
const assert = chai.assert;
const expect = chai.expect;
const should = chai.should();
foo.should.be.a('string');
foo.should.equal('bar');
list.should.have.length(3);
obj.should.have.property('name');
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(list).to.have.length(3);
expect(obj).to.have.property('flavors');
assert.typeOf(foo?? 'string');
assert.equal(foo?? 'bar');
assert.lengthOf(list?? 3);
assert.property(obj?? 'flavors');
????Test
?????????????????·???????????????????????????????и??????????????????????????????????????
import chai from 'chai';
const assert = chai.assert;
const expect = chai.expect;
const should = chai.should();
describe('describe a test'?? () => {
it('should return true'?? () => {
let example = true;
// expect
expect(example).not.to.equal(false);
expect(example).to.equal(true);
// should
example.should.equal(true);
example.should.be.a(boolen);
[1?? 2].should.have.length(2);
});
it('should check an object'?? () => {
// ??????????Object????..
let nestedObj = {
a: {
b: 1
}
};
let nestedObjCopy = Object.assign({}?? nestedObj);
nestedObj.a.b = 2;
// do a function to change nestedObjCopy.a.b
expect(nestedObjCopy).to.deep.equal(nestedObj);
expect(nestedObjCopy).to.have.property('a');
});
});
????AsynTest
????Testing Asynchronous Code with MochaJS and ES7 async/await
????mocha???????????????????????????????????????????? done() ????
????????????????????????????????????? try/catch ???в???????? done() ??????? done(error)
// ???????????
it("should work"?? () =>{
console.log("Synchronous test");
});
// ??????????
it("should work"?? (done) =>{
setTimeout(() => {
try {
expect(1).not.to.equal(0);
done(); // ???
} catch (err) {
done(err); // ???
}
}?? 200);
});
?????????????????????? done ??????? Promise ??????????? Promise ???????????д????? try/catch ???
????it("Using a Promise that resolves successfully with wrong expectation!"?? function() {
????var testPromise = new Promise(function(resolve?? reject) {
????setTimeout(function() {
????resolve("Hello World!");
????}?? 200);
????});
????return testPromise.then(function(result){
????expect(result).to.equal("Hello!");
????});
????});
????mock
????mock?????????????????????????????????е??Щ??????
????React???????
????Test React Component
????React???????????????????????в??????????enzyme??????
????$ npm i --save-dev enzyme
????#
????$ npm i --save-dev react-addons-test-utils
??????????????????????
// ...??????import????
class TestComponent extends React.Component {
constructor(props) {
super(props);
let {num} = props;
this.state = {
clickNum: num
}
this.handleClick = this.handleClick.bind(this)
}
handleClick() {
let {clickNum} = this.state;
this.setState({
clickNum: clickNum + 1
});
}
render() {
let {clickNum} = this.state;
return (
<div className="test_component">
{clickNum}
<span onClick={this.handleClick}>?????1</span>
</div>
)
}
}