#S:MODE=test #S:EXTERNAL=rust=hello_test.rs #S:EXTERNAL=javascript=hello_test.js=test
Welcome to the Hello Test tutorial. Today you will be learning how to test your Holochain apps. This tutorial will add to the previous Hello Holo tutorial, so make sure you do that one first.
Testing is a really important part of building higher quality apps but it's also a an excellent way to think through how your app will be used.
When you ran hc init
in the previous tutorial Holochain already generated some tests for you.
The tests are written in JavaScript and use the Holochain testing framework Diorama, along with a popular test harness called Tape. You can run them with Node.JS, a runtime that lets you execute JavaScript in the terminal.
Open up the cc_tuts/test/index.js
in your favourite text editor. Have a look through the code.
Imports required to do testing: #S:INCLUDE
const path = require('path')
const tape = require('tape')
const { Diorama, tapeExecutor, backwardCompatibilityMiddleware } = require('@holochain/diorama')
This is a catch-all error logger that will let you know if a Promise
fails and there's no error handler to hear it. Promise
s are a way of simplifying complex asynchronous code, and Diorama uses a lot of them.
process.on('unhandledRejection', error => {
console.error('got unhandledRejection:', error);
});
The path to your compiled DNA.
const dnaPath = path.join(__dirname, "../dist/cc_tuts.dna.json")
const dna = Diorama.dna(dnaPath, 'cc_tuts')
Setup a testing scenario. This creates two agents: Alice and Bob.
const diorama = new Diorama({
instances: {
alice: dna,
bob: dna,
},
bridges: [],
debugLog: false,
executor: tapeExecutor(require('tape')),
middleware: backwardCompatibilityMiddleware,
})
This is the test that Holochain generated based on the my_entry
struct and the zome functions that work with it. We removed them in our Hello Holo tutorial, so let's remove the test.
Remove the following section:
#S:SKIP
diorama.registerScenario("description of example test", async (s, t, { alice }) => {
// Make a call to a Zome function
// indicating the function, and passing it an input
const addr = await alice.call("my_zome", "create_my_entry", {"entry" : {"content":"sample content"}})
const result = await alice.call("my_zome", "get_my_entry", {"address": addr.Ok})
// check for equality of the actual and expected results
t.deepEqual(result, { Ok: { App: [ 'my_entry', '{"content":"sample content"}' ] } })
})
This line will run the tests that you have set up.
diorama.run()
Tests are organized by creating scenarios. Think of them as a series of actions that the user or group of users take when interacting with your app.
For this test you simply want to get the Alice user to call the hello_holo
zome function. Then check that you get the result Hello Holo
.
Place the following just above diorama.run()
.
Register a test scenario that checks hello_holo()
returns the correct value:
#S:INCLUDE
diorama.registerScenario("Test hello holo", async (s, t, { alice }) => {
Make a call to the hello_holo
Zome function, passing no arguments:
const result = await alice.call("hello", "hello_holo", {});
Make sure the result is okay:
t.ok(result.Ok);
Check that the result matches what you expected:
t.deepEqual(result, { Ok: 'Hello Holo' })
})
#S:INCLUDE,HIDE
diorama.run()
#S:CHECK=javascript
Now in the hello_helo
directory, run the test like this:
$ hc test
This will compile and run the test scenario you just wrote. You will see a lot of output.
!!! success "If everything went okay, then right at the end you will see:" ``` # tests 2 # pass 2
# ok
```
Congratulations; you have tested your first Holochain app. Look at you go!
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing