Pausable.test.js
is a fairly complex test, we have to go back and forth among many files to understand what every part does.
We first set a pauser role in Pausable.test.js:
But we have to jump to another js file for now. In line 5, we require a function from PublicRole.behavior.js.
const { shouldBehaveLikePublicRole } = require('../access/roles/PublicRole.behavior');
When we deploy the test, half of it will be about this shouldBehaveLikePublicRole
So, in PublicRole.behavior.js
, we go to our shouldBehaveLikePublicRole
function and start analyzing it to see how it works.
Now a bit of mocha:
Async/await
is a way to write asynchronous code.
-
async
ensures that the function returns a promise.
-
await
expression causes async
function execution to pause until a Promise
is resolved (fulfilled or rejected). Makes JavaScript wait until that promise settles and returns its result.
- When resumed, the value of the
await
expression is that of the fulfilled Promise
.
More about async/await.
Tests here are written following the best practices of TDD (Test Driven Development), but they are more like BDD (Behavior Driven Develpment), using describe()
, context()
, it()
, specify()
, before()
, after()
, beforeEach()
, and afterEach()
.
describe()
’s are commonly known as test suites, which contain test cases. They are merely used for grouping, and you can have groups within groups. So inside of 'should behave like public role'
we will have all the tests cases of this file.
beforeEach()
runs before each test case it()
.
context()
is just an alias for describe()
, and behaves the same way; it just provides a way to keep tests easier to read and organized. Similarly, specify()
is an alias for it()
.
This guide was very helpful to understand what each one of them do. We also have mocha documentation.