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

Commit

Permalink
Convert examples to Jest instead of using ESM loader (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Jul 14, 2022
1 parent 7d6d674 commit d1eb29b
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 65 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-turtles-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/flow-js-testing": patch
---

Convert examples to run in Jest environment instead of ESM loader
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1

# gallium is the v16 lts
- name: Setup Node.js lts/gallium
uses: actions/setup-node@v2
with:
node-version: lts/gallium

- name: Cache Node.js modules
uses: actions/cache@v2
with:
Expand All @@ -23,6 +29,7 @@ jobs:
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
- name: Install Flow CLI
run: brew install flow-cli
- name: Install dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import path from "path"
import {init, emulator, getAccountAddress} from "../src"

;(async () => {
beforeEach(async () => {
const basePath = path.resolve(__dirname, "./cadence")

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

test("get account address", async () => {
const Alice = await getAccountAddress("Alice")
console.log({Alice})

// Expect Alice to be address of Alice's account
expect(Alice).toMatch(/^0x[0-9a-f]{16}$/)
})

afterEach(async () => {
await emulator.stop()
})()
})
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import path from "path"
import {init, emulator, deployContractByName, executeScript} from "../src"

;(async () => {
beforeEach(async () => {
// Init framework
const basePath = path.resolve(__dirname, "./cadence")
await init(basePath)

// Start Emulator
await emulator.start()
})

test("deploy contract by name", async () => {
// Deploy contract Greeting with single argument
await deployContractByName({
name: "Greeting",
Expand All @@ -25,7 +27,7 @@ import {init, emulator, deployContractByName, executeScript} from "../src"
}
`,
})
console.log({greetingMessage})
expect(greetingMessage).toBe("Hello from Emulator")

// Deploy contract Hello with no arguments
await deployContractByName({name: "Hello"})
Expand All @@ -38,8 +40,10 @@ import {init, emulator, deployContractByName, executeScript} from "../src"
}
`,
})
console.log({helloMessage})
expect(helloMessage).toBe("Hi!")
})

afterEach(async () => {
// Stop Emulator
await emulator.stop()
})()
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {
executeScript,
} from "../src"

;(async () => {
beforeEach(async () => {
const basePath = path.resolve(__dirname, "../cadence")

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

test("deploy contract", async () => {
// We can specify, which account will hold the contract
const to = await getAccountAddress("Alice")

Expand All @@ -37,7 +39,9 @@ import {
}
`,
})
console.log({balance})
expect(balance).toBe(1337)
})

afterEach(async () => {
await emulator.stop()
})()
})
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import path from "path"
import {init, emulator, deployContractByName, getContractAddress} from "../src"

;(async () => {
beforeEach(async () => {
const basePath = path.resolve(__dirname, "./cadence")

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

test("get contract address", async () => {
// if we omit "to" it will be deployed to Service Account
// but let's pretend we don't know where it will be deployed :)
await deployContractByName({name: "Hello"})

const contractAddress = await getContractAddress("Hello")
console.log({contractAddress})

// Expect contractAddress to be address
expect(contractAddress).toMatch(/^0x[0-9a-f]{16}$/)
})

afterEach(async () => {
await emulator.stop()
})()
})
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import path from "path"
import {clearInterval} from "timers"
import {emulator, init, executeScript} from "../src"

;(async () => {
beforeEach(async () => {
const basePath = path.resolve(__dirname, "./cadence")
await init(basePath)

// Let's define simple method to log message to emulator console
const logMessage = async message => {
return executeScript({
code: `
pub fun main(){
log("------------> ${message}")
}
`,
})
}
await init(basePath)

// Let's enable logging initially
const logging = true
Expand All @@ -26,7 +17,20 @@ import {emulator, init, executeScript} from "../src"

// Start emulator instance on available ports
await emulator.start({logging})
})

// eslint-disable-next-line jest/expect-expect
test("emulator management", async () => {
// Let's define simple method to log message to emulator console
const logMessage = async message => {
return executeScript({
code: `
pub fun main(){
log("------------> ${message}")
}
`,
})
}
// This line will be visible in emulator output
emulator.setLogging(true)
await logMessage("Now you see me...")
Expand All @@ -46,6 +50,9 @@ import {emulator, init, executeScript} from "../src"

// Then silently turn it off
emulator.setLogging(false)
})

afterEach(async () => {
// Stop running emulator
await emulator.stop()
})()
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@ import {
mintFlow,
} from "../src"

;(async () => {
beforeEach(async () => {
const basePath = path.resolve(__dirname, "./cadence")

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

test("flow management", async () => {
// Get address for account with alias "Alice"
const Alice = await getAccountAddress("Alice")

// Get initial balance
const [initialBalance] = await getFlowBalance(Alice)
console.log({initialBalance})
expect(initialBalance).toBe("0.00100000")

// Add 1.0 FLOW tokens to Alice account
await mintFlow(Alice, "1.0")

// Check updated balance
const [updatedBalance] = await getFlowBalance(Alice)
console.log({updatedBalance})
const expectedBalance = parseFloat(initialBalance) + 1.0
expect(parseFloat(updatedBalance)).toBe(expectedBalance)

await emulator.stop()
})()
})
16 changes: 10 additions & 6 deletions examples/07-block-offset.js → examples/07-block-offset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import {
executeScript,
} from "../src"

;(async () => {
beforeEach(async () => {
const basePath = path.resolve(__dirname, "./cadence")

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

test("block offset", async () => {
const [initialBlockOffset] = await getBlockOffset()
console.log({initialBlockOffset})
expect(initialBlockOffset).toBe(0)

// "getCurrentBlock().height" in your Cadence code will be replaced by Manager to a mocked value
const code = `
Expand All @@ -26,20 +28,22 @@ import {

// We can check that non-transformed code still works just fine
const [normalResult] = await executeScript({code})
console.log({normalResult})
expect(normalResult).toBe(1)

// Offset current block height by 42
await setBlockOffset(42)
// Let's check that offset value on Manager is actually changed to 42
const [blockOffset] = await getBlockOffset()
console.log({blockOffset})
expect(blockOffset).toBe(42)

// "transformers" field expects array of functions to operate update the code.
// We will pass single operator "builtInMethods" provided by the framework to alter how getCurrentBlock().height is calculated
const transformers = [builtInMethods]
const [transformedResult] = await executeScript({code, transformers})
console.log({transformedResult})
expect(transformedResult).toBe(44)
})

afterEach(async () => {
// Stop the emulator
await emulator.stop()
})()
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import path from "path"
import {init, emulator, executeScript} from "../src"

;(async () => {
beforeEach(async () => {
const basePath = path.resolve(__dirname, "./cadence")

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

test("execute script", async () => {
// We have created a file called "log-args.cdc" under "./cadence/scripts" folder.
// It's available for use since we configured framework to use "./cadence" folder as root
const code = `
Expand Down Expand Up @@ -39,13 +41,15 @@ import {init, emulator, executeScript} from "../src"

const [fromCode] = await executeScript({code, args})
const [fromFile] = await executeScript({name, args})
console.log({fromCode})
console.log({fromFile})
expect(fromCode).toBe(fromFile)
expect(fromCode).toBe(42)

// "executeScript" also supports short form, accepting name of the file in "scripts folder
// and array of arguments
const [shortForm] = await executeScript("hello")
console.log({shortForm})
expect(shortForm).toBe("Hello from Cadence")
})

afterEach(async () => {
await emulator.stop()
})()
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import path from "path"
import {init, emulator, getAccountAddress, sendTransaction} from "../src"

;(async () => {
beforeEach(async () => {
const basePath = path.resolve(__dirname, "./cadence")

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

test("send transaction", async () => {
emulator.addFilter(`debug`)

const Alice = await getAccountAddress("Alice")
Expand All @@ -31,12 +33,17 @@ import {init, emulator, getAccountAddress, sendTransaction} from "../src"
// 2. Providing "name" field to read Cadence template from file in "./transaction" folder
const [txFileResult] = await sendTransaction({name, signers, args})

console.log("txInlineResult", txInlineResult)
console.log("txFileResult", txFileResult)
expect(txInlineResult).toBeTruthy()
expect(txFileResult).toBeTruthy()
expect(txInlineResult.statusCode).toBe(0)
expect(txFileResult.statusCode).toBe(0)

// 3. Providing name of the file in short form (name, signers, args)
const [txShortResult] = await sendTransaction(name, signers, args)
console.log("txShortResult", txShortResult)
expect(txShortResult).toBeTruthy()
expect(txShortResult.statusCode).toBe(0)
})

afterEach(async () => {
await emulator.stop()
})()
})
Loading

0 comments on commit d1eb29b

Please sign in to comment.