/
main.ava.ts
43 lines (35 loc) 路 1.41 KB
/
main.ava.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Worker, NearAccount } from 'near-workspaces';
import anyTest, { TestFn } from 'ava';
import { setDefaultResultOrder } from 'dns'; setDefaultResultOrder('ipv4first'); // temp fix for node >v17
// Global context
const test = anyTest as TestFn<{ worker: Worker, accounts: Record<string, NearAccount> }>;
test.beforeEach(async (t) => {
// Create sandbox, accounts, deploy contracts, etc.
const worker = t.context.worker = await Worker.init();
// Deploy contract
const root = worker.rootAccount;
const contract = await root.createSubAccount('test-account');
// Get wasm file path from package.json test script in folder above
await contract.deploy(
process.argv[2],
);
// Save state for test runs, it is unique for each test
t.context.accounts = { root, contract };
});
test.afterEach.always(async (t) => {
// Stop Sandbox server
await t.context.worker.tearDown().catch((error) => {
console.log('Failed to stop the Sandbox:', error);
});
});
test('returns the default greeting', async (t) => {
const { contract } = t.context.accounts;
const greeting: string = await contract.view('get_greeting', {});
t.is(greeting, 'Hello');
});
test('changes the greeting', async (t) => {
const { root, contract } = t.context.accounts;
await root.call(contract, 'set_greeting', { greeting: 'Howdy' });
const greeting: string = await contract.view('get_greeting', {});
t.is(greeting, 'Howdy');
});