mocha-behavior-tree
Hierarchical step definition for Mocha
Easily defining multiple next steps to test from the current state with a tree structure hierarchically. This is useful for performing tests in a natural sequence of actions and reducing duplication of test code.
Install
npm i -D mocha mocha-behavior-tree
Usage
const step =
Scenario, Given, When, Then
scenario
is proxy for describe
, and given
,when
,then
,and
are proxies for step
, just adding BDD style keywords as prefix to the step name.
Below test case
const assert = const scenario given and then when =
is executed as follows:
// running each step route of the trees from the root to the leaf Scenario: User purchases products Given: User login And: List products When: Add the first product to cart 1. User login 2. List products 3. Add the first product to cart ✓ Then: The product is in the cart When: Puchase the first products 1. User login 2. List products 3. Puchase the first products ✓ Then: An order for the product is created When: Select all products on the page 1. User login 2. List products 3. Select all products on the page ✓ Then: all products are selected And: Add selected products to cart 1. User login 2. List products 3. Select all products on the page 4. Add selected products to cart ✓ Then: The products are in the cart And: Puchase selected products 1. User login 2. List products 3. Select all products on the page 4. Puchase selected products ✓ Then: Orders for the products are created 5 passing
For integration with other test codes (or some IDE plugins), you can also do this:
Passing values to the next Step
Multiple values can be passed to next step within object key-values
Async testing
Use function name as step name
Step name can be omitted. then function name will be used to step name
result :
User purchases products Given: user And: user_logged_in And: products_are_listed_in_the_page When: add_the_first_product_to_cart ✓ Then: the_product_is_in_the_cart
This can be done more Cucumber way
// steps.jsmoduleexports = { ... } { ... } { ... } { ... } { ... } //test.jsconst _ =
More usages
You can find out more examples in the library's test cases