I'm trying to check if my event "ItemListed" is being emit properly.
Here is my solidity code:
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
error Marketplace__PriceIsZero();
contract Marketplace is ReentrancyGuard {
address payable public immutable feeAccount;
uint256 public immutable feePercent;
uint256 public itemCount;
struct Item {
uint256 itemId;
uint256 tokenId;
uint256 price;
IERC721 nft;
address payable seller;
bool sold;
}
event ItemListed(
uint256 itemId,
uint256 tokenId,
uint256 price,
address indexed nft,
address indexed seller
);
mapping(uint256 => Item) public items;
constructor(uint256 _feePercent) {
feeAccount = payable(msg.sender);
feePercent = _feePercent;
}
function listItem(
IERC721 _nft,
uint256 _tokenId,
uint256 _price
) external nonReentrant {
if (_price <= 0) {
revert Marketplace__PriceIsZero();
}
_nft.transferFrom(msg.sender, address(this), _tokenId);
items[itemCount] = Item(
itemCount,
_tokenId,
_price,
_nft,
payable(msg.sender),
false
);
emit ItemListed(itemCount, _tokenId, _price, address(_nft), msg.sender);
}
}
And here is the test script:
const { expect } = require("chai")
const toWei = (num) => ethers.utils.parseEther(num.toString())
describe("NFTMarketplace", function () {
let feePercent = 1
let nft, marketplace, deployer, addr1, addr2
let URI = "SampleURI"
beforeEach(async () => {
const NFTFactory = await ethers.getContractFactory("NFT")
const MarketplaceFactory = await ethers.getContractFactory(
"Marketplace"
)
const accounts = await ethers.getSigners()
deployer = accounts[0]
addr1 = accounts[1]
addr2 = accounts[2]
nft = await NFTFactory.deploy()
marketplace = await MarketplaceFactory.deploy(feePercent)
})
describe("Deployment", function () {
it("Should track name and symbol of the nft collection", async () => {
expect(await nft.name(), "DApp NFT")
expect(await nft.symbol(), "DAPP")
})
it("Should feeAccount and feePercent of the marketplace", async () => {
expect(await marketplace.feeAccount(), deployer.address)
expect(await marketplace.feePercent(), feePercent)
})
})
describe("Minting NFTs", function () {
it("Should track each minted NFT", async () => {
await nft.connect(addr1).mint(URI)
expect(await nft.tokenCount(), 1)
expect(await nft.balanceOf(addr1.address), 1)
expect(await nft.tokenURI(1), URI)
await nft.connect(addr2).mint(URI)
expect(await nft.tokenCount(), 2)
expect(await nft.balanceOf(addr2.address), 1)
expect(await nft.tokenURI(2), URI)
})
})
describe("Creating items", function () {
beforeEach(async function () {
await nft.connect(addr1).mint(URI)
await nft
.connect(addr1)
.setApprovalForAll(marketplace.address, true)
})
it("Should track created items, transfer NFT from seller to marketplace and emit an event", async () => {
await expect(
marketplace.connect(addr1).listItem(nft.address, 1, toWei(1))
)
.to.emit(marketplace, "ItemListed")
.withArgs(1, 1, toWei(1), nft.address, addr1.address)
})
})
})
I get the issue at the final "it" (Should track created items, transfer NFT from seller to marketplace and emit an event) on the test script. Here is the error:
Error: Invalid Chai property: emit. Did you mean "exist"?
Anyone having the same problem?