Skip to content

Commit

Permalink
feat: add forc tests to create fuels (#3585)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
petertonysmith94 and Torres-ssf authored Jan 29, 2025
1 parent 2ec0dac commit 13064a7
Show file tree
Hide file tree
Showing 18 changed files with 206 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-deers-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-fuels": patch
---

feat: add `forc` tests to create fuels
19 changes: 19 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ jobs:
env:
PUBLISHED_NPM_TAG: next

forc:
runs-on: ubuntu-latest
timeout-minutes: 25
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Test Setup
uses: ./.github/actions/test-setup

- name: Setup forc and fuel-core paths
shell: bash
run: |
echo "$PWD/internal/forc/forc-binaries" >> $GITHUB_PATH
echo "$PWD/internal/fuel-core/fuel-core-binaries" >> $GITHUB_PATH
- name: Run Tests
run: pnpm test:forc

e2e:
runs-on: ubuntu-latest
timeout-minutes: 25
Expand Down
1 change: 1 addition & 0 deletions .knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"apps/docs/src/guide/types/snippets/numbers/for-u8-u16-and-u32-2.ts"
],
"ignore": [".github/**"],
"ignoreBinaries": ["forc"],
"ignoreDependencies": [
"fuels",
"bun",
Expand Down
1 change: 1 addition & 0 deletions apps/create-fuels-counter-guide/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "module",
"scripts": {
"test:ui": "sh ./test/ui/test-ui.sh",
"test:forc": "forc test --path ./sway-programs",
"original:dev": "vite",
"original:build": "tsc -b && vite build",
"original:start": "vite start",
Expand Down
33 changes: 33 additions & 0 deletions apps/create-fuels-counter-guide/sway-programs/contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,36 @@ impl Counter for Contract {
}
}
// #endregion create-fuels-counter-guide-impl

#[test]
fn should_get_count() {
let contract_instance = abi(Counter, CONTRACT_ID);
let expected = 0;

let actual = contract_instance.get_count();

assert(actual == expected);
}

#[test]
fn should_increment_counter() {
let contract_instance = abi(Counter, CONTRACT_ID);
let count_before = contract_instance.get_count();
let expected = count_before + 1;

let count_after = contract_instance.increment_counter(1);

assert(count_after == expected);
}

// #region create-fuels-counter-guide-sway-contract-test
#[test]
fn test_decrement_counter() {
let contract_instance = abi(Counter, CONTRACT_ID);
let _ = contract_instance.increment_counter(5);

let count_before = contract_instance.get_count();
let count_after = contract_instance.decrement_counter(1);
assert(count_after == count_before - 1);
}
// #endregion create-fuels-counter-guide-sway-contract-test
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,21 @@ configurable {
fn main(pin: u64) -> bool {
return PIN == pin;
}

#[test]
fn should_return_true_when_password_is_1337() {
let expected = true;

let actual = main(1337);

assert(actual == expected);
}

#[test]
fn should_return_false_when_password_is_not_1337() {
let expected = false;

let actual = main(1338);

assert(actual == expected);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ script;
fn main(input: u64) -> u64 {
return input;
}

#[test]
fn should_return_input() {
let expected = 1234;

let actual = main(1234);

assert(actual == expected);
}
14 changes: 13 additions & 1 deletion apps/docs/src/guide/creating-a-fuel-dapp/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,19 @@ You can find the complete source code of the dApp we built [here](https://github

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.

### 3. Extending the Test Suite (Optional)
### 3. Extending the contract testing suite (Optional)

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.

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.

For the guide, we'll add a test for our new `decrement_counter` function in the `./sway-programs/contract/src/main.sw` file:

<<< @/../../create-fuels-counter-guide/sway-programs/contract/src/main.sw#create-fuels-counter-guide-sway-contract-test{rust:line-numbers}

After writing our test, we can run either using `forc test` or via PNPM using `pnpm test:forc`.

### 4. Extending the integration test suite (Optional)

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.

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"test:browser:filter": "vitest --run --coverage false --config vitest.browser.config.mts",
"test:e2e": "vitest --run --config vitest.node.config.mts $(scripts/tests-find.sh --e2e)",
"test:integration": "vitest --run --config vitest.node.config.mts $(scripts/tests-find.sh --integration)",
"test:forc": "turbo run test:forc",
"test:network": "vitest --run --config vitest.node.config.mts $(scripts/tests-find.sh --network)",
"lint": "run-s type:check-tests lint:check prettier:check type:check",
"lint:check": "eslint . --ext .ts --max-warnings 0",
Expand Down
4 changes: 3 additions & 1 deletion templates/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"build": "pnpm run xprebuild && next build",
"start": "next start",
"lint": "next lint",
"test": "vitest",
"test": "run-s test:*",
"test:forc": "forc test --path ./sway-programs",
"test:e2e": "vitest",
"test:ui": "sh ./test/ui/test-ui.sh"
},
"dependencies": {
Expand Down
21 changes: 21 additions & 0 deletions templates/nextjs/sway-programs/contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,24 @@ impl Counter for Contract {
storage.counter.read()
}
}

#[test]
fn should_get_count() {
let contract_instance = abi(Counter, CONTRACT_ID);
let expected = 0;

let actual = contract_instance.get_count();

assert(actual == expected);
}

#[test]
fn should_increment_counter() {
let contract_instance = abi(Counter, CONTRACT_ID);
let count_before = contract_instance.get_count();
let expected = count_before + 1;

let count_after = contract_instance.increment_counter(1);

assert(count_after == expected);
}
18 changes: 18 additions & 0 deletions templates/nextjs/sway-programs/predicate/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@ predicate;
fn main(password: u64) -> bool {
return password == 1337;
}

#[test]
fn should_return_true_when_password_is_1337() {
let expected = true;

let actual = main(1337);

assert(actual == expected);
}

#[test]
fn should_return_false_when_password_is_not_1337() {
let expected = false;

let actual = main(1338);

assert(actual == expected);
}
9 changes: 9 additions & 0 deletions templates/nextjs/sway-programs/script/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ script;
fn main(input: u64) -> u64 {
return input;
}

#[test]
fn should_return_input() {
let expected = 1234;

let actual = main(1234);

assert(actual == expected);
}
4 changes: 3 additions & 1 deletion templates/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"build": "pnpm run xprebuild && tsc -b && vite build",
"lint": "eslint .",
"fuels:dev": "fuels dev",
"test": "vitest",
"test": "run-s test:*",
"test:forc": "forc test --path ./sway-programs",
"test:e2e": "vitest",
"test:ui": "sh ./test/ui/test-ui.sh"
},
"dependencies": {
Expand Down
21 changes: 21 additions & 0 deletions templates/vite/sway-programs/contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,24 @@ impl Counter for Contract {
storage.counter.read()
}
}

#[test]
fn should_get_count() {
let contract_instance = abi(Counter, CONTRACT_ID);
let expected = 0;

let actual = contract_instance.get_count();

assert(actual == expected);
}

#[test]
fn should_increment_counter() {
let contract_instance = abi(Counter, CONTRACT_ID);
let count_before = contract_instance.get_count();
let expected = count_before + 1;

let count_after = contract_instance.increment_counter(1);

assert(count_after == expected);
}
18 changes: 18 additions & 0 deletions templates/vite/sway-programs/predicate/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@ predicate;
fn main(password: u64) -> bool {
return password == 1337;
}

#[test]
fn should_return_true_when_password_is_1337() {
let expected = true;

let actual = main(1337);

assert(actual == expected);
}

#[test]
fn should_return_false_when_password_is_not_1337() {
let expected = false;

let actual = main(1338);

assert(actual == expected);
}
9 changes: 9 additions & 0 deletions templates/vite/sway-programs/script/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ script;
fn main(input: u64) -> u64 {
return input;
}

#[test]
fn should_return_input() {
let expected = 1234;

let actual = main(1234);

assert(actual == expected);
}
4 changes: 4 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"test": {
"dependsOn": ["^test", "pretest"],
"outputLogs": "new-only"
},
"test:forc": {
"dependsOn": ["^test:forc", "build"],
"outputLogs": "new-only"
}
}
}

0 comments on commit 13064a7

Please sign in to comment.