Emulator

How to start a new instance of emulator

Flow Javascript Testing Framework exposes emulator singleton allowing you to run and stop emulator instance programmatically. There are two methods available on it.

emulator.start(options)

Starts emulator on random available port, unless overriden in options. Returns Promise.

Arguments

NameTypeOptionalDescription
optionsEmulatorOptionsan object containing options for starting the emulator

EmulatorOptions

KeyTypeOptionalDescription
loggingbooleanwhether log messages from emulator shall be added to the output (default: false)
flagsstringcustom command-line flags to supply to the emulator (default: "")
adminPortnumberoverride the port which the emulator will run the admin server on (default: auto)
restPortnumberoverride the port which the emulator will run the REST server on (default: auto)
grpcPortnumberoverride the port which the emulator will run the GRPC server on (default: auto)

Returns

TypeDescription
PromisePromise, which resolves to true if emulator started successfully

Usage

1
import {emulator, init} from "@onflow/flow-js-testing"
2
3
describe("test setup", () => {
4
// Instantiate emulator and path to Cadence files
5
beforeEach(async () => {
6
const basePath = path.resolve(__dirname, "../cadence")
7
8
await init(basePath)
9
10
// Start emulator instance on auto-selected available ports
11
await emulator.start()
12
})
13
})

emulator.stop()

Stops emulator instance. Returns Promise.

Arguments

This method does not expect any arguments.

Usage

1
import {emulator, init} from "@onflow/flow-js-testing"
2
3
describe("test setup", () => {
4
// Instantiate emulator and path to Cadence files
5
beforeEach(async () => {
6
const basePath = path.resolve(__dirname, "../cadence")
7
8
await init(basePath)
9
await emulator.start()
10
})
11
12
// Stop emulator, so it could be restarted
13
afterEach(async () => {
14
await emulator.stop()
15
})
16
})

emulator.setLogging(newState)

Set logging flag on emulator, allowing to temporally enable/disable logging.

Arguments

NameTypeDescription
newStatebooleanEnable/disable logging

Usage

1
import {emulator, init} from "@onflow/flow-js-testing"
2
3
describe("test setup", () => {
4
// Instantiate emulator and path to Cadence files
5
beforeEach(async () => {
6
const basePath = path.resolve(__dirname, "../cadence")
7
8
await init(basePath)
9
await emulator.start()
10
})
11
12
// Stop emulator, so it could be restarted
13
afterEach(async () => {
14
await emulator.stop()
15
})
16
17
test("basic test", async () => {
18
// Turn on logging from begining
19
emulator.setLogging(true)
20
// some asserts and interactions
21
22
// Turn off logging for later calls
23
emulator.setLogging(false)
24
// more asserts and interactions here
25
})
26
})