How to test require statement with hardhat? - ethereum

I'm testing a smart contract that has a require statement in one of the functions.
In the past, I would just write.
await expect(token.transfer(user.address, ethers.utils.parseEther("10")).to.be.reverted;
But recently it stopped working, and it gives me this error:
Property 'reverted' does not exist on type 'Assertion'.ts(2339)
Any ideas why it's not working properly, or am I doing something wrong?
Thanks a lot.

It turns out I didn't import chai correctly.
So to solve this I created another file called chai-setup.ts and added this code inside:
import chaiModule from "chai";
import { chaiEthers } from "chai-ethers";
chaiModule.use(chaiEthers);
export = chaiModule;
Then in my main test file, I added this import statement:
import { expect } from "./chai-setup"
And that solved my problem.

A little late to answer, but you can use hardhat chai matchers to solve this issue.
Install hardhat chai matchers by
yarn add --dev #nomicfoundation/hardhat-chai-matchers
Now all you need to is import it in your hardhat config by:
import "#nomicfoundation/hardhat-chai-matchers";
or
require("#nomicfoundation/hardhat-chai-matchers")
Now you will be able to use reverted and other stuff.

Related

Why aren't some of my Junit classes being run?

I have a problem using Junit tests with Java and Eclipse. All of my tests run just fine when I invoke them standalone. By this I mean that HDLmTreeTest, HDLmTreeTest1, and HDLmTreeTest2 all run fine when I run them by themselves. However, I have a Java source module with all of the test files in it. See below.
package com.headlamp;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
#RunWith(JUnitPlatform.class)
#SelectClasses({HDLmConfigTest.class, HDLmDefinesTest.class,
HDLmErrorTest.class, HDLmStringTest.class, HDLmTreeTest.class,
HDLmTreeTest1.class, HDLmTreeTest2.class,
HDLmModTest.class, HDLmBuildJSTest.class, HDLmFindTest.class,
HDLmAssertTest.class, HDLmBuildLinesTest.class, HDLmUtilityTest.class,
HDLmSavedChangeTest.class, HDLmCurlApacheTest.class, HDLmMainTest.class,
HDLmJettyTest.class, HDLmCurlJettyTest.class, HDLmEditorServletTest.class,
HDLmApacheTest.class, HDLmProxyTest.class, HDLmSessionTest.class,
HDLmLogMsgTest.class, HDLmMatchTest.class, HDLmImageInformationTest.class,
HDLmClusteringTest.class, HDLmJsonTest.class})
class HDLmAllTests { }
When I run this file, all of my tests get invoked except for HDLmTreeTest1.class and HDLmTreeTest2.class. I should say that HDLmTreeTest1.class and HDLmTreeTest2.class were just recently created and added to the SelectClasses list. For some reason, they are not invoked, but all of the other classes are invoked. What am I doing wrong?
I figured this out (with lots of online help). The problem was the name(s) of my test classes. Test classes (the names of test classes) must end with 'Test' or 'Tests'. They can not end with 'Test1' or 'Test2'. That was my error. I changed the class names and the problem went away. See https://howtodoinjava.com/junit5/junit5-test-suites-examples/ for some details.

Attempted import error: 'useRouterMatch' is not exported from 'react-router-dom'

I have copied the react router "2nd Example: Nested Routing" example from the react-router docs in to my app as-is. (The "1st Example: Basic Routing" works fine)
The error I'm getting is Attempted import error: 'useRouterMatch' is not exported from 'react-router-dom'
The example references useRouterMatch but it doesn't seem to be exported and searching google for 'useRouterMatch' returns no results outside of the example so I'm having a hard time finding docs or anything.
Here are all my react-router dependencies from package.json:
"#types/react-router": "^5.1.1",
"#types/react-router-bootstrap": "^0.24.5",
"#types/react-router-dom": "^4.3.5",
"react-router-bootstrap": "^0.24.4",
"react-router-dom": "^5.1.2",
Turns out there were a couple of issues here.
First, there seems to be a typo in the docs (which I've submitted a PR for)
Second, I probably should have mentioned I'm using Typescript, and after reading a couple of threads, I realized that while I had the latest react-router-dom package, I didn't have the latest types. Updating that package and changing useRouterMatch to useRouteMatch seems to have fixed the problem.

how to import function in angular2 using angular-cli?

I've previously used the following code
import { clone } from '../../utilities/javascript';
and in the javaccript.ts
export function clone(source: any) {...}
In my module i made the call using the following syntax:
this.x=clone(y);
And all was working.
However, since I am using Angular-cli, the following exception comes up in Chrome:
error_handler.js:47 EXCEPTION: Cannot read property 'clone' of undefined
Is there something in my syntax that is wrong, which surfaced just as I switched to Angular-cli?
I currently copy this function into every module in which i use it, which solves the problem, but that is not something I am comfortable with.
try to import it in the following way:
import * as javascriptUtils from '../../utilities/javascript';
then call it with:
this.x=javascriptUtils.clone(y);
taken from https://github.com/angular/angular-cli#3rd-party-library-installation

"Assert in junit.framework has been deprecated" - what next to use?

I bump version of junit to 4.11 and get:
[WARNING] [deprecation] Assert in junit.framework has been deprecated
[WARNING] [deprecation] Assert in junit.framework has been deprecated
....
How and to what migrate?
As it seems the Assert class has been moved from junit.framework to org.junit.Assert in JUnit 4.0 - you can use that instead, it's not deprecated.
Change your import statement from
import junit.framework.Assert;
to
import org.junit.Assert;
and this will rectify your JUnit deprecation warnings.
Both are depricated:
junit.framework.Assert.assertThat
org.junit.Assert.assertThat
According to docs, use Instead:
org.hamcrest.MatcherAssert.assertThat
After facing this problem I have tried lots of ways to solve this but failed again and again.
The good thing is: I have download junit-4.12.jar file from here and added the jar file in the project section under the libs folder. If previously any kind of Junit dependancy exist in the project then remove that from the build.gradle and build + clean your project.
It is worked for me. Hope it will work for you.
Note: Take a look in the image that I attached in below.
Thank you
We had a large number of tests with many assertions.
Adding something like
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
to the import statements also helped to limit the changes in test code.
You can refer to jUnit4 Assert class methods from JUnit4

Using JUnit in Jython - NameError for assertTrue

Environment Details
Mac OS X 10.9
Oracle JDK 1.7.0_55 64-bit
jython-standalone-2.5.3.jar
junit-4.11
What I have done so far
I have added the junit jar to /Library/Java/Extensions.
I invoked Jython as follows java -jar jython-standalone-2.5.3.jar
In the Jython interpreter, I imported the following import org.junit.Assert, and this import was successful.
Problem
When I tried to use assertTrue, I got a NameError in the interpreter. Why is this so?
I understand that assertTrue is a static method. Not sure what implication this has when I try to use it in Jython.
Additional Context
I am using XMLUnit in Jython. Was able to successfully import the Diff class from org.custommonkey.xmlunit in Jython. Also able to use the methods in this class, and call them on a Diff object. The result of this method call is what I am trying to pass to assertTrue, when it throws the error.
from org.custommonkey.xmlunit import Diff
import org.junit.Assert
xml1 = ...some XML string...
xml2 = ...some XML string...
myDiff = Diff(xml1, xml2)
assertTrue(myDiff.similar())
Hope this additional information is useful in identifying a solution to this problem.
Latest Status
I narrowed it down to setting this property python.security.respectJavaAccessibility = false, since the Assert() constructor is protected.
Still trying to get it to work. Any help is greatly appreciated.
Figured it out.
In addition to junit.jar file, the hamcrest-core.jar file also needed to be copied to /Library/Java/Extensions.
Then I got rid of the jython.jar file, and instead installed it using the jython installer.
After the installation was completed, I updated the registry file in the installation folder, specifically setting this property python.security.respectJavaAccessibility = false.
Now I am able to see the assertTrue method, and no longer getting a NameError.