Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Truffle test return TypeError: Cannot read property 'map' of undefined on specific case #4191

Closed
1 task done
FabienCoutant opened this issue Jul 19, 2021 · 17 comments
Closed
1 task done
Assignees

Comments

@FabienCoutant
Copy link

FabienCoutant commented Jul 19, 2021


Issue

When I run truffle test of npx truffle test I got the following error message:

Capture d’écran 2021-07-19 à 12 59 25

But! If I run truffle compile or truffle migrate I got no error.
And even stranger if I install solidity coverage and run npx truffle run coverage, all my tests running as expected.

After some days of investigation I found what causing this issue: I added into my contracts_build_directory others contract like USDC, etc. and this is what causing the error.

Steps to Reproduce

Here the steps:

  • Add another ABI.json like USDC.json in your directory defined by contracts_build_directory. In my case
    contracts_build_directory: path.join(__dirname, "client/src/contracts"),

Capture d’écran 2021-07-19 à 13 05 02

  • Add test file(s) in the test directory (it can be empty but need at least the contract() and one it() function):
const {expectRevert, expectEvent} = require("@openzeppelin/test-helpers");
const {expect} = require("chai");
const CampaignFactoryContract = artifacts.require("CampaignFactory");
const CampaignContract = artifacts.require("Campaign");

contract("Test", async (accounts) => {
	console.log("outside");
	it("should log owner", async () => {

		console.log("inside");
	});

});

In my cases I have my contract test files and a dummit test file (same code as above):
Capture d’écran 2021-07-19 à 13 06 51

  • Run truffle test or npx truffle test

Expected Behavior

I expect no error or at least a clear error message.

Actual Results

truffe test compile correctly the contract files and then throw an Error at the first it(). The error is not clear at all
Capture d’écran 2021-07-19 à 13 11 00

Environment

My co-worker on windows also have the issue.

  • Operating System: MacOS Big Sur v11.2.3
  • Ethereum client: Web3js v1.4.0
  • Truffle version (truffle version): v5.4.1
  • Solidity : v 0.8.6
  • node version (node --version): v15.11
  • npm version (npm --version): v7.19.1
@eggplantzzz
Copy link
Contributor

Currently Truffle and npm versions >7 don't play well together. What happens when you downgrade and use 6?

Actually it is not surprising that truffle migrate and truffle compile run successfully: the error is stating that something is failing while running the "before all" in your tests (which don't get run with these two commands).

@FabienCoutant
Copy link
Author

Hi there, didn't tried with npm v6.

If you run the dummy test describe in my post with only a it(); function inside. You will see in the console the console.log before the it() and then the error.
Anyway, the workaround is to put all externals ABI.json file into a sub directory.

@eggplantzzz
Copy link
Contributor

Oh, I'm glad you found a workaround. I cannot duplicate your error however, can you provide more specific test steps with the minimal project to recreate this? You really shouldn't be creating artifact files directly as those are the files Truffle creates. I mean, you can do it if you know what you are doing but if they have the wrong format somehow they can cause errors like this.

@vetal20600
Copy link

Had the same issue, at least the same title. The reason was corrupted file inside build/contracts directory cause by 3 party

@gnidan
Copy link
Contributor

gnidan commented Jul 28, 2021

@vetal20600 could you tell us which 3rd party utility caused this corruption? That sounds like something we should reach out to them about.

In the meantime, @FabienCoutant if you could get us those specific test steps, that'll help us understand what's going wrong.

Thanks all!

@vetal20600
Copy link

vetal20600 commented Aug 3, 2021

This utility packages called «truffle-deploy-registry»: https://www.npmjs.com/package/truffle-deploy-registry
I am using it to share deployed contract addresses for every new build by injecting deployed addresses information into networks section for specified artifact located at build/contracts/artifact.json path

The problem is that I have some obsolete artifacts not used anymore. In this case the utiliy still inject network property leaving other properties blank @gnidan

@FabienCoutant
Copy link
Author

Hi! Sorry for the delay
Here the step to reproduce it:

  • Run truffle unbox react to get the basic things (the error appear whatever the config)
  • Update the truffle-config into order to push the compile contract into client/src/contracts folder.
  • Add into the client/src/contracts the ABI of another contract (in my case ERC20)
  • Run truffle test
    =>The error appear
  • Move the ABI of the contract added into another place (sub folder for instance)
  • Run truffle test
    => No error

here a test repo : https://github.com/FabienCoutant/Truffle-test-issue

@gjols
Copy link

gjols commented Oct 27, 2021

I´ve just had the exact problem. Thanks to @FabienCoutant workaround, it´s now possible to run the tests! Was found a cause and solution for this issue? Tks!

@gnidan gnidan self-assigned this Nov 8, 2021
@gnidan
Copy link
Contributor

gnidan commented Dec 1, 2021

So it looks like the problem is that you're supplying Truffle with a raw ABI JSON as opposed to a Truffle contract artifact, which is what Truffle expects. I think you need to wrap the ABI with something like this: https://github.com/FabienCoutant/Truffle-test-issue/commit/387ca878e72fc4a88d64cf3195022a5f872c9372

Sorry for the confusion. Please let me know if this helps!

@haltman-at
Copy link
Contributor

Closing this for issue maintenance. Let us know if there's still a problem!

@olamideokunola
Copy link

Hi, I am having same problem. Putting the ABI file in the test folder or its subfolder as mentioned by @FabienCoutant did not work for me. Also, the ABI is correctly wrapped as mentioned by @gnidan.

`

 Contract: PaymentContract
1) "before all" hook: prepare suite for "should register vendor"

0 passing (1s)
1 failing

  1. Contract: PaymentContract
    "before all" hook: prepare suite for "should register vendor":
    TypeError: Cannot read property 'map' of undefined
`

@haltman-at
Copy link
Contributor

@olamideokunola Do you have a reproduction example we could inspect? Thanks!

@olamideokunola
Copy link

Steps to reproduce

  • Create a truffle project in a folder with truffle init

project/

  • Create artifacts folder outside the project folder to store the contract artifacts

../artifacts/

  • Folder hierarchy is like this:
--artifacts
|    |
|    artifact.json
|
--project
     |
     --contracts
     |    |
     |    MyContract.sol
     |
     --test
     |   |
     |  test.spec.js
     |
     --truffle.config.js
  • In truffle.config.js, set contracts_build_directory:
    contracts_build_directory: "../artifacts"

  • In test.spec.js,

let MyContract = artifacts.require("MyContract");

contract("MyContract", async accounts => {
    it("check address", async function () {
        let myContract = await MyContract.deployed();
   	assert.equal(myContract.address,'-KNOWN_ADDRESS-', "Incorrect address");
   });
}

  • Run the test and the error shows up
  • Moving artifact.json into the test folder does not solve the problem

Update

For now, I am using describe() function instead of contract() to run my tests

@haltman-at
Copy link
Contributor

Hm, so the error only occurs when the contracts build directory is outside of the project directory? Do I have that right? I'm not sure we support that case... @gnidan, can you comment on this?

@haltman-at
Copy link
Contributor

Hm, OK, @gnidan didn't respond here, but he says that putting the directory outside shouldn't be a problem, so I guess I'll take a look.

@haltman-at
Copy link
Contributor

OK, I can't reproduce this problem. Do you have a complete reproduction example we could check out on GitHub? Your example seems to be incompletely specified and it's possible that I filled in some of the gaps in the wrong way. (Also, you may want to check that you're using the latest version of Truffle.) Thanks!

@binkiebinkie
Copy link

I also encountered this problem, and what fixed it for me was moving the build directory. In truffle-config.js I changed the value
contracts_build_directory: "./src/ABIs/",
to
contracts_build_directory: "./abis/"
I don't know if moving the source directory out of /src/ was the fix or just generating completely new files, but it works now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants