Help with testing AccessControl in an ERC20 contract

I am using test-environment and test-helpers in a Mocha test suite and I cannot figure out how to interact with a role from my tests.

In my smart contract I setup the WHITELISTED role:

  bytes32 public constant WHITELISTED = keccak256("WHITELISTED");

  constructor(uint256 _initialSupply) ERC20PresetMinterPauser("ExperimenDAO", "EXD") {
    _setupRole(WHITELISTED, msg.sender);
    _mint(msg.sender, _initialSupply * 10 ** 18);

in my test suite I am trying to set an account to have the WHITELISTED role:

  beforeEach(async function() {
    this.experimenDAOToken = await, { from: owner });
    await this.experimenDAOToken.grantRole(this.experimenDAOToken.WHITELISTED, whitelisted1);

With setup I am getting errors when I call grantRole() and I cannot figure out what's going on. Any thoughts? Thanks!

To set up a role with an internal function _setupRole is different from to do the same with a public function grantRole which requires that the caller must be the admin of the role that is being granted. See the modifier in the function below.

You may do two calls before calling grantRole: one is to _setRoleAdmin to set up the adminRole of role and the other is to _setupRole to setup owner as the adminRole. After calling these two properly, you can call grantRole as you did.

    function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
        _grantRole(role, account);