{"id":3672,"date":"2022-12-15T12:43:44","date_gmt":"2022-12-15T12:43:44","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=3672"},"modified":"2022-12-15T13:28:02","modified_gmt":"2022-12-15T13:28:02","slug":"test-solidity-require-javascript","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/test-solidity-require-javascript\/","title":{"rendered":"How to easily test Solidity require in JavaScript"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/test-solidity-require-javascript\/#Steps_to_set_up_and_test_the_Solidity_require_statement_using_JavaScript\" title=\"Steps to set up and test the Solidity require statement using JavaScript\">Steps to set up and test the Solidity require statement using JavaScript<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/test-solidity-require-javascript\/#1_Install_the_necessary_tools_and_dependencies\" title=\"1. Install the necessary tools and dependencies\">1. Install the necessary tools and dependencies<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/test-solidity-require-javascript\/#2_Set_up_a_new_Truffle_project\" title=\"2. Set up a new Truffle project\">2. Set up a new Truffle project<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/test-solidity-require-javascript\/#3_Write_your_Solidity_contract\" title=\"3. Write your Solidity contract\">3. Write your Solidity contract<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/test-solidity-require-javascript\/#4_Write_your_JavaScript_tests\" title=\"4. Write your JavaScript tests\">4. Write your JavaScript tests<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/test-solidity-require-javascript\/#5_Run_your_tests\" title=\"5. Run your tests\">5. Run your tests<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p>In order to test Solidity require statement using JavaScript, you will need to set up a development environment that includes a local <a href=\"https:\/\/metaschool.so\/blockchains\/ethereum\" target=\"_blank\" rel=\"noreferrer noopener\">Ethereum blockchain<\/a> and a JavaScript testing framework.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"steps-to-set-up-and-test-the-solidity-require-statement-using-javascript\"><span class=\"ez-toc-section\" id=\"Steps_to_set_up_and_test_the_Solidity_require_statement_using_JavaScript\"><\/span>Steps to set up and test the Solidity require statement using JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>They will involve setting a development environment, <a href=\"https:\/\/metaschool.so\/courses\/writing-your-first-hello-world-contract-in-solidity\" target=\"_blank\" rel=\"noreferrer noopener\">writing a Solidity contract<\/a> that uses the <code>require<\/code> statement, writing JavaScript tests for the contract, and running the tests to verify that the <code>require<\/code> statement behaves as expected.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-install-the-necessary-tools-and-dependencies\"><span class=\"ez-toc-section\" id=\"1_Install_the_necessary_tools_and_dependencies\"><\/span>1. Install the necessary tools and dependencies<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Install the <code>truffle<\/code> and <code>ganache-cli<\/code> npm packages. These tools will allow you to set up a local Ethereum blockchain and deploy your Solidity contracts.<\/li><li>Install the <code>chai<\/code> npm package. This is a popular JavaScript testing framework that will be used to write and run the tests for your Solidity contracts.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-set-up-a-new-truffle-project\"><span class=\"ez-toc-section\" id=\"2_Set_up_a_new_Truffle_project\"><\/span>2. Set up a new Truffle project<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Use the <code>truffle init<\/code> command to create a new <a href=\"https:\/\/trufflesuite.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Truffle<\/a> project in a directory of your choice. This will create a new directory with the basic structure and configuration files needed for a Truffle project.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-write-your-solidity-contract\"><span class=\"ez-toc-section\" id=\"3_Write_your_Solidity_contract\"><\/span>3. Write your Solidity contract<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a new Solidity file in the <code>contracts<\/code> directory of your Truffle project. This is where you will write your contract that uses the <code>require<\/code> statement.<\/li><li>In your Solidity contract, use the <code>require<\/code> statement to enforce a certain condition or precondition. For example, you might require that a certain function can only be called by the contract owner, or that a certain variable must have a certain value before a certain operation can be performed.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-write-your-javascript-tests\"><span class=\"ez-toc-section\" id=\"4_Write_your_JavaScript_tests\"><\/span>4. Write your JavaScript tests<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a new JavaScript file in the <code>test<\/code> directory of your Truffle project. This is where you will write the tests for your Solidity contract.<\/li><li>In your JavaScript tests, use the <code>chai<\/code> library to define test cases for your <a href=\"https:\/\/metaschool.so\/articles\/smart-contract-definition\/\">smart contract<\/a>. For each test case, use the <code>assert.throws<\/code> method to assert that the <code>require<\/code> statement in your contract will throw an exception if the condition it enforces is not met.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-run-your-tests\"><span class=\"ez-toc-section\" id=\"5_Run_your_tests\"><\/span>5. Run your tests<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Start your local Ethereum blockchain using the <code>ganache-cli<\/code> tool.<\/li><li>Use the <code>truffle test<\/code> command to run your JavaScript tests. This will deploy your <a href=\"https:\/\/metaschool.so\/articles\/solidity-language-smart-contracts\/\">Solidity<\/a> contract to the local blockchain and execute the tests you defined.<\/li><li>If all of your tests pass, then you have successfully tested the <code>require<\/code> statement in Solidity using JavaScript.<\/li><\/ul>\n\n\n\n<p>That wasn&#8217;t so difficult, was it? Give it a try and see if it works for you \ud83e\uddf1<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3674,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[31,47],"class_list":["post-3672","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-ethereum-blockchain","tag-smart-contracts"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=3672"}],"version-history":[{"count":3,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3672\/revisions"}],"predecessor-version":[{"id":3691,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3672\/revisions\/3691"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/3674"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=3672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=3672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=3672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}