create-near-app icon indicating copy to clipboard operation
create-near-app copied to clipboard

Switch workspaces-rs tests to build contract

Open austinabell opened this issue 1 year ago • 0 comments

Currently, tests are setup in such a way that they are in their own crate as a binary and accept the wasm file path as an argument. This is not ideal because the contract has to be built separately (or through some script like it does through yarn now).

compile_project can be used to streamline this and make it easier for devs to expand on these tests later.

The project structure of having a separate bin crate just used for testing isn't ideal, should just be a tests directory under the cargo root https://doc.rust-lang.org/book/ch11-03-test-organization.html, but that is another problem

Edit: also, the larger issue is that contracts are not automatically rebuilt for tests, so requires devs to know that you need to build before running tests

What this diff looks like:

diff --git a/integration-tests/src/tests.rs b/integration-tests/src/tests.rs
index 8afd31d..8c39e01 100644
--- a/integration-tests/src/tests.rs
+++ b/integration-tests/src/tests.rs
@@ -1,4 +1,3 @@
-use std::{env, fs};
 use near_units::parse_near;
 use serde_json::json;
 use workspaces::prelude::*;
@@ -6,12 +5,10 @@ use workspaces::{network::Sandbox, Account, Contract, Worker};
 
 #[tokio::main]
 async fn main() -> anyhow::Result<()> {
-    let wasm_arg: &str = &(env::args().nth(1).unwrap());
-    let wasm_filepath = fs::canonicalize(env::current_dir()?.join(wasm_arg))?;
-
     let worker = workspaces::sandbox().await?;
-    let wasm = std::fs::read(wasm_filepath)?;
-    let contract = worker.dev_deploy(&wasm).await?;
+    let contract = worker
+        .dev_deploy(&workspaces::compile_project("../contract").await?)
+        .await?;
 
     // create accounts
     let account = worker.dev_create_account().await?;
@@ -65,4 +62,4 @@ async fn test_changes_message(
     assert_eq!(message, "Howdy".to_string());
     println!("      Passed ✅ changes message");
     Ok(())
-}
\ No newline at end of file
+}
diff --git a/package.json b/package.json
index 6bc7660..6aa20a4 100644
--- a/package.json
+++ b/package.json
@@ -8,7 +8,7 @@
     "build:contract": "cd contract && ./build.sh",
     "test": "npm run test:unit && npm run test:integration",
     "test:unit": "cd contract && cargo test",
-    "test:integration": "cd integration-tests && cargo run --example integration-tests \"../contract/target/wasm32-unknown-unknown/release/hello_near.wasm\"",
+    "test:integration": "cd integration-tests && cargo run --example integration-tests",
     "postinstall": "echo no frontend && echo rs tests && echo rs contract"
   },
   "devDependencies": {

austinabell avatar Sep 21 '22 12:09 austinabell