Skip to content

Commit 13064a7

Browse files
feat: add forc tests to create fuels (#3585)
* feat: added tests * chore: added test workflow * chore: fix CI * chore: bump `forc` to `0.66.6` * chore: changeset * docs: updated docs * chore: added `forc` to knip * chore: fix namespaces * chore: update templates * chore: appeasing the `forc fmt` * chore: fix import * chore: changeset * Update .changeset/lemon-avocados-walk.md * Update .changeset/lemon-avocados-walk.md * chore: ignore forc --------- Co-authored-by: Sérgio Torres <[email protected]>
1 parent 2ec0dac commit 13064a7

File tree

18 files changed

+206
-3
lines changed

18 files changed

+206
-3
lines changed

.changeset/loud-deers-wait.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-fuels": patch
3+
---
4+
5+
feat: add `forc` tests to create fuels

.github/workflows/test.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ jobs:
9191
env:
9292
PUBLISHED_NPM_TAG: next
9393

94+
forc:
95+
runs-on: ubuntu-latest
96+
timeout-minutes: 25
97+
steps:
98+
- name: Checkout
99+
uses: actions/checkout@v4
100+
101+
- name: Test Setup
102+
uses: ./.github/actions/test-setup
103+
104+
- name: Setup forc and fuel-core paths
105+
shell: bash
106+
run: |
107+
echo "$PWD/internal/forc/forc-binaries" >> $GITHUB_PATH
108+
echo "$PWD/internal/fuel-core/fuel-core-binaries" >> $GITHUB_PATH
109+
110+
- name: Run Tests
111+
run: pnpm test:forc
112+
94113
e2e:
95114
runs-on: ubuntu-latest
96115
timeout-minutes: 25

.knip.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"apps/docs/src/guide/types/snippets/numbers/for-u8-u16-and-u32-2.ts"
55
],
66
"ignore": [".github/**"],
7+
"ignoreBinaries": ["forc"],
78
"ignoreDependencies": [
89
"fuels",
910
"bun",

apps/create-fuels-counter-guide/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"test:ui": "sh ./test/ui/test-ui.sh",
8+
"test:forc": "forc test --path ./sway-programs",
89
"original:dev": "vite",
910
"original:build": "tsc -b && vite build",
1011
"original:start": "vite start",

apps/create-fuels-counter-guide/sway-programs/contract/src/main.sw

+33
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,36 @@ impl Counter for Contract {
4444
}
4545
}
4646
// #endregion create-fuels-counter-guide-impl
47+
48+
#[test]
49+
fn should_get_count() {
50+
let contract_instance = abi(Counter, CONTRACT_ID);
51+
let expected = 0;
52+
53+
let actual = contract_instance.get_count();
54+
55+
assert(actual == expected);
56+
}
57+
58+
#[test]
59+
fn should_increment_counter() {
60+
let contract_instance = abi(Counter, CONTRACT_ID);
61+
let count_before = contract_instance.get_count();
62+
let expected = count_before + 1;
63+
64+
let count_after = contract_instance.increment_counter(1);
65+
66+
assert(count_after == expected);
67+
}
68+
69+
// #region create-fuels-counter-guide-sway-contract-test
70+
#[test]
71+
fn test_decrement_counter() {
72+
let contract_instance = abi(Counter, CONTRACT_ID);
73+
let _ = contract_instance.increment_counter(5);
74+
75+
let count_before = contract_instance.get_count();
76+
let count_after = contract_instance.decrement_counter(1);
77+
assert(count_after == count_before - 1);
78+
}
79+
// #endregion create-fuels-counter-guide-sway-contract-test

apps/create-fuels-counter-guide/sway-programs/predicate/src/main.sw

+18
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,21 @@ configurable {
1010
fn main(pin: u64) -> bool {
1111
return PIN == pin;
1212
}
13+
14+
#[test]
15+
fn should_return_true_when_password_is_1337() {
16+
let expected = true;
17+
18+
let actual = main(1337);
19+
20+
assert(actual == expected);
21+
}
22+
23+
#[test]
24+
fn should_return_false_when_password_is_not_1337() {
25+
let expected = false;
26+
27+
let actual = main(1338);
28+
29+
assert(actual == expected);
30+
}

apps/create-fuels-counter-guide/sway-programs/script/src/main.sw

+9
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@ script;
44
fn main(input: u64) -> u64 {
55
return input;
66
}
7+
8+
#[test]
9+
fn should_return_input() {
10+
let expected = 1234;
11+
12+
let actual = main(1234);
13+
14+
assert(actual == expected);
15+
}

apps/docs/src/guide/creating-a-fuel-dapp/index.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,19 @@ You can find the complete source code of the dApp we built [here](https://github
196196

197197
Whenever you want to add a new feature to your dApp and quickly prototype things, you can follow the same steps we followed in this guide.
198198

199-
### 3. Extending the Test Suite (Optional)
199+
### 3. Extending the contract testing suite (Optional)
200+
201+
Testing our smart contract is a good practice to ensure that our implementation is working as expected. It also give assurances down the line if we decide to change the implementation of our contract.
202+
203+
We write our test in the `#[test]` macro within our Sway contract, these can be inline within our Sway contract or in a separate file.
204+
205+
For the guide, we'll add a test for our new `decrement_counter` function in the `./sway-programs/contract/src/main.sw` file:
206+
207+
<<< @/../../create-fuels-counter-guide/sway-programs/contract/src/main.sw#create-fuels-counter-guide-sway-contract-test{rust:line-numbers}
208+
209+
After writing our test, we can run either using `forc test` or via PNPM using `pnpm test:forc`.
210+
211+
### 4. Extending the integration test suite (Optional)
200212

201213
Testing the integration with your smart contract isn't essential, but it's good practice to ensure that your application is working as expected. It also gives you the ability to test your application in a controlled environment against a local node.
202214

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"test:browser:filter": "vitest --run --coverage false --config vitest.browser.config.mts",
3232
"test:e2e": "vitest --run --config vitest.node.config.mts $(scripts/tests-find.sh --e2e)",
3333
"test:integration": "vitest --run --config vitest.node.config.mts $(scripts/tests-find.sh --integration)",
34+
"test:forc": "turbo run test:forc",
3435
"test:network": "vitest --run --config vitest.node.config.mts $(scripts/tests-find.sh --network)",
3536
"lint": "run-s type:check-tests lint:check prettier:check type:check",
3637
"lint:check": "eslint . --ext .ts --max-warnings 0",

templates/nextjs/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"build": "pnpm run xprebuild && next build",
1010
"start": "next start",
1111
"lint": "next lint",
12-
"test": "vitest",
12+
"test": "run-s test:*",
13+
"test:forc": "forc test --path ./sway-programs",
14+
"test:e2e": "vitest",
1315
"test:ui": "sh ./test/ui/test-ui.sh"
1416
},
1517
"dependencies": {

templates/nextjs/sway-programs/contract/src/main.sw

+21
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,24 @@ impl Counter for Contract {
3030
storage.counter.read()
3131
}
3232
}
33+
34+
#[test]
35+
fn should_get_count() {
36+
let contract_instance = abi(Counter, CONTRACT_ID);
37+
let expected = 0;
38+
39+
let actual = contract_instance.get_count();
40+
41+
assert(actual == expected);
42+
}
43+
44+
#[test]
45+
fn should_increment_counter() {
46+
let contract_instance = abi(Counter, CONTRACT_ID);
47+
let count_before = contract_instance.get_count();
48+
let expected = count_before + 1;
49+
50+
let count_after = contract_instance.increment_counter(1);
51+
52+
assert(count_after == expected);
53+
}

templates/nextjs/sway-programs/predicate/src/main.sw

+18
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,21 @@ predicate;
66
fn main(password: u64) -> bool {
77
return password == 1337;
88
}
9+
10+
#[test]
11+
fn should_return_true_when_password_is_1337() {
12+
let expected = true;
13+
14+
let actual = main(1337);
15+
16+
assert(actual == expected);
17+
}
18+
19+
#[test]
20+
fn should_return_false_when_password_is_not_1337() {
21+
let expected = false;
22+
23+
let actual = main(1338);
24+
25+
assert(actual == expected);
26+
}

templates/nextjs/sway-programs/script/src/main.sw

+9
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@ script;
44
fn main(input: u64) -> u64 {
55
return input;
66
}
7+
8+
#[test]
9+
fn should_return_input() {
10+
let expected = 1234;
11+
12+
let actual = main(1234);
13+
14+
assert(actual == expected);
15+
}

templates/vite/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"build": "pnpm run xprebuild && tsc -b && vite build",
1010
"lint": "eslint .",
1111
"fuels:dev": "fuels dev",
12-
"test": "vitest",
12+
"test": "run-s test:*",
13+
"test:forc": "forc test --path ./sway-programs",
14+
"test:e2e": "vitest",
1315
"test:ui": "sh ./test/ui/test-ui.sh"
1416
},
1517
"dependencies": {

templates/vite/sway-programs/contract/src/main.sw

+21
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,24 @@ impl Counter for Contract {
3030
storage.counter.read()
3131
}
3232
}
33+
34+
#[test]
35+
fn should_get_count() {
36+
let contract_instance = abi(Counter, CONTRACT_ID);
37+
let expected = 0;
38+
39+
let actual = contract_instance.get_count();
40+
41+
assert(actual == expected);
42+
}
43+
44+
#[test]
45+
fn should_increment_counter() {
46+
let contract_instance = abi(Counter, CONTRACT_ID);
47+
let count_before = contract_instance.get_count();
48+
let expected = count_before + 1;
49+
50+
let count_after = contract_instance.increment_counter(1);
51+
52+
assert(count_after == expected);
53+
}

templates/vite/sway-programs/predicate/src/main.sw

+18
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,21 @@ predicate;
66
fn main(password: u64) -> bool {
77
return password == 1337;
88
}
9+
10+
#[test]
11+
fn should_return_true_when_password_is_1337() {
12+
let expected = true;
13+
14+
let actual = main(1337);
15+
16+
assert(actual == expected);
17+
}
18+
19+
#[test]
20+
fn should_return_false_when_password_is_not_1337() {
21+
let expected = false;
22+
23+
let actual = main(1338);
24+
25+
assert(actual == expected);
26+
}

templates/vite/sway-programs/script/src/main.sw

+9
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@ script;
44
fn main(input: u64) -> u64 {
55
return input;
66
}
7+
8+
#[test]
9+
fn should_return_input() {
10+
let expected = 1234;
11+
12+
let actual = main(1234);
13+
14+
assert(actual == expected);
15+
}

turbo.json

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
"test": {
3333
"dependsOn": ["^test", "pretest"],
3434
"outputLogs": "new-only"
35+
},
36+
"test:forc": {
37+
"dependsOn": ["^test:forc", "build"],
38+
"outputLogs": "new-only"
3539
}
3640
}
3741
}

0 commit comments

Comments
 (0)