From 7b6993a018a2352cb33c0a9cb6e860861d188d5d Mon Sep 17 00:00:00 2001 From: garikbesson Date: Thu, 14 Mar 2024 20:50:20 +0100 Subject: [PATCH 1/2] Creating a separate worker for every single test case --- contract-ts/package.json | 8 ++++---- contract-ts/sandbox-ts/main.ava.ts | 23 ++++++++++------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/contract-ts/package.json b/contract-ts/package.json index 4387d44..c507419 100644 --- a/contract-ts/package.json +++ b/contract-ts/package.json @@ -5,19 +5,19 @@ "type": "module", "scripts": { "build": "near-sdk-js build src/contract.ts build/contract.wasm", - "test": "$npm_execpath build && ava -- ./build/contract.wasm" + "test": "ava -- ./build/contract.wasm" }, "dependencies": { - "near-cli": "^4.0.8", + "near-cli": "^4.0.10", "near-sdk-js": "1.0.0" }, "devDependencies": { "@ava/typescript": "^4.1.0", "ava": "^6.1.2", "near-workspaces": "^3.5.0", - "ts-morph": "^21.0.1", + "ts-morph": "^22.0.0", "ts-node": "^10.9.2", "tsimp": "^2.0.11", - "typescript": "^5.3.3" + "typescript": "^5.4.2" } } \ No newline at end of file diff --git a/contract-ts/sandbox-ts/main.ava.ts b/contract-ts/sandbox-ts/main.ava.ts index 83b2e65..10cd3c7 100644 --- a/contract-ts/sandbox-ts/main.ava.ts +++ b/contract-ts/sandbox-ts/main.ava.ts @@ -3,14 +3,11 @@ import anyTest, { TestFn } from 'ava'; import { setDefaultResultOrder } from 'dns'; setDefaultResultOrder('ipv4first'); // temp fix for node >v17 // Global context -let worker: Worker; -let accounts: Record; +const test = anyTest as TestFn<{ worker: Worker, accounts: Record }>; -const test = anyTest as TestFn<{}>; - -test.before(async (t) => { - // Init the worker and start a Sandbox server - worker = await Worker.init(); +test.beforeEach(async (t) => { + // Create sandbox, accounts, deploy contracts, etc. + const worker = t.context.worker = await Worker.init(); // deploy contract const root = worker.rootAccount; @@ -27,18 +24,18 @@ test.before(async (t) => { await contract.deploy(process.argv[2]); // Save state for test runs, it is unique for each test - accounts = { root, contract, alice }; + t.context.accounts = { root, contract, alice }; }); -test.after.always(async (t) => { +test.afterEach.always(async (t) => { // Stop Sandbox server - await worker.tearDown().catch((error) => { - console.log("Failed to stop the Sandbox:", error); + await t.context.worker.tearDown().catch((error) => { + console.log('Failed to stop the Sandbox:', error); }); }); test("send one message and retrieve it", async (t) => { - const { root, contract } = accounts; + const { root, contract } = t.context.accounts; await root.call(contract, "add_message", { text: "aloha" }); const msgs = await contract.view("get_messages"); const expectedMessagesResult = [ @@ -48,7 +45,7 @@ test("send one message and retrieve it", async (t) => { }); test("send two messages and expect two total", async (t) => { - const { root, contract, alice } = accounts; + const { root, contract, alice } = t.context.accounts; await root.call(contract, "add_message", { text: "aloha" }); await alice.call(contract, "add_message", { text: "hola" }, { attachedDeposit: NEAR.parse('1') }); From b419167be2fa7bea27214229d7f09a8313fe1867 Mon Sep 17 00:00:00 2001 From: gagdiez Date: Mon, 18 Mar 2024 17:10:23 +0100 Subject: [PATCH 2/2] Update contract-ts/package.json --- contract-ts/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contract-ts/package.json b/contract-ts/package.json index c507419..a4b723c 100644 --- a/contract-ts/package.json +++ b/contract-ts/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "build": "near-sdk-js build src/contract.ts build/contract.wasm", - "test": "ava -- ./build/contract.wasm" + "test": "$npm_execpath build && ava -- ./build/contract.wasm" }, "dependencies": { "near-cli": "^4.0.10",