From 15d0b9444df86d8d3789cb458698c0d6f1d4381b Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Tue, 14 Jan 2025 17:24:09 +0000 Subject: [PATCH 1/3] feat: added tests --- apps/create-fuels-counter-guide/package.json | 1 + .../sway-programs/contract/src/main.sw | 21 +++++++++++++++++++ .../sway-programs/predicate/src/main.sw | 18 ++++++++++++++++ .../sway-programs/script/src/main.sw | 9 ++++++++ package.json | 1 + templates/nextjs/package.json | 4 +++- .../nextjs/sway-programs/contract/src/main.sw | 21 +++++++++++++++++++ .../sway-programs/predicate/src/main.sw | 18 ++++++++++++++++ .../nextjs/sway-programs/script/src/main.sw | 9 ++++++++ templates/vite/package.json | 4 +++- .../vite/sway-programs/contract/src/main.sw | 21 +++++++++++++++++++ .../vite/sway-programs/predicate/src/main.sw | 18 ++++++++++++++++ .../vite/sway-programs/script/src/main.sw | 9 ++++++++ turbo.json | 4 ++++ 14 files changed, 156 insertions(+), 2 deletions(-) diff --git a/apps/create-fuels-counter-guide/package.json b/apps/create-fuels-counter-guide/package.json index 84862cf78d3..adfc0b40bd6 100644 --- a/apps/create-fuels-counter-guide/package.json +++ b/apps/create-fuels-counter-guide/package.json @@ -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", diff --git a/apps/create-fuels-counter-guide/sway-programs/contract/src/main.sw b/apps/create-fuels-counter-guide/sway-programs/contract/src/main.sw index 7b50cb4a5b7..0a166faf95a 100644 --- a/apps/create-fuels-counter-guide/sway-programs/contract/src/main.sw +++ b/apps/create-fuels-counter-guide/sway-programs/contract/src/main.sw @@ -44,3 +44,24 @@ 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); +} diff --git a/apps/create-fuels-counter-guide/sway-programs/predicate/src/main.sw b/apps/create-fuels-counter-guide/sway-programs/predicate/src/main.sw index a367decdac3..e1cc4da8975 100644 --- a/apps/create-fuels-counter-guide/sway-programs/predicate/src/main.sw +++ b/apps/create-fuels-counter-guide/sway-programs/predicate/src/main.sw @@ -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); +} diff --git a/apps/create-fuels-counter-guide/sway-programs/script/src/main.sw b/apps/create-fuels-counter-guide/sway-programs/script/src/main.sw index ac1eaf0716c..979d3f86330 100644 --- a/apps/create-fuels-counter-guide/sway-programs/script/src/main.sw +++ b/apps/create-fuels-counter-guide/sway-programs/script/src/main.sw @@ -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); +} diff --git a/package.json b/package.json index d9009eec20d..cd95d5cf779 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/templates/nextjs/package.json b/templates/nextjs/package.json index 77d5888c79b..0a88366a4dc 100644 --- a/templates/nextjs/package.json +++ b/templates/nextjs/package.json @@ -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": { diff --git a/templates/nextjs/sway-programs/contract/src/main.sw b/templates/nextjs/sway-programs/contract/src/main.sw index 07d57ccdb79..16cbf57e64e 100644 --- a/templates/nextjs/sway-programs/contract/src/main.sw +++ b/templates/nextjs/sway-programs/contract/src/main.sw @@ -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); +} diff --git a/templates/nextjs/sway-programs/predicate/src/main.sw b/templates/nextjs/sway-programs/predicate/src/main.sw index b690c17ea99..af44110cf2e 100644 --- a/templates/nextjs/sway-programs/predicate/src/main.sw +++ b/templates/nextjs/sway-programs/predicate/src/main.sw @@ -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); +} diff --git a/templates/nextjs/sway-programs/script/src/main.sw b/templates/nextjs/sway-programs/script/src/main.sw index ac1eaf0716c..979d3f86330 100644 --- a/templates/nextjs/sway-programs/script/src/main.sw +++ b/templates/nextjs/sway-programs/script/src/main.sw @@ -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); +} diff --git a/templates/vite/package.json b/templates/vite/package.json index 8fa7f0efca6..d0e9133aa77 100644 --- a/templates/vite/package.json +++ b/templates/vite/package.json @@ -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": { diff --git a/templates/vite/sway-programs/contract/src/main.sw b/templates/vite/sway-programs/contract/src/main.sw index 07d57ccdb79..16cbf57e64e 100644 --- a/templates/vite/sway-programs/contract/src/main.sw +++ b/templates/vite/sway-programs/contract/src/main.sw @@ -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); +} diff --git a/templates/vite/sway-programs/predicate/src/main.sw b/templates/vite/sway-programs/predicate/src/main.sw index b690c17ea99..af44110cf2e 100644 --- a/templates/vite/sway-programs/predicate/src/main.sw +++ b/templates/vite/sway-programs/predicate/src/main.sw @@ -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); +} diff --git a/templates/vite/sway-programs/script/src/main.sw b/templates/vite/sway-programs/script/src/main.sw index ac1eaf0716c..979d3f86330 100644 --- a/templates/vite/sway-programs/script/src/main.sw +++ b/templates/vite/sway-programs/script/src/main.sw @@ -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); +} diff --git a/turbo.json b/turbo.json index 0ec6a16078d..c732f27ac41 100644 --- a/turbo.json +++ b/turbo.json @@ -32,6 +32,10 @@ "test": { "dependsOn": ["^test", "pretest"], "outputLogs": "new-only" + }, + "test:forc": { + "dependsOn": ["^test:forc", "build"], + "outputLogs": "new-only" } } } From 5b06544c0c7f8e27c2cd4347c34e973ccc1d2573 Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Tue, 14 Jan 2025 17:25:46 +0000 Subject: [PATCH 2/3] chore: added test workflow --- .github/workflows/test.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index b4eebdd9f42..399348a82da 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -91,6 +91,22 @@ 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: Run Tests + run: pnpm test:forc + env: + FUEL_CORE_PATH: "fuels-core" + FORC_PATH: "fuels-forc" + e2e: runs-on: ubuntu-latest timeout-minutes: 25 From c1a4a1b434c0b72c912f0522a6c94153d719af8c Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Tue, 14 Jan 2025 17:59:48 +0000 Subject: [PATCH 3/3] chore: fix CI --- .github/workflows/test.yaml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 399348a82da..6b99494346a 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -101,11 +101,14 @@ jobs: - 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 - env: - FUEL_CORE_PATH: "fuels-core" - FORC_PATH: "fuels-forc" e2e: runs-on: ubuntu-latest