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

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
15 changes: 15 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ jobs:
# uncomment the below line to test against devnet
# DEVNET_WALLET_PVT_KEY: ${{ secrets.DEVNET_WALLET_PVT_KEY }}

templates:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

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

- name: Pretest
run: pnpm pretest

- name: Run Template Tests
run: pnpm test:templates

test:
if: github.base_ref == 'master' || github.ref_name == 'master'
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions apps/create-fuels-counter-guide/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"version": "0.1.0",
"type": "module",
"scripts": {
"test": "run-p test:feat",
"test:all": "run-p test:feat test:unit test:ui",
"test:feat": "vitest",
"test:unit": "cd sway-programs && forc test",
"test:ui": "./test/ui/test-ui.sh",
"original:dev": "vite",
"original:build": "tsc -b && vite build",
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
7 changes: 6 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,15 @@ 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}
The template also comes with a UI testing setup using [Playwright](https://playwright.dev/). We can add a test for our new `decrement_counter` function in the `./test/ui/ui.test.ts` file:

<<< @/../../create-fuels-counter-guide/test/ui/ui.test.ts#decrement-counter-ui-test{ts:line-numbers}
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:templates": "./scripts/tests-template.sh",
"lint": "run-s type:check-tests lint:check prettier:check",
"lint:check": "eslint . --ext .ts --max-warnings 0",
"lint:fix": "pnpm lint:check --fix",
Expand Down
6 changes: 6 additions & 0 deletions scripts/tests-template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

ROOT_DIR=$(pwd)
cd $ROOT_DIR/templates/vite/sway-programs && ls && pnpm fuels-forc test
cd $ROOT_DIR/templates/nextjs/sway-programs && pnpm forc test
cd $ROOT_DIR/apps/create-fuels-counter-guide/sway-programs && pnpm forc test
5 changes: 4 additions & 1 deletion templates/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"build": "pnpm run xprebuild && next build",
"start": "next start",
"lint": "next lint",
"test": "vitest",
"test": "run-p test:feat",
"test:all": "run-p test:feat test:unit test:ui",
"test:feat": "vitest",
"test:unit": "cd sway-programs && forc test",
"test:ui": "./test/ui/test-ui.sh"
},
"dependencies": {
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);
}
5 changes: 4 additions & 1 deletion templates/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"build": "pnpm run xprebuild && tsc -b && vite build",
"lint": "eslint .",
"fuels:dev": "fuels dev",
"test": "vitest",
"test": "run-p test:feat",
"test:all": "run-p test:feat test:unit test:ui",
"test:feat": "vitest",
"test:unit": "cd sway-programs && forc test",
"test:ui": "./test/ui/test-ui.sh"
},
"dependencies": {
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