Skip to content

Instantly share code, notes, and snippets.

@yanneves
Last active February 13, 2017 23:07
Show Gist options
  • Save yanneves/93a41bd38923e4bc247d to your computer and use it in GitHub Desktop.
Save yanneves/93a41bd38923e4bc247d to your computer and use it in GitHub Desktop.
Mocha Programmatic API w/ JSPM
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Example Mocha Test</title>
</head>
<body>
<!-- Required for browser reporter -->
<div id="mocha"></div>
<!-- Configure and run tests -->
<script src="mocha.bundle.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
// build using JSPM to mocha.bundle.js
// dependencies
import mocha from 'mocha'
import chai, {expect} from 'chai'
// setup mocha
mocha.setup('bdd')
// test specifications
import wombat from './wombat.spec'
wombat()
// run mocha
mocha.run()
// as found in grunt-mocha example
// https://github.com/kmiyashiro/grunt-mocha/blob/master/example/js/wombat.js
export default function(opts) {
console.log('Log option works')
opts = opts || {}
this.name = opts.name || 'Wally'
this.eat = function(food) {
if (!food) throw Error('D:')
return 'nom nom'
}
return this
}
// as found in grunt-mocha example
// https://github.com/kmiyashiro/grunt-mocha/blob/master/example/test/spec/wombat.js
import Wombat from './wombat'
export default function () {
describe('Wombat', function() {
beforeEach(function() {
this.wombat = new Wombat()
})
afterEach(function() {
delete this.wombat
})
it('should create a wombat with defaults', function() {
expect(this.wombat).property('name', 'Wally')
})
it('should name itself if name passed in options', function() {
this.wombat = new Wombat({ name: 'Matt' })
expect(this.wombat).property('name', 'Matt')
})
describe('#eat', function() {
it('should throw if no food passed', function() {
expect(this.wombat.eat).to.throw('D:')
})
it('should return noms if food passed', function() {
expect(this.wombat.eat('apple')).to.eql('nom nom')
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment