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

Commit d1eb29b

Browse files
authored
Convert examples to Jest instead of using ESM loader (#144)
1 parent 7d6d674 commit d1eb29b

17 files changed

+142
-65
lines changed

.changeset/selfish-turtles-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@onflow/flow-js-testing": patch
3+
---
4+
5+
Convert examples to run in Jest environment instead of ESM loader

.github/workflows/ci.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/checkout@v2
16-
- uses: actions/setup-node@v1
16+
17+
# gallium is the v16 lts
18+
- name: Setup Node.js lts/gallium
19+
uses: actions/setup-node@v2
20+
with:
21+
node-version: lts/gallium
22+
1723
- name: Cache Node.js modules
1824
uses: actions/cache@v2
1925
with:
@@ -23,6 +29,7 @@ jobs:
2329
restore-keys: |
2430
${{ runner.OS }}-node-
2531
${{ runner.OS }}-
32+
2633
- name: Install Flow CLI
2734
run: brew install flow-cli
2835
- name: Install dependencies
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import path from "path"
22
import {init, emulator, getAccountAddress} from "../src"
33

4-
;(async () => {
4+
beforeEach(async () => {
55
const basePath = path.resolve(__dirname, "./cadence")
66

77
await init(basePath)
88
await emulator.start()
9+
})
910

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

14+
// Expect Alice to be address of Alice's account
15+
expect(Alice).toMatch(/^0x[0-9a-f]{16}$/)
16+
})
17+
18+
afterEach(async () => {
1319
await emulator.stop()
14-
})()
20+
})

examples/02-deploy-contract-by-name.js renamed to examples/02-deploy-contract-by-name.test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import path from "path"
22
import {init, emulator, deployContractByName, executeScript} from "../src"
33

4-
;(async () => {
4+
beforeEach(async () => {
55
// Init framework
66
const basePath = path.resolve(__dirname, "./cadence")
77
await init(basePath)
88

99
// Start Emulator
1010
await emulator.start()
11+
})
1112

13+
test("deploy contract by name", async () => {
1214
// Deploy contract Greeting with single argument
1315
await deployContractByName({
1416
name: "Greeting",
@@ -25,7 +27,7 @@ import {init, emulator, deployContractByName, executeScript} from "../src"
2527
}
2628
`,
2729
})
28-
console.log({greetingMessage})
30+
expect(greetingMessage).toBe("Hello from Emulator")
2931

3032
// Deploy contract Hello with no arguments
3133
await deployContractByName({name: "Hello"})
@@ -38,8 +40,10 @@ import {init, emulator, deployContractByName, executeScript} from "../src"
3840
}
3941
`,
4042
})
41-
console.log({helloMessage})
43+
expect(helloMessage).toBe("Hi!")
44+
})
4245

46+
afterEach(async () => {
4347
// Stop Emulator
4448
await emulator.stop()
45-
})()
49+
})
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import {
77
executeScript,
88
} from "../src"
99

10-
;(async () => {
10+
beforeEach(async () => {
1111
const basePath = path.resolve(__dirname, "../cadence")
1212

1313
await init(basePath)
1414
await emulator.start()
15+
})
1516

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

@@ -37,7 +39,9 @@ import {
3739
}
3840
`,
3941
})
40-
console.log({balance})
42+
expect(balance).toBe(1337)
43+
})
4144

45+
afterEach(async () => {
4246
await emulator.stop()
43-
})()
47+
})
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import path from "path"
22
import {init, emulator, deployContractByName, getContractAddress} from "../src"
33

4-
;(async () => {
4+
beforeEach(async () => {
55
const basePath = path.resolve(__dirname, "./cadence")
66

77
await init(basePath)
88
await emulator.start()
9+
})
910

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

1416
const contractAddress = await getContractAddress("Hello")
15-
console.log({contractAddress})
1617

18+
// Expect contractAddress to be address
19+
expect(contractAddress).toMatch(/^0x[0-9a-f]{16}$/)
20+
})
21+
22+
afterEach(async () => {
1723
await emulator.stop()
18-
})()
24+
})
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
import path from "path"
2+
import {clearInterval} from "timers"
23
import {emulator, init, executeScript} from "../src"
34

4-
;(async () => {
5+
beforeEach(async () => {
56
const basePath = path.resolve(__dirname, "./cadence")
6-
await init(basePath)
77

8-
// Let's define simple method to log message to emulator console
9-
const logMessage = async message => {
10-
return executeScript({
11-
code: `
12-
pub fun main(){
13-
log("------------> ${message}")
14-
}
15-
`,
16-
})
17-
}
8+
await init(basePath)
189

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

2718
// Start emulator instance on available ports
2819
await emulator.start({logging})
20+
})
2921

22+
// eslint-disable-next-line jest/expect-expect
23+
test("emulator management", async () => {
24+
// Let's define simple method to log message to emulator console
25+
const logMessage = async message => {
26+
return executeScript({
27+
code: `
28+
pub fun main(){
29+
log("------------> ${message}")
30+
}
31+
`,
32+
})
33+
}
3034
// This line will be visible in emulator output
3135
emulator.setLogging(true)
3236
await logMessage("Now you see me...")
@@ -46,6 +50,9 @@ import {emulator, init, executeScript} from "../src"
4650

4751
// Then silently turn it off
4852
emulator.setLogging(false)
53+
})
54+
55+
afterEach(async () => {
4956
// Stop running emulator
5057
await emulator.stop()
51-
})()
58+
})
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,28 @@ import {
77
mintFlow,
88
} from "../src"
99

10-
;(async () => {
10+
beforeEach(async () => {
1111
const basePath = path.resolve(__dirname, "./cadence")
1212

1313
await init(basePath)
1414
await emulator.start()
15+
})
1516

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

1921
// Get initial balance
2022
const [initialBalance] = await getFlowBalance(Alice)
21-
console.log({initialBalance})
23+
expect(initialBalance).toBe("0.00100000")
2224

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

2628
// Check updated balance
2729
const [updatedBalance] = await getFlowBalance(Alice)
28-
console.log({updatedBalance})
30+
const expectedBalance = parseFloat(initialBalance) + 1.0
31+
expect(parseFloat(updatedBalance)).toBe(expectedBalance)
2932

3033
await emulator.stop()
31-
})()
34+
})
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ import {
88
executeScript,
99
} from "../src"
1010

11-
;(async () => {
11+
beforeEach(async () => {
1212
const basePath = path.resolve(__dirname, "./cadence")
1313

1414
await init(basePath)
1515
await emulator.start()
16+
})
1617

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

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

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

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

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

46+
afterEach(async () => {
4347
// Stop the emulator
4448
await emulator.stop()
45-
})()
49+
})
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import path from "path"
22
import {init, emulator, executeScript} from "../src"
33

4-
;(async () => {
4+
beforeEach(async () => {
55
const basePath = path.resolve(__dirname, "./cadence")
66

77
await init(basePath)
88
await emulator.start()
9+
})
910

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

4042
const [fromCode] = await executeScript({code, args})
4143
const [fromFile] = await executeScript({name, args})
42-
console.log({fromCode})
43-
console.log({fromFile})
44+
expect(fromCode).toBe(fromFile)
45+
expect(fromCode).toBe(42)
4446

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

53+
afterEach(async () => {
5054
await emulator.stop()
51-
})()
55+
})

0 commit comments

Comments
 (0)