Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 1e35afb

Browse files
author
Maksim Daunarovich
authored
Update flow-cadut dependency. Introduce bootstrap utility (#46)
* Create metadata tests * Update flow-cadut version. Fix jest config * Fix logging for emulator exit * Disable logging in tests * Prettify * Setup bin field. Add Prettier config. Install yargs package * Setup bin executable * Create templates, commands and CLI processor * Add make command * Refactor imports * Bump version * CHANGELOG added * Add flag to make command to specify base path * Create documentation for bootstrap utility * Set jest matcher * Remove prettier duplicate * Update fcl dependency * Add more templates for dev tests * Add proposed changes * Destructure arguments in short notation based on interaction type * Update dev tests * Bump @onflow/types * Prettify * Move isObject to utils module * Refactor deployByName to allow short notation * Add dev tests for deployment methods * Add header * Prettify * Refactor prefix
1 parent a90459d commit 1e35afb

25 files changed

+26884
-15205
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### 0.1.13 - 2021-07-28
2+
3+
- Bootstrap utility added to framework
4+
- Update `flow-cadut` dependency and fixed support for complex types
5+
- Fixed issue with emulator throwing an error with `logging` flag set to `true`

babel.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
2+
presets: [["@babel/preset-env", { targets: { node: "current" } }]],
33
};

bin/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require("esm")(module /*, options*/)("../src/cli").run(process.argv);
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
pub fun main(){
1+
pub fun main(): Int{
22
log("hello from cadence")
3+
return 42
34
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub fun main(message: String):String{
2+
log(message)
3+
return message
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
transaction(message:String){
2+
prepare(signer: AuthAccount){
3+
log(message)
4+
}
5+
}

dev-test/deploy.test.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import path from "path";
2+
import {
3+
emulator,
4+
init,
5+
deployContractByName,
6+
getContractAddress,
7+
getAccountAddress,
8+
getServiceAddress,
9+
} from "../src";
10+
11+
// We need to set timeout for a higher number, cause some transactions might take up some time
12+
jest.setTimeout(10000);
13+
14+
describe("interactions - sendTransaction", () => {
15+
// Instantiate emulator and path to Cadence files
16+
beforeEach(async () => {
17+
const basePath = path.resolve(__dirname, "./cadence");
18+
const port = 8080;
19+
await init(basePath, { port });
20+
return emulator.start(port);
21+
});
22+
23+
// Stop emulator, so it could be restarted
24+
afterEach(async () => {
25+
return emulator.stop();
26+
});
27+
28+
test("deploy basic contract - to service account", async () => {
29+
const name = "HelloWorld";
30+
await deployContractByName({ name });
31+
const address = await getContractAddress(name);
32+
const serviceAccount = await getServiceAddress();
33+
expect(address).toBe(serviceAccount);
34+
});
35+
36+
test("deploy basic contract - to service account, short notation", async () => {
37+
const name = "HelloWorld";
38+
await deployContractByName(name);
39+
const address = await getContractAddress(name);
40+
const serviceAccount = await getServiceAddress();
41+
expect(address).toBe(serviceAccount);
42+
});
43+
44+
test("deploy basic contract - to Alice account", async () => {
45+
const Alice = await getAccountAddress("Alice");
46+
const name = "HelloWorld";
47+
await deployContractByName({ name, to: Alice });
48+
const address = await getContractAddress(name);
49+
expect(address).toBe(Alice);
50+
});
51+
52+
test("deploy basic contract - to Alice account, short notation", async () => {
53+
const name = "HelloWorld";
54+
const Alice = await getAccountAddress("Alice");
55+
await deployContractByName(name, Alice);
56+
const address = await getContractAddress(name);
57+
expect(address).toBe(Alice);
58+
});
59+
});

dev-test/imports.test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from "path";
2-
import { emulator, init, deployContract, resolveImports, getAccountAddress } from "../src";
2+
import { emulator, init, deployContract, resolveImports, getServiceAddress } from "../src";
33
import { defaultsByName } from "../src/file";
4-
import { getServiceAddress } from "../src/manager";
54

65
jest.setTimeout(10000);
76

dev-test/interaction.test.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
init,
66
sendTransaction,
77
executeScript,
8+
getAccountAddress,
89
shallResolve,
910
shallThrow,
1011
shallPass,
@@ -19,7 +20,7 @@ describe("interactions - sendTransaction", () => {
1920
const basePath = path.resolve(__dirname, "./cadence");
2021
const port = 8080;
2122
await init(basePath, { port });
22-
return emulator.start(port, false);
23+
return emulator.start(port);
2324
});
2425

2526
// Stop emulator, so it could be restarted
@@ -102,7 +103,34 @@ describe("interactions - sendTransaction", () => {
102103
return sendTransaction({ code, args });
103104
});
104105
});
106+
107+
test("sendTransaction - short notation, no signers", async () => {
108+
emulator.setLogging(true);
109+
await shallPass(async () => {
110+
return sendTransaction("log-signer-address");
111+
});
112+
});
113+
114+
test("sendTransaction - short notation, Alice signed", async () => {
115+
emulator.setLogging(true);
116+
await shallPass(async () => {
117+
const Alice = await getAccountAddress("Alice");
118+
const signers = [Alice];
119+
return sendTransaction("log-signer-address", signers);
120+
});
121+
});
122+
123+
test("sendTransaction - short notation, Alice signed, with args", async () => {
124+
emulator.setLogging(true);
125+
await shallPass(async () => {
126+
const args = ["Hello, from Cadence!"];
127+
const Alice = await getAccountAddress("Alice");
128+
const signers = [Alice];
129+
return sendTransaction("log-message", signers, args);
130+
});
131+
});
105132
});
133+
106134
describe("interactions - executeScript", () => {
107135
// Instantiate emulator and path to Cadence files
108136
beforeEach(async () => {
@@ -140,4 +168,18 @@ describe("interactions - executeScript", () => {
140168
return executeScript({ code });
141169
});
142170
});
171+
172+
test("executeScript - shall pass with short notation", async () => {
173+
const result = await shallResolve(executeScript("log-message"));
174+
expect(result).toBe(42);
175+
});
176+
177+
test("executeScript - shall pass with short notation and arguments", async () => {
178+
const message = "Hello, from Cadence!";
179+
const result = await shallResolve(() => {
180+
const args = [message];
181+
return executeScript("log-passed-message", args);
182+
});
183+
expect(result).toBe(message);
184+
});
143185
});

dev-test/metadata.test.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import path from "path";
2+
import { init, emulator, executeScript, shallResolve } from "../src";
3+
import { mapValuesToCode } from "flow-cadut";
4+
5+
jest.setTimeout(10000);
6+
7+
describe("metadata examples", () => {
8+
beforeEach(async () => {
9+
const basePath = path.resolve("./cadence");
10+
const port = 8080;
11+
await init(basePath, { port });
12+
return emulator.start(port);
13+
});
14+
15+
afterEach(async () => {
16+
return emulator.stop();
17+
});
18+
19+
test("simple dictionary - {String: String}", async () => {
20+
const code = `
21+
pub fun main(metadata: {String: String}): String{
22+
return metadata["name"]!
23+
}
24+
`;
25+
const name = "Cadence";
26+
const args = [{ name }];
27+
const result = await shallResolve(executeScript({ code, args }));
28+
expect(result).toBe(name);
29+
});
30+
31+
test("simple dictionary - {String: Int}", async () => {
32+
const code = `
33+
pub fun main(metadata: {String: Int}): Int{
34+
return metadata["answer"]!
35+
}
36+
`;
37+
const answer = 42;
38+
const args = [{ answer }];
39+
const result = await shallResolve(executeScript({ code, args }));
40+
expect(result).toBe(answer);
41+
});
42+
43+
test("simple array - [String]", async () => {
44+
const code = `
45+
pub fun main(list: [String]): String{
46+
return list[0]
47+
}
48+
`;
49+
const value = "test";
50+
const args = [[value]];
51+
const result = await shallResolve(executeScript({ code, args }));
52+
expect(result).toBe(value);
53+
});
54+
55+
test("nested arrays - [[Int]]", async () => {
56+
const code = `
57+
pub fun main(list: [[Int]], index: Int): Int {
58+
log("this is log message we want to output")
59+
log(list[0][0])
60+
log(list[0][1])
61+
log(list[0][2])
62+
return list[0][index]
63+
}
64+
`;
65+
const value = [1, 3, 3, 7];
66+
const index = 3;
67+
const args = [[value], index];
68+
69+
try {
70+
const result = await executeScript({ code, args });
71+
expect(result).toBe(value[index]);
72+
} catch (e) {
73+
console.error(e);
74+
}
75+
});
76+
});

dev-test/usage.test.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
getFlowBalance,
1111
shallRevert,
1212
shallResolve,
13-
shallPass, shallThrow
13+
shallPass,
14+
shallThrow,
1415
} from "../src";
1516

1617
// We need to set timeout for a higher number, because some transactions might take up some time
@@ -21,7 +22,7 @@ describe("Basic Usage test", () => {
2122
beforeEach(async () => {
2223
const basePath = path.resolve(__dirname, "./cadence");
2324
const port = 8080;
24-
init(basePath, port);
25+
await init(basePath, port);
2526
return emulator.start(port, false);
2627
});
2728

@@ -59,7 +60,7 @@ describe("jest methods", () => {
5960
async () =>
6061
new Promise((_, reject) => {
6162
reject("something is wrong");
62-
})
63+
}),
6364
);
6465
});
6566

@@ -69,7 +70,7 @@ describe("jest methods", () => {
6970
async () =>
7071
new Promise((resolve) => {
7172
resolve(ALL_GOOD);
72-
})
73+
}),
7374
);
7475
expect(result).toBe(ALL_GOOD);
7576
});
@@ -82,16 +83,16 @@ describe("jest methods", () => {
8283
status: 4,
8384
errorMessage: "",
8485
});
85-
})
86+
}),
8687
);
8788
});
8889

8990
test("shall throw error", async () => {
9091
await shallThrow(
9192
async () =>
9293
new Promise(() => {
93-
throw Error("didn't happen")
94-
})
94+
throw Error("didn't happen");
95+
}),
9596
);
9697
});
9798
});

docs/generator.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: Bootstrap Framework
3+
sidebar_title: Bootstrap Framework
4+
description: How to quickly init testing environment
5+
---
6+
7+
### Init Testing Environment
8+
9+
Bootstrap your testing environment with a single command:
10+
11+
```shell
12+
npx flow-js-testing init
13+
```
14+
15+
When you run this command in the terminal it will initiate npm package in your current directory and create `package.json` file.
16+
Then it will install dependencies. After the installation is finished, the utility will create the required config files for Babel, Jest and Flow CLI.
17+
18+
> ⚠️ **Warning:** This command will overwrite `babel.config.sj`, `jest.config.js` and `flow.json` files in the folder, where
19+
> it would be executed and also could affect your `package.json` file. That's why we advise you to use new empty folder
20+
> to contain your Cadence related tests.
21+
22+
### Generate New Test Suit
23+
24+
Create a test suit file for your project with all necessary imports and setup for describe blocks.
25+
You can start writing your asserts and expectations right away:
26+
27+
```shell
28+
npx flow-js-testing make [name]
29+
```
30+
31+
#### Flags
32+
33+
| Name | Description |
34+
| --------------------- | ------------------------------------ |
35+
| `-c` or `--clear` | Exclude comments from test suit code |
36+
| `-b` or `--base-path` | Specify base path to Cadence folder |
37+
38+
If you do not specify `name` as second argument, the tool will give your file a unique name.

0 commit comments

Comments
 (0)