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

Re Organize TS example #4

Merged
merged 6 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 2 additions & 5 deletions .github/workflows/tests-rs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ jobs:
platform: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16"
- uses: actions/checkout@v4
- name: Install and test modules
run: |
cd ./contract-rs
./test.sh
cargo test
7 changes: 4 additions & 3 deletions .github/workflows/tests-ts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ jobs:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest]
node-version: [18, 20]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "16"
node-version: ${{ matrix.node-version }}
- name: Install and test modules
run: |
cd ./contract-ts
Expand Down
26 changes: 9 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
# Rust
./contract-rs/target/
./contract-rs/Cargo.lock

# TypeScript - Sandbox
./contract-ts/sandbox-ts/package-lock.json
./contract-ts/sandbox-ts/node_modules/
**/target
**/Cargo.lock

# TypeScript
./contract-ts/build/
./contract-ts/node_modules/
./contract-ts/sandbox-ts/node_modules/
./contract-ts/sandbox-ts/yarn.lock
./contract-ts/yarn.lock
./contract-ts/package-lock.json
**/package-lock.json
**/node_modules/
**/build/
**/yarn.lock
**/.tsimp

# Frontend
./frontend/dist/
./frontend/node_modules/
./frontend/.parcel-cache
./frontend/yarn.lock
./frontend/package-lock.json
**/dist/
**/.parcel-cache
15 changes: 15 additions & 0 deletions contract-ts/ava.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require('util').inspect.defaultOptions.depth = 5; // Increase AVA's printing depth

module.exports = {
timeout: '10000',
files: ['sandbox-ts/*.ava.ts'],
failWithoutAssertions: false,
extensions: {
js: true,
ts: 'module'
},
require: ['ts-node/register', 'near-workspaces'],
"nodeArguments": [
"--import=tsimp"
]
};
14 changes: 9 additions & 5 deletions contract-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
"type": "module",
"scripts": {
"build": "near-sdk-js build src/contract.ts build/hello_near.wasm",
"test": "$npm_execpath run build && cd sandbox-ts && $npm_execpath run test -- -- ../build/hello_near.wasm",
"postinstall": "cd sandbox-ts && $npm_execpath install"
"test": "$npm_execpath build && ava -- ./build/hello_near.wasm"
},
"dependencies": {
"near-cli": "^4.0.7",
"near-cli": "^4.0.8",
"near-sdk-js": "1.0.0"
},
"devDependencies": {
"typescript": "^5.2.2",
"ts-morph": "^20.0.0"
"@ava/typescript": "^4.1.0",
"ava": "^6.1.2",
"near-workspaces": "^3.5.0",
"ts-morph": "^21.0.1",
"ts-node": "^10.9.2",
"tsimp": "^2.0.11",
"typescript": "^5.3.3"
}
}
9 changes: 0 additions & 9 deletions contract-ts/sandbox-ts/ava.config.cjs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
import { Worker, NearAccount } from 'near-workspaces';
import anyTest, { TestFn } from 'ava';
import { setDefaultResultOrder } from 'dns'; setDefaultResultOrder('ipv4first'); // temp fix for node >v17

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

test.beforeEach(async (t) => {
const test = anyTest as TestFn<{}>;

test.before(async (t) => {
// Init the worker and start a Sandbox server
const worker = await Worker.init();
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.worker = worker;
t.context.accounts = { root, contract };
accounts = { root, contract };
});

test.afterEach.always(async (t) => {
test.after.always(async (t) => {
// Stop Sandbox server
await t.context.worker.tearDown().catch((error) => {
await 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 { contract } = 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;
const { root, contract } = accounts;
await root.call(contract, 'set_greeting', { greeting: 'Howdy' });
const greeting: string = await contract.view('get_greeting', {});
t.is(greeting, 'Howdy');
Expand Down
17 changes: 0 additions & 17 deletions contract-ts/sandbox-ts/package.json

This file was deleted.

9 changes: 7 additions & 2 deletions contract-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "es5",
"noEmit": true
"target": "ES5",
"noEmit": true,
"noImplicitAny": false,
},
"files": [
"sandbox-ts/main.ava.ts",
"src/contract.ts"
],
"exclude": [
"node_modules"
],
Expand Down
Loading