A test framework that embraces promises for real
npm install barriertest
This part describes the regular sync tests.
Barrier uses RSpec-like syntax to describe tests, and it bundles Chai as the assertion framework, as so:
# Coffeescriptdescribe "MyObject"->it "sums two numbers"->expectsum23toeq5
You can nest describe blocks as you like:
# Coffeescriptdescribe "MyObject"->describe "some internal"->it "do something"->expectsomethingtoeqtrue
# Coffeescriptdescribe "MyObject"->someVar = nullbefore -> someVar = "hello"it "must set someVar"->expectsomeVartoeq"hello"
Also, we support lazy blocks for dependency injection:
# Coffeescriptdescribe "MyObject"->lazy "value"-> 50it "loads the value"expectvaluetoeq50
Note that the we do a reflection on the function to extract the variable name, them we lookup and build it for each test.
And you can inject dependencies on each other:
# Coffeescriptdescribe "MyObject"->lazy "value"-> 50lazy "value2" value + 10it "loads the values"expectvalue2toeq60
For basic async testing, you can call
async into the current object to make the test wait:
# Coffeescriptdescribe "Awesome"->it "supports async testing"->done = @asyncsetTimeout ->expecttruetobetruedone50
Despites the support on simple async testing, we really recommend you to use promises instead, as described on the next section.
We recognize that there is a lot of async code into Node, and also, we recognize that promises have a great value in helping to write better async code.
That's why Barrier supports and recommends promises usage over the entire testing process.
But actually there is not much to say about the usage, because it's just transparent! You can just use promises as values and we handle the rest!
Check some examples:
# Coffeescriptdescribe "Using Promises"->it "can use promises as values on expectations"-># imagine that loadUser and fetchRemoteAge are functions that returns# promises that will eventually handle de valuesexpectloadUser30tohaveProperty"age"fetchRemoteAge
Also, if your test returns a promise, the runner will wait for it:
# Coffeescriptdescribe "Delaying the runner"->it "will wait for my promise"->Q"value"delay30thenexpectvtoeq"value" # and that's it, Barrier will do the async handling magic
Before and after blocks does the same, if you return promises on they, the runner will wait before going on:
# Coffeescriptdescribe "Before promise me..."->user = nulluserDecorated = nullbefore -> loadUserthen user = u# note that before blocks run in series, so, it's safe to expect that previous# before blocks are donebefore -> userDecorated = decorateUseruserit "is awesome"-> expectuserDecoratedtonotbenull
But remember about Lazy Attributes? They can be promises too!
# Coffeescriptdescribe "Lazy Promises"->lazy "user"-> findUserOnDBit "will load the promise and inject it!"expectusernametoeq"sir"
And even better, you can do it while injecting lazy dependencies!
# Coffeescriptdescribe "Lazy Promises Dependencies!"->lazy "store"-> createStoreOnDblazy "user" createUserstore: storeidit "will load gracefully"expectuserstorenotnull
Get much more examples here