Add option for jest test scaffolding (#2200)

* Add in option for jest scaffolding

* Add tests for Jest in both JS/TS

* Clone moved value; use new function name

* Remove invalid tests
This commit is contained in:
Kyle Gilliam 2022-12-05 07:11:56 -08:00 committed by GitHub
parent 50724df110
commit 23c9717af1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 173 additions and 37 deletions

View File

@ -68,6 +68,8 @@ pub enum Command {
javascript: bool, javascript: bool,
#[clap(long)] #[clap(long)]
no_git: bool, no_git: bool,
#[clap(long)]
jest: bool,
}, },
/// Builds the workspace. /// Builds the workspace.
#[clap(name = "build", alias = "b")] #[clap(name = "build", alias = "b")]
@ -359,7 +361,8 @@ pub fn entry(opts: Opts) -> Result<()> {
name, name,
javascript, javascript,
no_git, no_git,
} => init(&opts.cfg_override, name, javascript, no_git), jest,
} => init(&opts.cfg_override, name, javascript, no_git, jest),
Command::New { name } => new(&opts.cfg_override, name), Command::New { name } => new(&opts.cfg_override, name),
Command::Build { Command::Build {
idl, idl,
@ -466,7 +469,13 @@ pub fn entry(opts: Opts) -> Result<()> {
} }
} }
fn init(cfg_override: &ConfigOverride, name: String, javascript: bool, no_git: bool) -> Result<()> { fn init(
cfg_override: &ConfigOverride,
name: String,
javascript: bool,
no_git: bool,
jest: bool,
) -> Result<()> {
if Config::discover(cfg_override)?.is_some() { if Config::discover(cfg_override)?.is_some() {
return Err(anyhow!("Workspace already initialized")); return Err(anyhow!("Workspace already initialized"));
} }
@ -474,7 +483,7 @@ fn init(cfg_override: &ConfigOverride, name: String, javascript: bool, no_git: b
// We need to format different cases for the dir and the name // We need to format different cases for the dir and the name
let rust_name = name.to_snake_case(); let rust_name = name.to_snake_case();
let project_name = if name == rust_name { let project_name = if name == rust_name {
name rust_name.clone()
} else { } else {
name.to_kebab_case() name.to_kebab_case()
}; };
@ -496,6 +505,17 @@ fn init(cfg_override: &ConfigOverride, name: String, javascript: bool, no_git: b
fs::create_dir("app")?; fs::create_dir("app")?;
let mut cfg = Config::default(); let mut cfg = Config::default();
if jest {
cfg.scripts.insert(
"test".to_owned(),
if javascript {
"yarn run jest"
} else {
"yarn run jest --preset ts-jest"
}
.to_owned(),
);
} else {
cfg.scripts.insert( cfg.scripts.insert(
"test".to_owned(), "test".to_owned(),
if javascript { if javascript {
@ -505,6 +525,8 @@ fn init(cfg_override: &ConfigOverride, name: String, javascript: bool, no_git: b
} }
.to_owned(), .to_owned(),
); );
}
let mut localnet = BTreeMap::new(); let mut localnet = BTreeMap::new();
localnet.insert( localnet.insert(
rust_name, rust_name,
@ -540,20 +562,25 @@ fn init(cfg_override: &ConfigOverride, name: String, javascript: bool, no_git: b
if javascript { if javascript {
// Build javascript config // Build javascript config
let mut package_json = File::create("package.json")?; let mut package_json = File::create("package.json")?;
package_json.write_all(template::package_json().as_bytes())?; package_json.write_all(template::package_json(jest).as_bytes())?;
let mut mocha = File::create(&format!("tests/{}.js", &project_name))?; if jest {
mocha.write_all(template::mocha(&project_name).as_bytes())?; let mut test = File::create(&format!("tests/{}.test.js", &project_name))?;
test.write_all(template::jest(&project_name).as_bytes())?;
} else {
let mut test = File::create(&format!("tests/{}.js", &project_name))?;
test.write_all(template::mocha(&project_name).as_bytes())?;
}
let mut deploy = File::create("migrations/deploy.js")?; let mut deploy = File::create("migrations/deploy.js")?;
deploy.write_all(template::deploy_script().as_bytes())?; deploy.write_all(template::deploy_script().as_bytes())?;
} else { } else {
// Build typescript config // Build typescript config
let mut ts_config = File::create("tsconfig.json")?; let mut ts_config = File::create("tsconfig.json")?;
ts_config.write_all(template::ts_config().as_bytes())?; ts_config.write_all(template::ts_config(jest).as_bytes())?;
let mut ts_package_json = File::create("package.json")?; let mut ts_package_json = File::create("package.json")?;
ts_package_json.write_all(template::ts_package_json().as_bytes())?; ts_package_json.write_all(template::ts_package_json(jest).as_bytes())?;
let mut deploy = File::create("migrations/deploy.ts")?; let mut deploy = File::create("migrations/deploy.ts")?;
deploy.write_all(template::ts_deploy_script().as_bytes())?; deploy.write_all(template::ts_deploy_script().as_bytes())?;
@ -3169,6 +3196,7 @@ mod tests {
"await".to_string(), "await".to_string(),
true, true,
false, false,
false,
) )
.unwrap(); .unwrap();
} }
@ -3184,6 +3212,7 @@ mod tests {
"fn".to_string(), "fn".to_string(),
true, true,
false, false,
false,
) )
.unwrap(); .unwrap();
} }
@ -3199,6 +3228,7 @@ mod tests {
"1project".to_string(), "1project".to_string(),
true, true,
false, false,
false,
) )
.unwrap(); .unwrap();
} }

View File

@ -221,7 +221,47 @@ describe("{}", () => {{
) )
} }
pub fn package_json() -> String { pub fn jest(name: &str) -> String {
format!(
r#"const anchor = require("@project-serum/anchor");
describe("{}", () => {{
// Configure the client to use the local cluster.
anchor.setProvider(anchor.AnchorProvider.env());
it("Is initialized!", async () => {{
// Add your test here.
const program = anchor.workspace.{};
const tx = await program.methods.initialize().rpc();
console.log("Your transaction signature", tx);
}});
}});
"#,
name,
name.to_upper_camel_case(),
)
}
pub fn package_json(jest: bool) -> String {
if jest {
format!(
r#"{{
"scripts": {{
"lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
"lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"
}},
"dependencies": {{
"@project-serum/anchor": "^{0}"
}},
"devDependencies": {{
"jest": "^29.0.3",
"prettier": "^2.6.2"
}}
}}
"#,
VERSION
)
} else {
format!( format!(
r#"{{ r#"{{
"scripts": {{ "scripts": {{
@ -240,9 +280,33 @@ pub fn package_json() -> String {
"#, "#,
VERSION VERSION
) )
}
} }
pub fn ts_package_json() -> String { pub fn ts_package_json(jest: bool) -> String {
if jest {
format!(
r#"{{
"scripts": {{
"lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
"lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"
}},
"dependencies": {{
"@project-serum/anchor": "^{0}"
}},
"devDependencies": {{
"@types/bn.js": "^5.1.0",
"@types/jest": "^29.0.3",
"jest": "^29.0.3",
"prettier": "^2.6.2",
"ts-jest": "^29.0.2",
"typescript": "^4.3.5"
}}
}}
"#,
VERSION
)
} else {
format!( format!(
r#"{{ r#"{{
"scripts": {{ "scripts": {{
@ -266,6 +330,7 @@ pub fn ts_package_json() -> String {
"#, "#,
VERSION VERSION
) )
}
} }
pub fn ts_mocha(name: &str) -> String { pub fn ts_mocha(name: &str) -> String {
@ -295,7 +360,47 @@ describe("{}", () => {{
) )
} }
pub fn ts_config() -> &'static str { pub fn ts_jest(name: &str) -> String {
format!(
r#"import * as anchor from "@project-serum/anchor";
import {{ Program }} from "@project-serum/anchor";
import {{ {} }} from "../target/types/{}";
describe("{}", () => {{
// Configure the client to use the local cluster.
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.{} as Program<{}>;
it("Is initialized!", async () => {{
// Add your test here.
const tx = await program.methods.initialize().rpc();
console.log("Your transaction signature", tx);
}});
}});
"#,
name.to_upper_camel_case(),
name.to_snake_case(),
name,
name.to_upper_camel_case(),
name.to_upper_camel_case(),
)
}
pub fn ts_config(jest: bool) -> &'static str {
if jest {
r#"{
"compilerOptions": {
"types": ["jest"],
"typeRoots": ["./node_modules/@types"],
"lib": ["es2015"],
"module": "commonjs",
"target": "es6",
"esModuleInterop": true
}
}
"#
} else {
r#"{ r#"{
"compilerOptions": { "compilerOptions": {
"types": ["mocha", "chai"], "types": ["mocha", "chai"],
@ -305,8 +410,9 @@ pub fn ts_config() -> &'static str {
"target": "es6", "target": "es6",
"esModuleInterop": true "esModuleInterop": true
} }
} }
"# "#
}
} }
pub fn git_ignore() -> &'static str { pub fn git_ignore() -> &'static str {