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

Commit

Permalink
Promisify emulator process (#26)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Maksim Daunarovich authored May 21, 2021
1 parent 26cc6d4 commit 672807c
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 1,059 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module.exports = {
extends: ["eslint:recommended", "plugin:jest/recommended"],
env: {
node: true,
es6: true
},
extends: ["eslint:recommended", "plugin:jest/recommended"],
parserOptions: {
ecmaVersion: 12,
sourceType: "module",
Expand Down
9 changes: 9 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"printWidth": 100,
"endOfLine": "lf",
"semi": true,
"useTabs": false,
"singleQuote": false,
"trailingComma": "es5",
"tabWidth": 2
}
124 changes: 92 additions & 32 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,89 @@
# Table of Contents

- [Init](#init)
- [Emulator](#emulator)
- [start](#emulator.start)
- [stop](#emulator.stop)
- [Accounts](#accounts)
- [getAccountAddress](#getaccountaddressname)
- [getAccountAddress](#getaccountaddressname)
- [Contracts](#contracts)
- [deployContractByName](#deploycontractbynameprops)
- [deployContract](#deploycontractbynameprops)
- [getContractAddress](#getcontractaddressname-usedefaults--false)
- [deployContractByName](#deploycontractbynameprops)
- [deployContract](#deploycontractbynameprops)
- [getContractAddress](#getcontractaddressname-usedefaults--false)
- [Cadence Code Templates](#cadence-code-templates)
- [getTemplate](#gettemplatefile-addressmap---byaddress--false)
- [getContractCode](#getcontractcodename-addressmap---service--false)
- [getTransactionCode](#gettransactioncodename-addressmap---service--false)
- [getScriptCode](#getscriptcodename-addressmap---service--false)
- [getTemplate](#gettemplatefile-addressmap---byaddress--false)
- [getContractCode](#getcontractcodename-addressmap---service--false)
- [getTransactionCode](#gettransactioncodename-addressmap---service--false)
- [getScriptCode](#getscriptcodename-addressmap---service--false)
- [Send and Execute](#send-and-execute)
- [sendTransaction](#sendtransactionprops)
- [executeScript](#executescriptprops)
- [sendTransaction](#sendtransactionprops)
- [executeScript](#executescriptprops)
- [FlowToken](#flowtoken)
- [getFlowBalance](#getflowbalanceaddress)
- [mintFlow](#mintflowrecipient-amount)
- [getFlowBalance](#getflowbalanceaddress)
- [mintFlow](#mintflowrecipient-amount)

---

## Init

### init(basePath, port)

Initializes framework variables and specifies port to use for HTTP and grpc access.
`port` is set to 8080 by default. grpc port is calculated to `3569 + (port - 8080)` to allow multiple instances
of emulator to be run in parallel

- `basePath` - path to the folder, with Cadence template files
- `port` - http port to use for access node

```javascript
import path from "path";
import { init } from "js-testing-framework";

describe("test setup", () => {
beforeEach(async (done) => {
const basePath = path.resolve(__dirname, "../cadence");
init(basePath);

// alternatively you can pass specific port
// init(basePath, 8085)

done();
});
});
```

## Emulator

### emulator.start

Starts emulator on specified port.

- `port` - number representing a port to use for access API
- `logging` - whether log messages from emulator shall be added to the output

### emulator.stop

Stops emulator instance.

```javascript
describe("test setup", () => {
// Instantiate emulator and path to Cadence files
beforeEach(async (done) => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
init(basePath, port);
await emulator.start(port, false);
done();
});

// Stop emulator, so it could be restarted
afterEach(async (done) => {
await emulator.stop();
done();
});
});
```

## Accounts

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

```javascript
import { getAccountAddress } from "flow-js-testing/dist";
import { getAccountAddress } from "flow-js-testing";

const main = async () => {
const Alice = await getAccountAddress("Alice");
Expand Down Expand Up @@ -58,7 +123,7 @@ Usage:

```javascript
import path from "path";
import { init, deployContractByName } from "flow-js-testing/dist";
import { init, deployContractByName } from "flow-js-testing";

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

```javascript
import path from "path";
import { deployContract } from "flow-js-testing/dist";
import { deployContract } from "flow-js-testing";

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

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

```javascript
import path from "path";
import { init, getTemplate } from "flow-js-testing/dist";
import { init, getTemplate } from "flow-js-testing";

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

```javascript
import path from "path";
import { init, getContractCode } from "flow-js-testing/dist";
import { init, getContractCode } from "flow-js-testing";

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

```javascript
import path from "path";
import { init, getTransactionCode } from "flow-js-testing/dist";
import { init, getTransactionCode } from "flow-js-testing";

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

```javascript
import path from "path";
import { init, getScriptCode } from "flow-js-testing/dist";
import { init, getScriptCode } from "flow-js-testing";

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

```javascript
import path from "path";
import {
init,
getContractCode,
getTransactionCode,
getScriptCode,
} from "flow-js-testing/dist";
import { init, getContractCode, getTransactionCode, getScriptCode } from "flow-js-testing";

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

```javascript
import { Int, UFix64 } from "@onflow/types";
import { deployContract } from "flow-js-testing/dist";
import { deployContract } from "flow-js-testing";

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

```javascript
import { Int, UFix64 } from "@onflow/types";
import { deployContract } from "flow-js-testing/dist";
import { deployContract } from "flow-js-testing";

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

```javascript
import { getFlowBalance } from "flow-js-testing/dist";
import { getFlowBalance } from "flow-js-testing";

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

```javascript
import { mintFlow } from "flow-js-testing/dist";
import { mintFlow } from "flow-js-testing";

const main = async () => {
const Alice = await getAccountAddress("Alice");
Expand All @@ -434,4 +494,4 @@ const main = async () => {
};

main();
```
```
4 changes: 3 additions & 1 deletion docs/examples/basic.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Basic usage

Before using any of the methods you need to `init` the framework, basically telling where you Cadence
code will live. In example below, we put all the Cadence code in the folder named `cadence` one level above the place
where your test script is located.
Expand All @@ -11,6 +12,7 @@ Let's create `deploy.test.js` file and write some basic test, which would create

```javascript
import path from "path";
import { getAccountAddress } from "flow-js-testing";

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

Expand All @@ -35,4 +37,4 @@ describe("Accounts", () => {
});
```

Run emulator with `flow emulator -v` and then in another terminal run `jest`
Run emulator with `flow emulator -v` and then in another terminal run `jest`
2 changes: 2 additions & 0 deletions docs/examples/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ In this example we will pass `{String:String}` dictionary and log it out.

```javascript
import path from "path";
import * as t from "@onflow/types"
import { init, executeScript } from "flow-js-testing"

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

Expand Down
Loading

0 comments on commit 672807c

Please sign in to comment.