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 testing to create-fuels #2755

Merged
merged 20 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
f3e53fd
feat: add testing capability to create fuels
danielbate Jul 11, 2024
9eac6f4
chore: changeset
danielbate Jul 11, 2024
4e64129
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
danielbate Jul 11, 2024
d0cea13
chore: update doc
danielbate Jul 11, 2024
eec4be4
feat: add test command with config
danielbate Jul 12, 2024
96c9324
Merge branch 'db/feat/create-fuels-testing' of https://github.com/Fue…
danielbate Jul 12, 2024
16635c3
chore: add docs
danielbate Jul 12, 2024
da35aad
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
danielbate Jul 12, 2024
04dd9d5
feat: rewrite test files
danielbate Jul 12, 2024
050836c
chore: add docs for test extension
danielbate Jul 12, 2024
1296cbe
docs: docs for extending the test suite
danielbate Jul 12, 2024
e0cd96e
Merge branch 'master' into db/feat/create-fuels-testing
danielbate Jul 15, 2024
ebf1cd4
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
danielbate Jul 16, 2024
cdeaa88
feat: add tests to counter guide app
danielbate Jul 16, 2024
855a111
Revert "feat: add tests to counter guide app"
danielbate Jul 16, 2024
8c26251
Merge branch 'master' into db/feat/create-fuels-testing
danielbate Jul 18, 2024
f84de95
chore: lint
petertonysmith94 Jul 18, 2024
465872d
chore: added test groups
petertonysmith94 Jul 18, 2024
690a77e
chore: stabilise testing for create fuels templating (#2799)
petertonysmith94 Jul 19, 2024
25eff54
Merge branch 'master' into db/feat/create-fuels-testing
petertonysmith94 Jul 19, 2024
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/shaggy-houses-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-fuels": patch
---

feat: add testing to `create-fuels`
2 changes: 2 additions & 0 deletions apps/docs/src/guide/creating-a-fuel-dapp/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ Whenever you want to add a new feature to your dApp and quickly prototype things

- If you want to deploy your dApp to the testnet, check out our [Deploying a dApp to Testnet](./deploying-a-dapp-to-testnet.md) guide.

- If you want to further validate the functionality of your dApp and program types, check out the `test` directory in your `create fuels` project. Couple this with our [testing guide](https://docs.fuel.network/docs/fuels-ts/testing/) to get a better understanding of how to test your dApp.
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved

- If you have any questions or need help, feel free to reach out to us on the [Official Fuel Forum](https://forum.fuel.network/).

- If you want to learn more about the Fuel Stack, check out the [Fuel Docs](https://docs.fuel.network/).
85 changes: 84 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/tests-find.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

ROOT=$(cd "$(dirname "$0")/.."; pwd)

FILES=$(find $ROOT/{apps,packages,internal} -name '*.test.ts')
FILES=$(find $ROOT/{apps,packages,internal,templates} -name '*.test.ts')

if [[ $* == *--all* ]]; then
grep -lE "@group\s+(node|browser|e2e)" $FILES
Expand Down
3 changes: 2 additions & 1 deletion templates/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"eslint-config-next": "14.2.4",
"postcss": "^8",
"tailwindcss": "^3.4.4",
"typescript": "^5"
"typescript": "^5",
"vitest": "^1.6.0"
danielbate marked this conversation as resolved.
Show resolved Hide resolved
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
}
}
59 changes: 59 additions & 0 deletions templates/nextjs/test/contract.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { launchTestNode } from 'fuels/test-utils';

import { describe, test, expect } from 'vitest';

/**
* Imports for the contract factory and bytecode, so that we can use them in the test.
*
* Can't find these imports? Make sure you've run `fuels build` to generate these with typegen.
*/
import { TestContractAbi__factory } from '../src/sway-api';
import bytecode from '../src/sway-api/contracts/TestContractAbi.hex';

/**
* @group node
nedsalk marked this conversation as resolved.
Show resolved Hide resolved
*
* Tests for the contract program type within the TS SDK. Here we will test the deployment of
* our contract, and the result of call it's functions.
*/
describe('Contract', () => {
test('Deploy and Call', async () => {
// First, we'll launch a test node, passing the contract factory and bytecode. This will deploy the contract
// to our test node so we can test against it.
using launched = await launchTestNode({
danielbate marked this conversation as resolved.
Show resolved Hide resolved
contractsConfigs: [
{
deployer: TestContractAbi__factory,
bytecode,
},
],
});

// We can now destructure the contract, provider, and wallets from the launched object.
danielbate marked this conversation as resolved.
Show resolved Hide resolved
const {
contracts: [contract],
} = launched;

// Lets setup some values to use in the test.
const initialCount = 0;
const incrementedCount = 5;

// We can now call the contract functions and test the results. Lets assert the initial value of the counter.
const { waitForResult: initWaitForResult } = await contract.functions.get_count().call();
const { value: initValue } = await initWaitForResult();
expect(initValue.toNumber()).toBe(initialCount);

// Next, we'll increment the counter by 5 and assert the new value.
const { waitForResult: incrementWaitForResult } = await contract.functions
.increment_counter(incrementedCount)
.call();
const { value: incrementValue } = await incrementWaitForResult();
expect(incrementValue.toNumber()).toBe(incrementedCount);

// Finally, we'll test the get count function again to ensure parity.
const { waitForResult: finalWaitForResult } = await contract.functions.get_count().call();
const { value: finalValue } = await finalWaitForResult();
expect(finalValue.toNumber()).toBe(incrementedCount);
expect(initValue.toNumber()).toBeLessThan(finalValue.toNumber());
});
});
63 changes: 63 additions & 0 deletions templates/nextjs/test/predicate.test.ts
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { launchTestNode } from 'fuels/test-utils';

import { describe, test, expect } from 'vitest';

/**
* Import for the predicate factory and input data type, so that we can use them in the test.
*
* Can't find these imports? Make sure you've run `fuels build` to generate these with typegen.
*/
import { TestPredicateAbi__factory, TestPredicateAbiInputs } from '../src/sway-api';

/**
* @group node
*
* Tests for the predicate program type within the TS SDK. Here we will test the use of our predicate
* in a transaction.
*/
describe('Predicate', () => {
test('Transaction', async () => {
// First, we'll launch a test node to use for our predicate transaction.
using launched = await launchTestNode();

// We can now destructure the provider and the sender and receiver wallet from the launched object.
const {
wallets: [sender, receiver],
provider,
} = launched;

// For a predicate, we need to pass in an argument to evaluate the predicate.
const predicateData: TestPredicateAbiInputs = [1337];

// Now, we can instantiate our predicate.
const predicate = TestPredicateAbi__factory.createInstance(provider, predicateData);

// Lets also setup some transfer values to assert against.
const amountToPredicate = 250_000;
const amountToReceiver = 50_000;

// Lets also get the initial balance of the receiver wallet to assert against.
const initialReceiverBalance = await receiver.getBalance();

// We can now transfer some assets to the predicate.
const setupTx = await sender.transfer(predicate.address, amountToPredicate);
await setupTx.waitForResult();

// And we'll assert it's value to confirm success of the transfer.
const predicateBalance = await predicate.getBalance();
expect(predicateBalance.toNumber()).toBe(amountToPredicate);

// Now we can transfer assets from the predicate to the receiver.
const tx = await predicate.transfer(receiver.address, amountToReceiver);
const { isStatusSuccess } = await tx.waitForResult();
expect(isStatusSuccess).toBe(true);

// Then get our receivers final balance.
const finalReceiverBalance = await receiver.getBalance();

// And lastly we'll assert all the values to confirm the success of the predicate transfer.
expect(finalReceiverBalance.toNumber()).toBe(
initialReceiverBalance.add(amountToReceiver).toNumber()
);
});
});
39 changes: 39 additions & 0 deletions templates/nextjs/test/script.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { launchTestNode } from 'fuels/test-utils';

import { describe, test, expect } from 'vitest';

/**
* Import for the script factory, so that we can use them in the test.
*
* Can't find these imports? Make sure you've run `fuels build` to generate these with typegen.
*/
import { TestScriptAbi__factory } from '../src/sway-api';

/**
* @group node
*
* Tests for the script program type within the TS SDK. Here we will test the use of our script
* function call.
*/
describe('Script', () => {
test('Call', async () => {
// First, we'll launch a test node to use for our script transaction.
using launched = await launchTestNode();

// We can now destructure the sender wallet from the launched object.
const {
wallets: [sender],
} = launched;

// Now, we can instantiate our script.
const script = TestScriptAbi__factory.createInstance(sender);

// Lets also setup a value to use in the test.
const expectedValue = 1337;

// We can now call the script function and assert the result.
const { waitForResult } = await script.functions.main(expectedValue).call();
const { value } = await waitForResult();
expect(value.toNumber()).toBe(expectedValue);
});
});
Loading