Giter Site home page Giter Site logo

arianxdev / hardhat-jest Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 1.0 387 KB

A Hardhat plugin that allows you to use Jest easily!

Home Page: https://npmjs.com/package/hardhat-jest

License: MIT License

JavaScript 100.00%
blockchain ethereum hardhat hardhat-plugin jest solidity web3 hardhat-jest

hardhat-jest's Introduction

hardhat-jest hardhat

Npm package version hardhat

Warning: If you're using < v1.0.6, Please update the package to the latest version ASAP! It may cause errors.

Have you always wanted to use Jest instead of Mocha + Chai in your Hardhat Projects? ๐Ÿ˜‰ Good News!

hardhat-jest allows you to use Jest easily ๐Ÿ˜ƒโœŒ๏ธ

Installation

First of all, Make sure that you have jest already installed!

Step 1: Install the package

npm i hardhat-jest

Step 2: Add to your hardhat.config.js file

require("hardhat-jest");

If you're using Typescript, to your hardhat.config.ts file

import "hardhat-jest"; // Typescript

Usage

It's so easy, You can run your Jest tests by

npx hardhat jest

You've been using npx hardhat test to run your Mocha tests so far, but NOW you only need to use jest instead of test and Done!

Options

If you're looking to see all the options, you can do it by running npx hardhat help jest:

Usage: hardhat [GLOBAL OPTIONS] jest [--bail] [--no-compile] [--show-config] [--use-verbose <BOOLEAN>] [--watch] [--watch-all]

OPTIONS:

  --bail       	Stop running tests after the first test failure.
  --no-compile 	Do NOT compile before running this task.
  --show-config	Print your jest config and then exits.
  --watch      	Watch files for changes and rerun tests related to changed files.
  --watch-all  	Watch files for changes and rerun all tests.
  --use-verbose	Display individual test results with the test suite hierarchy. | Enable / Disable [boolean] (default: false)


jest: Runs Jest tests

For global options help run: hardhat help

to use options, you can easily run

npx hardhat jest <option-name>

// for example
npx hardhat jest --no-compile --watch

--use-verbose

If you want Jest to display individual test results with the test suite hierarchy, you can use --use-verbose this way:

npx hardhat jest --use-verbose true // enables verbose mode only for this project

npx hardhat jest --use-verbose false // disables verbose mode for the project

Remember: the default value is false and disabled, so you must enable it if you want.

Specific files to test (testFiles)

If you want Jest to run only specific files instead of all test files, you can do that by:

npx hardhat jest test/Test3.test.js test/Test4.test.js

NOTE: You must specify files with their full path (from the project root directory)

Hardhat-tasks

You can see all the available Hardhat tasks by running npx hardhat

AVAILABLE TASKS:

  check             	Check whatever you need
  clean             	Clears the cache and deletes all artifacts
  compile           	Compiles the entire project, building all artifacts
  console           	Opens a hardhat console
  coverage          	Generates a code coverage report for tests
  flatten           	Flattens and prints contracts and their dependencies
  gas-reporter:merge
  help              	Prints this message
> jest                  Runs Jest tests
  node              	Starts a JSON-RPC server on top of Hardhat Network
  run               	Runs a user-defined script after compiling the project
  test              	Runs mocha tests
  typechain         	Generate Typechain typings for compiled contracts
  verify            	Verifies contract on Etherscan

and There it is, jest task is added successfully! it means that you can run npx hardhat jest and your tests will be run!

Final thoughts

Finally, show us some love by starring the repository on GitHub!๏ธ ๐Ÿ˜Š

Happy hacking!

hardhat-jest's People

Contributors

arianxdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

cryptoman544

hardhat-jest's Issues

Add jest as a peer dependency

Jest should be a peer dependency of the plugin. I'm not sure which range of versions should be supported though, but any version included should have the same interface in the part of the API that is being used.

Probably the easiest thing to do is to just use ^29.0.0.

Please consider adding a mock project/starter kit

Hi,

Thanks for the plugin! I wanted to check Jest, but I had a hard time making it work out of the box.

For instance, if you create a sample Hardhat project using the npx hardhat prompt, add jest following your guide, you'll run into a By default "node_modules" folder is ignored by transformers.. This means a jest config is needed, you have to debug, add a test env, etc, etc. This makes it hard to switch to this plugin when everything is working with Mocha.

Therefore, please consider adding to your roadmap a starter project, as it would help jest-noobs like me to make the switch.

Cheers!

Linking failure in asm.js: Unexpected stdlib member

@fvictorio Excuse me, I've come across an issue when using Jest + Hardhat, and searched a lot about it, but I couldn't figure it out,
It would be my pleasure if you can help me solve it, Thanks,

When I run npx hardhat jest, I get this warning

(node:34981) V8: /home/user/Projects/spectre-decentralized-exchange/node_modules/rustbn.js/lib/index.asm.js:2 Linking failure in asm.js: Unexpected stdlib member
(Use `node --trace-warnings ...` to show where the warning was created)
(node:34987) V8: /home/user/Projects/spectre-decentralized-exchange/node_modules/rustbn.js/lib/index.asm.js:2 Linking failure in asm.js: Unexpected stdlib member
(Use `node --trace-warnings ...` to show where the warning was created)

I couldn't figure out what's the problem with it, it happens when it's a Hardhat project, if you use Jest in other ways inside of a Hardhat project, it throws the same warning,

Do you know what's wrong or how can I solve it?! @fvictorio

Really appreciate your help.

Consider using `setupFiles`

I only skimmed the code, but as far as I can tell, this plugin needs all the files to explicitly require Hardhat. In typescript projects this is already necessary, but in js projects is not.

The builtin test task solves this by adding the "hardaht/register" module to the require option of Mocha. I think you can make something similar in jest with setupFiles. I haven't tried it though.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.