create-near-app 4.0.0-beta.2.0 → 4.0.0-beta.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/make.js CHANGED
@@ -59,18 +59,20 @@ async function createFiles({ contract, frontend, tests, projectPath, verbose, ro
59
59
  verbose,
60
60
  skip: skip.map(f => path_1.default.join(contractSourceDir, f))
61
61
  });
62
- // copy tests
63
- let sourceTestDir = `${rootDir}/integration-tests`;
64
- if (tests === 'js') {
65
- sourceTestDir = path_1.default.resolve(sourceTestDir, 'workspaces-tests/ts');
66
- }
67
- else {
68
- sourceTestDir = path_1.default.resolve(sourceTestDir, 'workspaces-tests/rs');
69
- }
70
- await copyDir(sourceTestDir, `${projectPath}/integration-tests/`, {
62
+ // copy tests - shared files
63
+ let sourceTestSharedDir = path_1.default.resolve(`${rootDir}/integration-tests/shared/${tests}-tests`);
64
+ await copyDir(sourceTestSharedDir, `${projectPath}/integration-tests/`, {
71
65
  verbose,
72
- skip: skip.map(f => path_1.default.join(sourceTestDir, f))
66
+ skip: skip.map(f => path_1.default.join(sourceTestSharedDir, f))
73
67
  });
68
+ // copy tests - overrides files
69
+ let sourceTestOverridesDir = path_1.default.resolve(`${rootDir}/integration-tests/overrides/${contract}-contract/${tests}-tests`);
70
+ if (fs_1.default.existsSync(sourceTestOverridesDir)) {
71
+ await copyDir(sourceTestOverridesDir, `${projectPath}/integration-tests/`, {
72
+ verbose,
73
+ skip: skip.map(f => path_1.default.join(sourceTestOverridesDir, f))
74
+ });
75
+ }
74
76
  // add .gitignore
75
77
  await (0, exports.renameFile)(`${projectPath}/near.gitignore`, `${projectPath}/.gitignore`);
76
78
  }
@@ -99,7 +99,7 @@ const integrationTestScripts = (contract, tests) => {
99
99
  case 'js':
100
100
  if (tests === 'js') {
101
101
  return {
102
- 'test:integration': 'npm run build:contract && cd integration-tests && npm test -- -- "./contract/build/hello_near.wasm" "js"',
102
+ 'test:integration': 'npm run build:contract && cd integration-tests && npm test -- -- "./contract/build/hello_near.wasm"',
103
103
  };
104
104
  }
105
105
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-near-app",
3
- "version": "4.0.0-beta.2.0",
3
+ "version": "4.0.0-beta.3.0",
4
4
  "description": "Quickly scaffold your dApp on NEAR Blockchain",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "near-cli": "^3.4.0",
13
- "near-sdk-js": "0.4.0-3"
13
+ "near-sdk-js": "0.4.0-4"
14
14
  },
15
15
  "devDependencies": {
16
16
  "typescript": "^4.7.4"
@@ -17,12 +17,8 @@ test.beforeEach(async (t) => {
17
17
  await contract.deploy(
18
18
  process.argv[2],
19
19
  );
20
- if (process.argv[3] === 'js') {
21
- // JavaScript contracts require calling 'init' function upon deployment
22
- // We pass this parameter in process.argv only for convenience of create-near-app
23
- // Rust contracts don't require 'init'
24
- await contract.call(contract, 'init', {});
25
- }
20
+ // JavaScript contracts require calling 'init' function upon deployment
21
+ await contract.call(contract, 'init', {});
26
22
 
27
23
  // Save state for test runs, it is unique for each test
28
24
  t.context.worker = worker;
@@ -0,0 +1,74 @@
1
+ use std::{env, fs};
2
+ use near_units::parse_near;
3
+ use serde_json::json;
4
+ use workspaces::prelude::*;
5
+ use workspaces::{network::Sandbox, Account, Contract, Worker};
6
+
7
+ #[tokio::main]
8
+ async fn main() -> anyhow::Result<()> {
9
+ let wasm_arg: &str = &(env::args().nth(1).unwrap());
10
+ let wasm_filepath = fs::canonicalize(env::current_dir()?.join(wasm_arg))?;
11
+
12
+ let worker = workspaces::sandbox().await?;
13
+ let wasm = std::fs::read(wasm_filepath)?;
14
+ let contract = worker.dev_deploy(&wasm).await?;
15
+
16
+ // create accounts
17
+ let account = worker.dev_create_account().await?;
18
+ let alice = account
19
+ .create_subaccount(&worker, "alice")
20
+ .initial_balance(parse_near!("30 N"))
21
+ .transact()
22
+ .await?
23
+ .into_result()?;
24
+
25
+ // js contracts need to be initialized
26
+ contract
27
+ .call(&worker, "init")
28
+ .transact()
29
+ .await?;
30
+
31
+ // begin tests
32
+ test_default_message(&alice, &contract, &worker).await?;
33
+ test_changes_message(&alice, &contract, &worker).await?;
34
+ Ok(())
35
+ }
36
+
37
+ async fn test_default_message(
38
+ user: &Account,
39
+ contract: &Contract,
40
+ worker: &Worker<Sandbox>,
41
+ ) -> anyhow::Result<()> {
42
+ let message: String = user
43
+ .call(&worker, contract.id(), "get_greeting")
44
+ .args_json(json!({}))?
45
+ .transact()
46
+ .await?
47
+ .json()?;
48
+
49
+ assert_eq!(message, "Hello".to_string());
50
+ println!(" Passed ✅ gets default message");
51
+ Ok(())
52
+ }
53
+
54
+ async fn test_changes_message(
55
+ user: &Account,
56
+ contract: &Contract,
57
+ worker: &Worker<Sandbox>,
58
+ ) -> anyhow::Result<()> {
59
+ user.call(&worker, contract.id(), "set_greeting")
60
+ .args_json(json!({"message": "Howdy"}))?
61
+ .transact()
62
+ .await?;
63
+
64
+ let message: String = user
65
+ .call(&worker, contract.id(), "get_greeting")
66
+ .args_json(json!({}))?
67
+ .transact()
68
+ .await?
69
+ .json()?;
70
+
71
+ assert_eq!(message, "Howdy".to_string());
72
+ println!(" Passed ✅ changes message");
73
+ Ok(())
74
+ }
@@ -0,0 +1,44 @@
1
+ import { Worker, NEAR, NearAccount } from 'near-workspaces';
2
+ import anyTest, { TestFn } from 'ava';
3
+
4
+ const test = anyTest as TestFn<{
5
+ worker: Worker;
6
+ accounts: Record<string, NearAccount>;
7
+ }>;
8
+
9
+ test.beforeEach(async (t) => {
10
+ // Init the worker and start a Sandbox server
11
+ const worker = await Worker.init();
12
+
13
+ // Deploy contract
14
+ const root = worker.rootAccount;
15
+ const contract = await root.createSubAccount('test-account');
16
+ // Get wasm file path from package.json test script in folder above
17
+ await contract.deploy(
18
+ process.argv[2],
19
+ );
20
+
21
+ // Save state for test runs, it is unique for each test
22
+ t.context.worker = worker;
23
+ t.context.accounts = { root, contract };
24
+ });
25
+
26
+ test.afterEach(async (t) => {
27
+ // Stop Sandbox server
28
+ await t.context.worker.tearDown().catch((error) => {
29
+ console.log('Failed to stop the Sandbox:', error);
30
+ });
31
+ });
32
+
33
+ test('returns the default greeting', async (t) => {
34
+ const { contract } = t.context.accounts;
35
+ const message: string = await contract.view('get_greeting', {});
36
+ t.is(message, 'Hello');
37
+ });
38
+
39
+ test('changes the message', async (t) => {
40
+ const { root, contract } = t.context.accounts;
41
+ await root.call(contract, 'set_greeting', { message: 'Howdy' });
42
+ const message: string = await contract.view('get_greeting', {});
43
+ t.is(message, 'Howdy');
44
+ });
@@ -14,7 +14,7 @@ serde_json = { version = "1.0", features = ["arbitrary_precision"] }
14
14
  tokio = { version = "1.18.1", features = ["full"] }
15
15
  tracing = "0.1"
16
16
  tracing-subscriber = { version = "0.3.11", features = ["env-filter"] }
17
- workspaces = "0.2.1"
17
+ workspaces = "0.4.0"
18
18
  pkg-config = "0.3.1"
19
19
 
20
20
  [[example]]
@@ -14,8 +14,8 @@ async fn main() -> anyhow::Result<()> {
14
14
  let contract = worker.dev_deploy(&wasm).await?;
15
15
 
16
16
  // create accounts
17
- let owner = worker.root_account();
18
- let alice = owner
17
+ let account = worker.dev_create_account().await?;
18
+ let alice = account
19
19
  .create_subaccount(&worker, "alice")
20
20
  .initial_balance(parse_near!("30 N"))
21
21
  .transact()
@@ -1,17 +0,0 @@
1
- {
2
- "name": "ava-testing",
3
- "version": "1.0.0",
4
- "license": "(MIT AND Apache-2.0)",
5
- "scripts": {
6
- "test": "ava"
7
- },
8
- "devDependencies": {
9
- "@types/bn.js": "^5.1.0",
10
- "@types/node": "^18.6.2",
11
- "ava": "^4.2.0",
12
- "near-api-js": "^0.44.2",
13
- "ts-node": "^10.8.0",
14
- "typescript": "^4.7.2"
15
- },
16
- "dependencies": {}
17
- }
@@ -1,34 +0,0 @@
1
- import {KeyPair, keyStores} from 'near-api-js';
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
-
6
- // first make sure you deployed your contract with dev-deploy, see ../package.json deploy script
7
- // here we take the contract from the auto-generated neardev folder
8
- const CONTRACT_NAME = fs.readFileSync(path.resolve(__dirname, '../../contract/neardev/dev-account'), 'utf-8');
9
- const NETWORK_ID = 'testnet';
10
-
11
- // Create an InMemoryKeyStore
12
- const keyStore = new keyStores.InMemoryKeyStore();
13
-
14
- // Load credentials
15
- const credPath = `${process.env.HOME}/.near-credentials/${NETWORK_ID}/${CONTRACT_NAME}.json`;
16
- let credentials = JSON.parse(fs.readFileSync(credPath));
17
-
18
- // Save key in the key store
19
- keyStore.setKey(
20
- NETWORK_ID,
21
- CONTRACT_NAME,
22
- KeyPair.fromString(credentials.private_key)
23
- );
24
-
25
- export const nearConfig = {
26
- networkId: NETWORK_ID,
27
- nodeUrl: 'https://rpc.testnet.near.org',
28
- contractName: CONTRACT_NAME,
29
- walletUrl: 'https://wallet.testnet.near.org',
30
- helperUrl: 'https://helper.testnet.near.org',
31
- explorerUrl: 'https://explorer.testnet.near.org',
32
- headers: {},
33
- deps: {keyStore}
34
- };
@@ -1,32 +0,0 @@
1
- import anyTest, { TestFn } from 'ava';
2
-
3
- import { Near, Account, Contract } from 'near-api-js';
4
- import { nearConfig } from './config';
5
-
6
- const test = anyTest as TestFn<{
7
- accounts: Record<string, any>;
8
- }>;
9
-
10
- test.beforeEach(async (t) => {
11
- const near = await new Near(nearConfig);
12
- const user = await new Account(near.connection, nearConfig.contractName);
13
- const contract = await new Contract(
14
- user,
15
- nearConfig.contractName,
16
- { viewMethods: ['get_greeting'], changeMethods: ['set_greeting'] }
17
- );
18
- t.context.accounts = { contract };
19
- });
20
-
21
- test('returns the default greeting', async (t) => {
22
- const { contract } = t.context.accounts;
23
- const message: string = await contract.get_greeting({});
24
- t.is(message, 'Hello');
25
- });
26
-
27
- test('changes the message', async (t) => {
28
- const { contract } = t.context.accounts;
29
- await contract.set_greeting({args:{ message: 'Howdy' }});
30
- const message: string = await contract.get_greeting({});
31
- t.is(message, 'Howdy');
32
- });
@@ -1,9 +0,0 @@
1
- require("util").inspect.defaultOptions.depth = 5; // Increase AVA's printing depth
2
-
3
- module.exports = {
4
- timeout: "300000",
5
- files: ["src/*.ava.ts"],
6
- failWithoutAssertions: false,
7
- extensions: ["ts"],
8
- require: ["ts-node/register"],
9
- };