Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sway tests to create-fuels #3100

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft
5 changes: 5 additions & 0 deletions .changeset/green-cars-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-fuels": patch
---

feat: add sway tests to `create-fuels`
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": "vitest",
"test:unit": "cd sway-programs && forc test",
"original:dev": "vite",
"original:build": "tsc -b && vite build",
"original:start": "next start",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,16 @@ impl Counter for Contract {
}
}
// #endregion create-fuels-counter-guide-impl


// #region create-fuels-counter-guide-sway-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-test
8 changes: 7 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 @@ -195,10 +195,16 @@ Whenever you want to add a new feature to your dApp and quickly prototype things

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.

We've provided some examples for each program type in the `./test` directory of your project. But let's also add a test for our new `decrement_counter` function in the `./test/contract.test.ts` file:
We've provided some examples for each program type in the `./test` directory of your project, and in the `.sw` source files as well.

But let's also add a test for our new `decrement_counter` function in the `./test/contract.test.ts` file:

<<< @/../../docs-snippets/src/guide/create-fuels/decrement_counter.test.ts#decrement-counter{ts:line-numbers}

And a test for the decrement 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-test{rust:line-numbers}

## Next Steps

- Now that you have a basic counter dApp running and have the `npm create fuels` workflow powering you, you can start building more complex dApps using the Fuel Stack. A good place to start for ideas and reference code is the [Sway Applications Repo](https://github.com/FuelLabs/sway-applications).
Expand Down
3 changes: 2 additions & 1 deletion templates/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build": "pnpm run xprebuild && next build",
"start": "next start",
"lint": "next lint",
"test": "vitest"
"test": "vitest",
"test:unit": "cd sway-programs && forc test"
},
"dependencies": {
"@fuels/connectors": "^0.27.1",
Expand Down
15 changes: 15 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,18 @@ impl Counter for Contract {
storage.counter.read()
}
}

#[test]
fn test_get_count() {
let contract_instance = abi(Counter, CONTRACT_ID);
let count = contract_instance.get_count();
assert(count == 0);
}

#[test]
fn test_increment_counter() {
let contract_instance = abi(Counter, CONTRACT_ID);
let count_before = contract_instance.get_count();
let count_after = contract_instance.increment_counter(1);
assert(count_after == count_before + 1);
}
9 changes: 9 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,12 @@ predicate;
fn main(password: u64) -> bool {
return password == 1337;
}

#[test]
fn test_main() {
let result1 = main(1337);
assert(result1 == true);

let result2 = main(1338);
assert(result2 == false);
}
6 changes: 6 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,9 @@ script;
fn main(input: u64) -> u64 {
return input;
}

#[test]
fn test_main() {
let result = main(1337);
assert(result == 1337);
}
3 changes: 2 additions & 1 deletion templates/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build": "pnpm run xprebuild && tsc -b && vite build",
"lint": "eslint .",
"fuels:dev": "fuels dev",
"test": "vitest"
"test": "vitest",
"test:unit": "cd sway-programs && forc test"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably run these tests in the CI to ensure they pass. Same for all packages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried adding them to CI but this would require us to install forc in CI. One thing we could do is use our internal binaries to run these tests only for CI instead of forc. Mind if I tackle this in a separate issue?

},
"dependencies": {
"@fuels/connectors": "^0.27.1",
Expand Down
15 changes: 15 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,18 @@ impl Counter for Contract {
storage.counter.read()
}
}

#[test]
fn test_get_count() {
let contract_instance = abi(Counter, CONTRACT_ID);
let count = contract_instance.get_count();
assert(count == 0);
}

#[test]
fn test_increment_counter() {
let contract_instance = abi(Counter, CONTRACT_ID);
let count_before = contract_instance.get_count();
let count_after = contract_instance.increment_counter(1);
assert(count_after == count_before + 1);
}
9 changes: 9 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,12 @@ predicate;
fn main(password: u64) -> bool {
return password == 1337;
}

#[test]
fn test_main() {
let result1 = main(1337);
assert(result1 == true);

let result2 = main(1338);
assert(result2 == false);
}
6 changes: 6 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,9 @@ script;
fn main(input: u64) -> u64 {
return input;
}

#[test]
fn test_main() {
let result = main(1337);
assert(result == 1337);
}
Loading