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

Commit da5e666

Browse files
authored
Add log capture functionality to all interactions (#170)
1 parent 69b25e0 commit da5e666

15 files changed

+351
-186
lines changed

.changeset/moody-moose-work.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@onflow/flow-js-testing": minor
3+
---
4+
5+
Emulator logs are now captured when calling `executeScript`, `sendTransaction`, `deployContract`, and `deployContractByName`. They part of the tuple returned by these functions (i.e. `[result, error, logs]`) and are provided as an array of strings.

dev-test/deploy.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ describe("interactions - sendTransaction", () => {
3636
expect(address).toBe(serviceAccount)
3737
})
3838

39+
test("deploy basic contract - captures logs", async () => {
40+
const name = "HelloWorld"
41+
const [, , logs] = await shallPass(deployContractByName({name}))
42+
expect(logs).toEqual(["contract added to account"])
43+
})
44+
3945
test("deploy basic contract - to service account, short notation", async () => {
4046
const name = "HelloWorld"
4147
await deployContractByName(name)

dev-test/interaction.test.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ describe("interactions - sendTransaction", () => {
6363
})
6464
})
6565

66+
test("sendTransaction - shall capture logs", async () => {
67+
const [, , logs] = await shallPass(async () => {
68+
const code = `
69+
transaction{
70+
prepare(signer: AuthAccount){
71+
log("hello world")
72+
}
73+
}
74+
`
75+
return sendTransaction({code})
76+
})
77+
78+
expect(logs).toEqual(["hello world"])
79+
})
80+
6681
test("sendTransaction - shall pass with signer address", async () => {
6782
const Alice = await getAccountAddress("Alice")
6883

@@ -277,7 +292,7 @@ describe("interactions - executeScript", () => {
277292
return emulator.stop()
278293
})
279294

280-
test("executeScript - shall throw when no code and name provided", async () => {
295+
test("executeScript - shall throw when no code and name provided", async () => {
281296
shallThrow(async () => {
282297
return executeScript({})
283298
})
@@ -301,6 +316,23 @@ describe("interactions - executeScript", () => {
301316
})
302317
})
303318

319+
test("executeScript - shall capture logs", async () => {
320+
const [, , logs] = await shallResolve(async () => {
321+
const code = `
322+
pub fun main(){
323+
log("hello from cadence")
324+
log("this second log has been captured!")
325+
}
326+
`
327+
return executeScript({code})
328+
})
329+
330+
expect(logs).toEqual([
331+
"hello from cadence",
332+
"this second log has been captured!",
333+
])
334+
})
335+
304336
test("executeScript - shall pass with short notation", async () => {
305337
const [result, err] = await shallResolve(executeScript("log-message"))
306338
expect(err).toBe(null)

docs/api.md

+20-9
Original file line numberDiff line numberDiff line change
@@ -925,14 +925,25 @@ describe("interactions - sendTransaction", () => {
925925
const signers = [Alice]
926926
const args = ["Hello, Cadence"]
927927

928-
const [txResult, error] = await shallRevert(
928+
// Catch any cadence error
929+
let [txResult, error] = await shallRevert(
929930
sendTransaction({
930931
code,
931932
signers,
932933
args,
933934
})
934935
)
935936

937+
// Catch only specific panic message
938+
let [txResult, error] = await shallRevert(
939+
sendTransaction({
940+
code,
941+
signers,
942+
args,
943+
}),
944+
"You shall not pass!"
945+
)
946+
936947
// Transaction result will hold status, events and error message
937948
console.log({txResult}, {error})
938949
})
@@ -1053,8 +1064,8 @@ const main = async () => {
10531064
`
10541065
const args = ["Hello, from Cadence"]
10551066

1056-
const [result, error] = await executeScript({code, args})
1057-
console.log({result}, {error})
1067+
const [result, error, logs] = await executeScript({code, args})
1068+
console.log({result}, {error}, {logs})
10581069

10591070
// Stop emulator instance
10601071
await emulator.stop()
@@ -1099,8 +1110,8 @@ const main = async () => {
10991110
const args = ["Hello, from Cadence"]
11001111

11011112
// We assume there is a file `scripts/log-message.cdc` under base path
1102-
const [result, error] = await executeScript("log-message", args)
1103-
console.log({result}, {error})
1113+
const [result, error, logs] = await executeScript("log-message", args)
1114+
console.log({result}, {error}, {logs})
11041115

11051116
await emulator.stop()
11061117
}
@@ -1171,8 +1182,8 @@ const main = async () => {
11711182
const Alice = await getAccountAddress("Alice")
11721183
const signers = [Alice]
11731184

1174-
const [result, error] = await sendTransaction({code, args, signers})
1175-
console.log({result}, {error})
1185+
const [result, error, logs] = await sendTransaction({code, args, signers})
1186+
console.log({result}, {error}, {logs})
11761187

11771188
// Stop emulator instance
11781189
await emulator.stop()
@@ -1214,10 +1225,10 @@ const main = async () => {
12141225
// Define arguments we want to pass
12151226
const args = ["Hello, Cadence"]
12161227

1217-
const [result, error] = await shallPass(
1228+
const [result, error, logs] = await shallPass(
12181229
sendTransaction("log-message", [], args)
12191230
)
1220-
console.log({result}, {error})
1231+
console.log({result}, {error}, {logs})
12211232

12221233
// Stop the emulator instance
12231234
await emulator.stop()

docs/execute-scripts.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ const main = async () => {
5959
`
6060
const args = ["Hello, from Cadence"]
6161

62-
const [result, error] = await executeScript({code, args})
63-
console.log({result}, {error})
62+
const [result, error, logs] = await executeScript({code, args})
63+
console.log({result}, {error}, {logs})
6464

6565
// Stop emulator instance
6666
await emulator.stop()
@@ -105,8 +105,8 @@ const main = async () => {
105105
const args = ["Hello, from Cadence"]
106106

107107
// We assume there is a file `scripts/log-message.cdc` under base path
108-
const [result, error] = await executeScript("log-message", args)
109-
console.log({result}, {error})
108+
const [result, error, logs] = await executeScript("log-message", args)
109+
console.log({result}, {error}, {logs})
110110

111111
await emulator.stop()
112112
}

docs/send-transactions.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ const main = async () => {
108108
// Define arguments we want to pass
109109
const args = ["Hello, Cadence"]
110110

111-
const [result, error] = await shallPass(
111+
const [result, error, logs] = await shallPass(
112112
sendTransaction("log-message", [], args)
113113
)
114-
console.log({result}, {error})
114+
console.log({result}, {error}, {logs})
115115

116116
// Stop the emulator instance
117117
await emulator.stop()

examples/08-execute-script.test.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,28 @@ test("execute script", async () => {
3939
]
4040
const name = "log-args"
4141

42-
const [fromCode] = await shallResolve(executeScript({code, args}))
43-
const [fromFile] = await shallResolve(executeScript({name, args}))
42+
const [fromCode, , logsFromCode] = await shallResolve(
43+
executeScript({code, args})
44+
)
45+
const [fromFile, , logsFromFile] = await shallResolve(
46+
executeScript({name, args})
47+
)
48+
49+
// Expect logs to be as expected
50+
const expectedLogs = [
51+
"1337",
52+
"true",
53+
"Hello, Cadence",
54+
"1.33700000",
55+
"[1, 3, 3, 7]",
56+
'{"name": "Cadence", "status": "active"}',
57+
]
58+
expect(logsFromCode).toEqual(expectedLogs)
59+
expect(logsFromFile).toEqual(expectedLogs)
60+
4461
expect(fromCode).toBe(fromFile)
4562
expect(fromCode).toBe("42")
63+
expect(fromFile).toBe("42")
4664

4765
// "executeScript" also supports short form, accepting name of the file in "scripts folder
4866
// and array of arguments

examples/09-send-transaction.test.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,19 @@ test("send transaction", async () => {
3939
sendTransaction({code, signers, args})
4040
)
4141
// 2. Providing "name" field to read Cadence template from file in "./transaction" folder
42-
const [txFileResult] = await shallPass(sendTransaction({name, signers, args}))
42+
const [txFileResult, , fileLogs] = await shallPass(
43+
sendTransaction({name, signers, args})
44+
)
4345

4446
// 3. Providing name of the file in short form (name, signers, args)
45-
const [txShortResult] = await shallPass(sendTransaction(name, signers, args))
47+
const [txShortResult, , inlineLogs] = await shallPass(
48+
sendTransaction(name, signers, args)
49+
)
50+
51+
// Expect logs to be as expected
52+
const expectedLogs = ["Hello from Cadence", Alice.toString(), Bob.toString()]
53+
expect(fileLogs).toEqual(expectedLogs)
54+
expect(inlineLogs).toEqual(expectedLogs)
4655

4756
// Check that all transaction results are the same
4857
expect(txFileResult).toEqual(txInlineResult)

0 commit comments

Comments
 (0)