Barrier
A test framework that embraces promises for real
Installation
npm install barriertest
Basic Testing
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:
// Javascript ;
# Coffeescript describe "MyObject"-> it "sums two numbers"-> expectsum23toeq5
You can nest describe blocks as you like:
// Javascript ;
# Coffeescript describe "MyObject"-> describe "some internal"-> it "do something"-> expectsomethingtoeqtrue
We support before
, beforeEach
, after
and afterEach
clauses:
// Javascript ;
# Coffeescript describe "MyObject"-> someVar = null before -> someVar = "hello" it "must set someVar"-> expectsomeVartoeq"hello"
Also, we support lazy blocks for dependency injection:
// Javascript ;
# Coffeescript describe "MyObject"-> lazy "value"-> 50 it "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:
// Javascript ;
# Coffeescript describe "MyObject"-> lazy "value"-> 50 lazy "value2" value + 10 it "loads the values" expectvalue2toeq60
Async Testing
For basic async testing, you can call async
into the current object to make the test wait:
// Javascript ;
# Coffeescript describe "Awesome"-> it "supports async testing"-> done = @async setTimeout -> expecttruetobetrue done 50
Despites the support on simple async testing, we really recommend you to use promises instead, as described on the next section.
Promises Testing
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:
// Javascript ;
# Coffeescript describe "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 values expectloadUser30tohaveProperty"age"fetchRemoteAge
Also, if your test returns a promise, the runner will wait for it:
// Javascript ;
# Coffeescript describe "Delaying the runner"-> it "will wait for my promise"-> Q"value"delay30then expectvtoeq"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:
// Javascript ;
# Coffeescript describe "Before promise me..."-> user = null userDecorated = null before -> loadUserthen user = u # note that before blocks run in series, so, it's safe to expect that previous # before blocks are done before -> userDecorated = decorateUseruser it "is awesome"-> expectuserDecoratedtonotbenull
But remember about Lazy Attributes? They can be promises too!
// Javascript ;
# Coffeescript describe "Lazy Promises"-> lazy "user"-> findUserOnDB it "will load the promise and inject it!" expectusernametoeq"sir"
And even better, you can do it while injecting lazy dependencies!
// Javascript ;
# Coffeescript describe "Lazy Promises Dependencies!"-> lazy "store"-> createStoreOnDb lazy "user" createUserstore: storeid it "will load gracefully" expectuserstorenotnull
Get much more examples here
TODO (PR's would be welcome):
- Separate Interface code from Suite
- Verbose mode logging each step block
- Isolated tests flag (for parallel testing)
- TDD Interface