Skip to content

Commit

Permalink
fix: sandbox tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gagdiez committed Mar 14, 2024
1 parent c1c1e90 commit be01ac8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion contract-simple-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"build": "near-sdk-js build src/contract.ts build/cross_contract.wasm",
"test": "$npm_execpath build && ava --concurrency=1 -- ./build/cross_contract.wasm"
"test": "ava -- ./build/cross_contract.wasm"
},
"dependencies": {
"near-cli": "^4.0.10",
Expand Down
32 changes: 14 additions & 18 deletions contract-simple-ts/sandbox-ts/main.ava.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ import { Worker, NearAccount } from 'near-workspaces';
import anyTest, { TestFn } from 'ava';
import { setDefaultResultOrder } from 'dns'; setDefaultResultOrder('ipv4first'); // temp fix for node >v17

// Global context
let worker: Worker;
let accounts: Record<string, NearAccount>;
// Global worker
const test = anyTest as TestFn<{ worker: Worker, accounts: Record<string, NearAccount> }>;

const test = anyTest as TestFn<{}>;

test.before(async (t) => {
// Init the worker and start a Sandbox server
worker = await Worker.init();

// Prepare sandbox for tests, create accounts, deploy contracts, etx.
const root = worker.rootAccount;
test.beforeEach(async (t) => {
// Create sandbox, accounts, deploy contracts, etc.
const worker = t.context.worker = await Worker.init()

const root = worker.rootAccount;

// Create test accounts
const alice = await root.createSubAccount("alice");
const xcc = await root.createSubAccount("xcc");
Expand All @@ -25,36 +21,36 @@ test.before(async (t) => {

// Deploy the xcc contract
await xcc.deploy(process.argv[2]);
await xcc.call(xcc, "init", {hello_account: helloNear.accountId})
await xcc.call(xcc, "init", { hello_account: helloNear.accountId })

// Save state for test runs, it is unique for each test
accounts = { root, alice, xcc, helloNear };
t.context.accounts = { root, alice, xcc, helloNear };
});

test.after.always(async (t) => {
test.afterEach.always(async (t) => {
// Stop Sandbox server
await worker.tearDown().catch((error) => {
await t.context.worker.tearDown().catch((error) => {
console.log('Failed to stop the Sandbox:', error);
});
});

test("returns the default greeting", async (t) => {
const { xcc, alice } = accounts;
const { xcc, alice } = t.context.accounts;

const greeting = await alice.call(xcc, "query_greeting", {}, { gas: "200000000000000" });
console.log('greeting: ', greeting);
t.is(greeting, 'Hello');
});

test("change the greeting", async (t) => {
const { xcc, alice } = accounts;
const { xcc, alice } = t.context.accounts;

const howdyChangingResult = await alice.call(xcc, "change_greeting", { new_greeting: "Howdy" }, { gas: "200000000000000" });
t.is(howdyChangingResult, true);

const howdyResult = await alice.call(xcc, "query_greeting", {}, { gas: "200000000000000" });
t.is(howdyResult, 'Howdy');

// const helloChangingResult = await alice.call(xcc, "change_greeting", { new_greeting: "Hello" }, { gas: "200000000000000" });
// t.is(helloChangingResult, true);

Expand Down

0 comments on commit be01ac8

Please sign in to comment.