rendro / mocha-given Goto Github PK
View Code? Open in Web Editor NEWAdds a Given/When/Then DSL to mocha as an alternative style for specs
License: MIT License
Adds a Given/When/Then DSL to mocha as an alternative style for specs
License: MIT License
I use jasmine-given at work (and have even worked with Justin Searls on writing some parts of it), and one of the fundamental goals is to only run setup (Given
, When
) once per Then
. I've noticed in my tests using mocha-given that
Then -> testSomething()
And -> testSomethingElse()
will run all Given
s and When
s twice. Which kind of defeats the purpose. Now, to only run setup once, instead of getting the nice formatting of cascading And
s, I have to put everything in one giant Then
block. If you check out https://github.com/searls/jasmine-given/blob/master/app/js/jasmine-given.coffee#L50 and https://github.com/searls/jasmine-given/blob/master/app/js/jasmine-given.coffee#L82, the subsequentThen
mechanism tacks And
functions on so that they're executed as part of the same it
statement.
In other words I want to do something like:
Given -> @foo = 'bar'
When -> @subject.test()
Then -> expect(@foo).to.equal 'not bar'
And -> expect(@subject.aSpiedOnMethod).to.have.been.called
And -> expect(@someOtherStuff).to.have.happened()
and have the Given
and When
run once instead of three times.
I'm willing to work on this (or work on this with you) if you want.
jasmine-given
checks the return values of Then
and And
functions as a part of the test. However the test only fails of the return value is strictly false
. That way you can write code such as:
When -> @result = 1 + 2
Then -> @result == 3
I noticed that this is not the behaviour of mocha-given
which seems to check if the return value is "truthy".
Since CoffeeScript implicitly returns the last statement in a function it might sometimes be something else than just true
or false
which makes the jasmine-given
approach a good one. Can this be changed?
Using Node 4.2.
This works:
Then(function () { expect(2).to.equal(2) })
This times out:
Then(() => expect(2).to.equal(2))
This works:
Then(done => {
expect(2).to.equal(2)
done()
})
Mocha now has promise support. Would it be possible to also add it to mocha-given?
In mocha i can write:
describe 'myPromiseFunction', ->
beforeEach -> someAsyncSetup()
it 'shoud return "foo" async', ->
myPromiseFunction().then (result) ->
result.should.equal 'foo'
It would be awesome if mocha-given supported this:
describe 'myPromiseFunction', ->
Given -> someAsyncSetup()
When -> myPromiseFunction().then (@result) =>
Then -> @result == 'foo'
A lot of times when I install mocha-given, I get this:
npm ERR! peerinvalid The package mocha does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer [email protected] wants mocha@~1.16.0
but the version of mocha
I'm using is 1.16.2 . . . soooooo, it should work (unless I really misunderstand semver). I can usually fix this locally by cd node_modules/mocha-given
followed by npm install
. However, that fix doesn't work on travis, even with something like:
install:
- "npm install"
- "cd node_modules/mocha-given && npm install"
because npm returns a code on a failed peer dependency, so travis just aborts immediately.
It's worth mentioning that I'm specifically using a branch on a fork of mocha (which is the reason I haven't updated mocha-given also). The version is right (1.16.2), but maybe peer dependencies are weird with forks and/or branches (so this might just be an npm bug, but I thought I'd start here). Here's the relevant package.json entry:
"devDependencies": {
"coffee-script": "~1.7.1",
"underscore": "~1.6.0",
"mocha": "git://github.com/tandrewnichols/mocha.git#suppress-stack",
"mocha-given": "~0.0.18",
"indeed": "~1.0.1"
},
And the actual installed version of that suppress-stack branch:
AndrewNichols:safe-obj$ cat node_modules/mocha/package.json | grep version
"version": "1.16.2",
Any insight into this?
Firstly, the browser distribution of mocha-given (/browser/mocha-given.js) is very much appreciated!
However, the compiled JavaScript currently pollutes the browser's global scope with all of the module's variables. I believe that compiling CoffeeScript without the --bare option should fix that.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.