MAINNET:
Loading...
TESTNET:
Loading...
/
onflow.org
Flow Playground

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

import {emulator, init} from "@onflow/flow-js-testing"

describe("test setup", () => {
  // Instantiate emulator and path to Cadence files
  beforeEach(async () => {
    const basePath = path.resolve(__dirname, "../cadence")

    await init(basePath)

    // Start emulator instance on auto-selected available ports
    await emulator.start()
  })
})

emulator.stop()

Stops emulator instance. Returns Promise.

Arguments

This method does not expect any arguments.

Usage

import {emulator, init} from "@onflow/flow-js-testing"

describe("test setup", () => {
  // Instantiate emulator and path to Cadence files
  beforeEach(async () => {
    const basePath = path.resolve(__dirname, "../cadence")

    await init(basePath)
    await emulator.start()
  })

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

emulator.setLogging(newState)

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

Arguments

NameTypeDescription
newStatebooleanEnable/disable logging

Usage

import {emulator, init} from "@onflow/flow-js-testing"

describe("test setup", () => {
  // Instantiate emulator and path to Cadence files
  beforeEach(async () => {
    const basePath = path.resolve(__dirname, "../cadence")

    await init(basePath)
    await emulator.start()
  })

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

  test("basic test", async () => {
    // Turn on logging from begining
    emulator.setLogging(true)
    // some asserts and interactions

    // Turn off logging for later calls
    emulator.setLogging(false)
    // more asserts and interactions here
  })
})