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

Commit 672807c

Browse files
author
Maksim Daunarovich
authored
Promisify emulator process (#26)
* Promisify emulator * Remove console.log * Allow to specify emulator ports * Directly set access api port * Add Prettier config * Add jest refactor package.json * Add docstrings to emulator methods * Add Jest assertion methods * Export new modules. Prettify. * Set es6 flag to true * Update files with missing headers * Fix eslint config * Add prettify script. Update lock file * Specify config for Prettier * Update documentation * Update prettier config * Update documentation * Bump version
1 parent 26cc6d4 commit 672807c

12 files changed

+342
-1059
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module.exports = {
2+
extends: ["eslint:recommended", "plugin:jest/recommended"],
23
env: {
34
node: true,
5+
es6: true
46
},
5-
extends: ["eslint:recommended", "plugin:jest/recommended"],
67
parserOptions: {
78
ecmaVersion: 12,
89
sourceType: "module",

.prettierrc.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"printWidth": 100,
3+
"endOfLine": "lf",
4+
"semi": true,
5+
"useTabs": false,
6+
"singleQuote": false,
7+
"trailingComma": "es5",
8+
"tabWidth": 2
9+
}

docs/api.md

+92-32
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,89 @@
11
# Table of Contents
2+
3+
- [Init](#init)
4+
- [Emulator](#emulator)
5+
- [start](#emulator.start)
6+
- [stop](#emulator.stop)
27
- [Accounts](#accounts)
3-
- [getAccountAddress](#getaccountaddressname)
8+
- [getAccountAddress](#getaccountaddressname)
49
- [Contracts](#contracts)
5-
- [deployContractByName](#deploycontractbynameprops)
6-
- [deployContract](#deploycontractbynameprops)
7-
- [getContractAddress](#getcontractaddressname-usedefaults--false)
10+
- [deployContractByName](#deploycontractbynameprops)
11+
- [deployContract](#deploycontractbynameprops)
12+
- [getContractAddress](#getcontractaddressname-usedefaults--false)
813
- [Cadence Code Templates](#cadence-code-templates)
9-
- [getTemplate](#gettemplatefile-addressmap---byaddress--false)
10-
- [getContractCode](#getcontractcodename-addressmap---service--false)
11-
- [getTransactionCode](#gettransactioncodename-addressmap---service--false)
12-
- [getScriptCode](#getscriptcodename-addressmap---service--false)
14+
- [getTemplate](#gettemplatefile-addressmap---byaddress--false)
15+
- [getContractCode](#getcontractcodename-addressmap---service--false)
16+
- [getTransactionCode](#gettransactioncodename-addressmap---service--false)
17+
- [getScriptCode](#getscriptcodename-addressmap---service--false)
1318
- [Send and Execute](#send-and-execute)
14-
- [sendTransaction](#sendtransactionprops)
15-
- [executeScript](#executescriptprops)
19+
- [sendTransaction](#sendtransactionprops)
20+
- [executeScript](#executescriptprops)
1621
- [FlowToken](#flowtoken)
17-
- [getFlowBalance](#getflowbalanceaddress)
18-
- [mintFlow](#mintflowrecipient-amount)
19-
22+
- [getFlowBalance](#getflowbalanceaddress)
23+
- [mintFlow](#mintflowrecipient-amount)
24+
2025
---
2126

27+
## Init
28+
29+
### init(basePath, port)
30+
31+
Initializes framework variables and specifies port to use for HTTP and grpc access.
32+
`port` is set to 8080 by default. grpc port is calculated to `3569 + (port - 8080)` to allow multiple instances
33+
of emulator to be run in parallel
34+
35+
- `basePath` - path to the folder, with Cadence template files
36+
- `port` - http port to use for access node
37+
38+
```javascript
39+
import path from "path";
40+
import { init } from "js-testing-framework";
41+
42+
describe("test setup", () => {
43+
beforeEach(async (done) => {
44+
const basePath = path.resolve(__dirname, "../cadence");
45+
init(basePath);
46+
47+
// alternatively you can pass specific port
48+
// init(basePath, 8085)
49+
50+
done();
51+
});
52+
});
53+
```
54+
55+
## Emulator
56+
57+
### emulator.start
58+
59+
Starts emulator on specified port.
60+
61+
- `port` - number representing a port to use for access API
62+
- `logging` - whether log messages from emulator shall be added to the output
63+
64+
### emulator.stop
65+
66+
Stops emulator instance.
67+
68+
```javascript
69+
describe("test setup", () => {
70+
// Instantiate emulator and path to Cadence files
71+
beforeEach(async (done) => {
72+
const basePath = path.resolve(__dirname, "../cadence");
73+
const port = 8080;
74+
init(basePath, port);
75+
await emulator.start(port, false);
76+
done();
77+
});
78+
79+
// Stop emulator, so it could be restarted
80+
afterEach(async (done) => {
81+
await emulator.stop();
82+
done();
83+
});
84+
});
85+
```
86+
2287
## Accounts
2388

2489
### getAccountAddress(name)
@@ -29,7 +94,7 @@ Next time when you call this method, it will grab exactly the same account. This
2994
and then use them throughout your code, without worrying that accounts match or trying to store/handle specific addresses.
3095

3196
```javascript
32-
import { getAccountAddress } from "flow-js-testing/dist";
97+
import { getAccountAddress } from "flow-js-testing";
3398

3499
const main = async () => {
35100
const Alice = await getAccountAddress("Alice");
@@ -58,7 +123,7 @@ Usage:
58123

59124
```javascript
60125
import path from "path";
61-
import { init, deployContractByName } from "flow-js-testing/dist";
126+
import { init, deployContractByName } from "flow-js-testing";
62127

63128
const main = async () => {
64129
init(path.resolve(__dirname, "../cadence"));
@@ -93,7 +158,7 @@ Usage:
93158

94159
```javascript
95160
import path from "path";
96-
import { deployContract } from "flow-js-testing/dist";
161+
import { deployContract } from "flow-js-testing";
97162

98163
const main = async () => {
99164
init(path.resolve(__dirname, "../cadence"));
@@ -133,7 +198,7 @@ Returns address of the account, where contract is currently deployed.
133198
> Though if you don't pass second argument, you can override contracts deployed by default.
134199
135200
```javascript
136-
import { getContractAddress } from "flow-js-testing/dist";
201+
import { getContractAddress } from "flow-js-testing";
137202

138203
const main = async () => {
139204
const contract = await getContractAddress("HelloWorld");
@@ -157,7 +222,7 @@ Returns Cadence template as string with addresses replaced using addressMap
157222

158223
```javascript
159224
import path from "path";
160-
import { init, getTemplate } from "flow-js-testing/dist";
225+
import { init, getTemplate } from "flow-js-testing";
161226

162227
const main = async () => {
163228
init(path.resolve(__dirname, "../cadence"));
@@ -178,7 +243,7 @@ Returns Cadence template from file with `name` in `_basepath/contracts` folder
178243

179244
```javascript
180245
import path from "path";
181-
import { init, getContractCode } from "flow-js-testing/dist";
246+
import { init, getContractCode } from "flow-js-testing";
182247

183248
const main = async () => {
184249
init(path.resolve(__dirname, "../cadence"));
@@ -206,7 +271,7 @@ Returns Cadence template from file with `name` in `_basepath/transactions` folde
206271

207272
```javascript
208273
import path from "path";
209-
import { init, getTransactionCode } from "flow-js-testing/dist";
274+
import { init, getTransactionCode } from "flow-js-testing";
210275

211276
const main = async () => {
212277
init(path.resolve(__dirname, "../cadence"));
@@ -235,7 +300,7 @@ Returns Cadence template from file with `name` in `_basepath/scripts` folder
235300

236301
```javascript
237302
import path from "path";
238-
import { init, getScriptCode } from "flow-js-testing/dist";
303+
import { init, getScriptCode } from "flow-js-testing";
239304

240305
const main = async () => {
241306
init(path.resolve(__dirname, "../cadence"));
@@ -259,12 +324,7 @@ If you don't have any contract dependencies, you can use those methods without s
259324

260325
```javascript
261326
import path from "path";
262-
import {
263-
init,
264-
getContractCode,
265-
getTransactionCode,
266-
getScriptCode,
267-
} from "flow-js-testing/dist";
327+
import { init, getContractCode, getTransactionCode, getScriptCode } from "flow-js-testing";
268328

269329
const main = async () => {
270330
init(path.resolve(__dirname, "../cadence"));
@@ -296,7 +356,7 @@ Usage:
296356

297357
```javascript
298358
import { Int, UFix64 } from "@onflow/types";
299-
import { deployContract } from "flow-js-testing/dist";
359+
import { deployContract } from "flow-js-testing";
300360

301361
const main = async () => {
302362
// Get signers adresses
@@ -349,7 +409,7 @@ Props object accepts following fields:
349409

350410
```javascript
351411
import { Int, UFix64 } from "@onflow/types";
352-
import { deployContract } from "flow-js-testing/dist";
412+
import { deployContract } from "flow-js-testing";
353413

354414
const main = async () => {
355415
// Read or create script code
@@ -396,7 +456,7 @@ Returns current FlowToken balance of account specified by address
396456
Usage:
397457

398458
```javascript
399-
import { getFlowBalance } from "flow-js-testing/dist";
459+
import { getFlowBalance } from "flow-js-testing";
400460

401461
const main = async () => {
402462
const Alice = await getAccountAddress("Alice");
@@ -420,7 +480,7 @@ Sends transaction to mint specified amount of FlowToken and send it to recipient
420480
- `amount` - amount to mint and send
421481

422482
```javascript
423-
import { mintFlow } from "flow-js-testing/dist";
483+
import { mintFlow } from "flow-js-testing";
424484

425485
const main = async () => {
426486
const Alice = await getAccountAddress("Alice");
@@ -434,4 +494,4 @@ const main = async () => {
434494
};
435495

436496
main();
437-
```
497+
```

docs/examples/basic.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Basic usage
2+
23
Before using any of the methods you need to `init` the framework, basically telling where you Cadence
34
code will live. In example below, we put all the Cadence code in the folder named `cadence` one level above the place
45
where your test script is located.
@@ -11,6 +12,7 @@ Let's create `deploy.test.js` file and write some basic test, which would create
1112

1213
```javascript
1314
import path from "path";
15+
import { getAccountAddress } from "flow-js-testing";
1416

1517
const basePath = path.resolve(__dirname, "../cadence");
1618

@@ -35,4 +37,4 @@ describe("Accounts", () => {
3537
});
3638
```
3739

38-
Run emulator with `flow emulator -v` and then in another terminal run `jest`
40+
Run emulator with `flow emulator -v` and then in another terminal run `jest`

docs/examples/metadata.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ In this example we will pass `{String:String}` dictionary and log it out.
33

44
```javascript
55
import path from "path";
6+
import * as t from "@onflow/types"
7+
import { init, executeScript } from "flow-js-testing"
68

79
const basePath = path.resolve(__dirname, "../cadence");
810

0 commit comments

Comments
 (0)