mocha??chai??sinon??istanbul????????????????????
?????liuwill ???????[ 2017/7/12 10:07:29 ] ?????????????? ??????? ???
???????????????校??????????????????????????????????妫�?????????????????????????????????????????????????????????????????????????
??????????????????????????????????椋�?????????????些未??????????????????????尾?????些???????????????????????????????????懈??????????
???????????????y???????????????????????????????????位??????????????????????????????????????????????斜???校?????????????????唯????????????????????械???????????????????????????????????????????????????????????????????????????????????????????????????????????????????椋�???????????????????????????????????????
???????????????
????Istanbul??JavaScript??????????????????????????????????????????Istanbul????????????????????????????????位???????????????????????????????????????????械??????????????????????Istanbul?????test runner??????mocha?????????????????????????????妾�
????Mocha?????????????????胁????????????Jasmine??Karma??Ava????JUnit??????????mocha????????????descibe??it????????????test suit?????????????椤ocha?????????assert?????????????????斜??????????????????chai???????????????nodejs????assert??椤�
???????????????校???????些????????????????io????????????????????????????????????????sinon??????????????Sinon???????Test Double?????????????????????????????些???????????婊�???????????????????????????????????????????????????????????????????????????????????小?
?????nodejs??????貌??????
????1 ??????????????
????mocha??istanbul?????????????????????????????
npm install --save-dev mocha chai sinon istanbul
???????????????package.json?????scripts?锟�??????胁???????????????????
??{
???? ...
???? "scripts":{
???? "coverage": "istanbul cover _mocha -- -R spec --timeout 5000 --recursive"??
???? "coverage:check": "istanbul check-coverage"??
???? }
???? ...
????}
????????npm run coverage??npm run coverage:check???????????????妫�????????????妫�?????????????????????????
????2 ????Istanbul
????istanbul??????胁????????????????????????????????????????????yaml?????.istanbul.yml????????谩????????些???????????
????instrumentation:
???? root: . # ??械????
???? extensions:
???? - .js # ??榇�??????????????
???? excludes: ['**/benchmark/**']
???? ... ...
????reporting:
???? print: summary
???? reports: [lcov?? text?? html?? text-summary] # ??????????
???? dir: ./coverage # ??????姹�?????
???? watermarks: # ??????????????????貌?????
???? statements: [80?? 95]
???? ... ...
????check:
???? global:
???? statements: 100
???? branches: 100
???? lines: 100
???? functions: 100
???????check???????????????????????????????????????????????????????????涓�????????????????懈????????????????????????瓒�?????????械???????????????????????????????????伞?
??????写???????
???????????????械???????????????????????写???????????????????????????????????写???????????????????????????????????????????????????????
????1 ??渭???mocha???????
????????chai????expect??????????????BDD??????写?????????????????????????????
??var chai = require('chai')
????chai.should()
????var expect = chai.expect
????var assert = chai.assert
????describe('basic test'?? function () {
???? describe('simple'?? function () {
???? it('data check'?? function () {
???? var data = { name: "test" }
???? assert.isNotNull(data?? 'data should not be null')
???? expect(data).to.be.an('object')
???? expect(data).to.have.all.keys(['name'])
???? expect(data).to.deep.include({name: 'test'});
???? });
???? });
????});
????2 ??Sinon????????写
??... ??? ...
????var sinon = require('sinon')
????var fs = require('fs')
????describe('sinon'?? function () {
???? it("should mock readFile"?? function(done){
???? sinon.stub(fs?? 'readFile').callsFake(function (path?? callback) { callback(new Error('read error')) })
???? fs.readFile("any file path"?? function(err??data){
???? assert.isNotNull(err)
???? done()
???? })
???? assert(fs.readFile.calledOnce)
???? });
????});
??????mocha???????校???????????????????????????????????it?????????done??????????mocha??????????????????????????????????????????????????
????Sinon????????????spy??stub??mock?????校?
????Spy?? ???????????????????????????浜�???????
????Stub?? ??????????????????????????????????????????stubbed??????????魏???????????????
????Mock?? ??????spies??stubs????婊�?????????????????
????????????????????????????????????sinon????梅????????????????????????????
????????????
??????????写??????????????????????github???????贸??????????travis????????????????????????coverall?????????????????????????????????????badges??
??????????梅?????????慰????????????coveralls?????????邪????????npm i -D coveralls?????????package.json??????istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
?????????nodejs???.travis.yml???????锟�?
??language: node_js
????node_js:
???? - "7.6.0"
????install:
???? - npm install
????script:
???? - npm test
????after_script:
???? - npm run coverall
?????????婕�??????????????慰??????????????????????????????????婕�?????????
????https://github.com/liuwill/find-ip-location
??????

???路???
??????????????????
2023/3/23 14:23:39???写?貌??????????
2023/3/22 16:17:39????????????????????些??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???路???????路
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11