Unit testing tutorial, "artifacts.require is not a function" error

I'm trying to follow the OpenZeppelin tutorial on unit testing using Hardhat and Chai but when I try to follow the steps under "Performing complex assertions" on https://docs.openzeppelin.com/learn/writing-automated-tests#writing-unit-tests
I get a javascript error when trying to run "npx hardhat test"

"An unexpected error occurred:

TypeError: artifacts.require is not a function" on line 9 of Box.test.js as shown below

// test/Box.test.js
// Load dependencies
const { expect } = require('chai');

// Import utilities from Test Helpers
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');

// Load compiled artifacts
const Box = artifacts.require('Box');

// Start test block
contract('Box', function ([ owner, other ]) {
  // Use large integers ('big numbers')
  const value = new BN('42');

  beforeEach(async function () {
    this.box = await Box.new({ from: owner });

  it('retrieve returns a value previously stored', async function () {
    await this.box.store(value, { from: owner });

    // Use large integer comparisons
    expect(await this.box.retrieve()).to.be.bignumber.equal(value);

  it('store emits an event', async function () {
    const receipt = await this.box.store(value, { from: owner });

    // Test that a ValueChanged event was emitted with the new value
    expectEvent(receipt, 'ValueChanged', { value: value });

  it('non owner cannot store a value', async function () {
    // Test a transaction reverts
    await expectRevert(
      this.box.store(value, { from: other }),
      'Ownable: caller is not the owner',

Can no-one help with this? I have previously used artifacts.require successfully in Truffle, I notice the webpage I linked has an option to follow it's code in Truffle or Hardhat, perhaps it has not been updated correctly for Hardhat/my browser has broken it's functionality and it is displaying the Truffle instructions only, I will try it in Truffle and report back if that fixes it.

EDIT: Solved it myself, it appears you need to import some functions from Truffle to fix this, following https://hardhat.org/plugins/nomiclabs-hardhat-truffle4.html

1 Like

I had the same problem. I followed your solution and now I get the following error (I am also using hardhat):

Error: This method only supports Buffer but input was: 0xac0974bec39a17e36baa6b31d238ff912bacb478cbed5efcae784d7bf4f2ff80
    at Object.exports.assertIsBuffer (/Users/joaoabrantis/crypto/node_modules/ethereumjs-util/src/helpers.ts:21:11)
    at Object.exports.privateToPublic (/Users/joaoabrantis/crypto/node_modules/ethereumjs-util/src/account.ts:275:3)
    at exports.privateToAddress (/Users/joaoabrantis/crypto/node_modules/ethereumjs-util/src/account.ts:285:26)
    at /Users/joaoabrantis/crypto/node_modules/@nomiclabs/hardhat-truffle4/src/index.ts:62:36
    at Array.map (<anonymous>)
    at describeContract (/Users/joaoabrantis/crypto/node_modules/@nomiclabs/hardhat-truffle4/src/index.ts:62:11)
    at /Users/joaoabrantis/crypto/node_modules/@nomiclabs/hardhat-truffle4/src/index.ts:83:20
    at Object.<anonymous> (/Users/joaoabrantis/crypto/test/Box.test.js:12:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at /Users/joaoabrantis/crypto/node_modules/mocha/lib/mocha.js:349:36
    at Array.forEach (<anonymous>)

Any ideas?

You need to convert your string into something of type Buffer.

This appears to be a problem in @nomiclabs/hardhat-truffle5 package:

see https://github.com/nomiclabs/hardhat/issues/1885