Skip to content

Commit 960a35f

Browse files
authored
Migrate JS tests to a Kit client and LiteSVM (#74)
1 parent 595d250 commit 960a35f

7 files changed

Lines changed: 200 additions & 70 deletions

File tree

Makefile

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,17 @@ build-doc-%:
7777
test-doc-%:
7878
cargo $(nightly) test --doc --all-features --manifest-path $(call make-path,$*)/Cargo.toml $(ARGS)
7979

80-
test-%:
81-
SBF_OUT_DIR=$(PWD)/target/deploy cargo $(nightly) test --manifest-path $(call make-path,$*)/Cargo.toml $(ARGS)
82-
8380
format-check-js-%:
8481
cd $(call make-path,$*) && pnpm install && pnpm format $(ARGS)
8582

8683
lint-js-%:
8784
cd $(call make-path,$*) && pnpm install && pnpm lint $(ARGS)
8885

8986
test-js-%:
90-
make restart-test-validator
9187
cd $(call make-path,$*) && pnpm install && pnpm build && pnpm test $(ARGS)
92-
make stop-test-validator
9388

94-
restart-test-validator:
95-
./scripts/restart-test-validator.sh
96-
97-
stop-test-validator:
98-
pkill -f solana-test-validator
89+
test-%:
90+
SBF_OUT_DIR=$(PWD)/target/deploy cargo $(nightly) test --manifest-path $(call make-path,$*)/Cargo.toml $(ARGS)
9991

10092
generate-idl-%:
10193
pnpm generate:idl $(call make-path,$*) $(ARGS)

clients/js/README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,19 @@ A generated JavaScript library for the Feature Gate program.
44

55
## Getting started
66

7-
To build and test your JavaScript client from the root of the repository, you may use the following command.
7+
The JS client tests use [LiteSVM](https://github.com/LiteSVM/litesvm) in-process, so no local validator is needed. To build and test your JavaScript client from the root of the repository, you may use the following command.
88

99
```sh
10-
pnpm clients:js:test
10+
make test-js-clients-js
1111
```
1212

13-
This will start a new local validator, if one is not already running, and run the tests for your JavaScript client.
13+
This installs dependencies, builds the client, and runs the test suite.
1414

1515
## Available client scripts.
1616

1717
Alternatively, you can go into the client directory and run the tests directly.
1818

1919
```sh
20-
# Build your programs and start the validator.
21-
pnpm programs:build
22-
pnpm validator:restart
23-
24-
# Go into the client directory and run the tests.
2520
cd clients/js
2621
pnpm install
2722
pnpm build

clients/js/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
"devDependencies": {
4141
"@solana/eslint-config-solana": "^3.0.3",
4242
"@solana/kit": "^6.4.0",
43+
"@solana/kit-plugin-litesvm": "^0.10.0",
44+
"@solana/kit-plugin-signer": "^0.10.0",
4345
"@solana/webcrypto-ed25519-polyfill": "3.0.3",
4446
"@types/node": "^24",
4547
"@typescript-eslint/eslint-plugin": "^7.16.1",

clients/js/pnpm-lock.yaml

Lines changed: 141 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/js/test/_setup.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import path from 'node:path';
2+
3+
import { createClient, lamports } from '@solana/kit';
4+
import { litesvm } from '@solana/kit-plugin-litesvm';
5+
import { airdropSigner, generatedSigner } from '@solana/kit-plugin-signer';
6+
7+
import {
8+
SOLANA_FEATURE_GATE_PROGRAM_ADDRESS,
9+
solanaFeatureGateProgram,
10+
} from '../src';
11+
12+
const SOLANA_FEATURE_GATE_BINARY_PATH = path.resolve(
13+
__dirname,
14+
'..',
15+
'..',
16+
'..',
17+
'target',
18+
'deploy',
19+
'solana_feature_gate_program.so'
20+
);
21+
22+
export const createTestClient = () => {
23+
return createClient()
24+
.use(generatedSigner())
25+
.use(litesvm())
26+
.use(airdropSigner(lamports(1_000_000_000n)))
27+
.use((client) => {
28+
// Load the feature-gate program into the LiteSVM instance from its
29+
// compiled `.so` file. This must run after the `litesvm()` plugin so
30+
// that `client.svm` is available.
31+
client.svm.addProgramFromFile(
32+
SOLANA_FEATURE_GATE_PROGRAM_ADDRESS,
33+
SOLANA_FEATURE_GATE_BINARY_PATH
34+
);
35+
return client;
36+
})
37+
.use(solanaFeatureGateProgram());
38+
};

clients/js/test/example.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { expect, it } from 'vitest';
22

3-
it('runs an example test', () => {
4-
expect(true).toBe(true);
3+
import { createTestClient } from './_setup';
4+
5+
it('sets up a LiteSVM client with the feature-gate program', async () => {
6+
// Given a test client whose payer is funded with SOL.
7+
const client = await createTestClient();
8+
9+
// Then the client exposes the feature-gate program plugin.
10+
expect(client.solanaFeatureGate).toBeDefined();
11+
12+
// And the payer was funded via LiteSVM.
13+
const { value: balance } = await client.rpc
14+
.getBalance(client.payer.address)
15+
.send();
16+
expect(balance).toBe(1_000_000_000n);
517
});

scripts/restart-test-validator.sh

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)