I got this snippet from: https://hardhat.org/tutorial/testing-contracts

Smart Contract Test

In this test, what does const { expect } = require("chai") mean? Also, I thought describe() is from the Mocha framework but I don't see any const { describe } = require("mocha") and const { it } = require("mocha") or something similar along that line here, but this test still runs successfully, why? Someone please help?

1 Answer 1

const { expect } = require("chai")

It means, you are importing the 'expect' from chai library.

But you don't need to import(require) describe and it, because they are built-in functions.

Think of "require" in the code above. You aren't importing require to use require. Because when you install node.js, the require will come built-in. You just have to know to use it.

That is the difference between external and built-in function.

In any programming langauge, there are some built-in functions and you don't need to require(in ES5 syntax and import in ES6 syntax) to use them.

Similarly, describe and it are built-in functions to mocha.

However, expect isn't something similar. They are external APIs and you need to import or require them from the chai library to use them.

The main difference between chai and mocha is that, mocha is a framework. While the Chai is a library. To use the APIs from the library you need to import or require them. While it is not the case for mocha.

You can read more about require in node.js here.

  • So what can be called as "something" inside the const { something } = require("something else")? Can "something" be a library, function, module, variable, or what? What about "something else"? Sep 19, 2022 at 17:37
  • Contrarily, something can be functions , methods. And something else is library in chai case. And you can also export normal functions by using module.export = something.
    – Ad-h0c
    Sep 19, 2022 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.