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?????????????顣mocha?????????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