Skip to content

Commit

Permalink
Set up canister unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanasa committed Jul 1, 2023
1 parent ae800ae commit 22e20b7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- name: Install dfx
run: |
echo y | DFX_VERSION=$DFX_VERSION bash -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
- name: Start dfx
run: |
dfx cache install
dfx start --background
- run: npm run setup
- run: npm test
2 changes: 1 addition & 1 deletion backend/Backend.mo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import HttpParser "mo:http-parser.mo";
import Types "./Types";
import Utils "./Utils";

shared ({ caller = installer }) actor class Backend() {
actor class Backend() {
type Request = HttpParser.ParsedHttpRequest;
type HttpRequest = HttpParser.HttpRequest;
type HttpResponse = HttpParser.HttpResponse;
Expand Down
24 changes: 24 additions & 0 deletions backend/test/Backend.test.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Debug "mo:base/Debug";
import Text "mo:base/Text";

import { Backend } "../Backend";

let backend = await Backend();

let cases = [
("/services/oembed?format=xml&url=https%3A%2F%2Fembed.smartcontracts.org%2Fabcdef&maxheight=800&lines=5", "<?xml version=\221.0\22 encoding=\22utf-8\22 standalone=\22yes\22?><oembed><version>1.0</version><provider_name>Embed Motoko</provider_name><provider_url>https://embed.motoko.org</provider_url><type>rich</type><width>800</width><height>285</height><html>&lt;iframe src=&quot;https://embed.smartcontracts.org/abcdef&quot; width=&quot;800&quot; height=&quot;285&quot; style=&quot;border:0&quot; /&gt;</html></oembed>"),
];

for ((url, expected) in cases.vals()) {
Debug.print(url);
let request = {
method = "GET";
headers = [];
url;
body = "" : Blob;
};
let received = (await backend.http_request(request)).body;
Debug.print(debug_show Text.decodeUtf8(received));
assert received == Text.encodeUtf8(expected);
Debug.print("");
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
"frontend": "vite --port 3000",
"backend": "mo-dev --generate --deploy -y",
"build": "vite build",
"test": "vitest run",
"test:watch": "vitest",
"test": "run-s test:frontend test:backend",
"test:frontend": "vitest run",
"test:backend": "mo-test",
"test:watch": "run-p vitest mo-test",
"sources": "mops sources",
"prepare": "husky install",
"precommit": "lint-staged"
Expand Down

0 comments on commit 22e20b7

Please sign in to comment.