Truffle Unit test and OZ

I set up a unit test on a SimpleStorage that works with OZ Hub and relay running on a ganache instance I get 0 passing. I tried the test on a nonOZ working version of my Solidity code and I get errors :

import { assert } from 'chai'
       ^
SyntaxError: Unexpected token {

my test file:

import { assert } from 'chai'
import { shallowMount } from '@vue/test-utils'
const MyContract = artifacts.require('SimpleStorage')


contract("MyContract", accounts => {
        let instance
        before(async () => {
          await instance = MyContract.deployed()
            }
           it("should set and get the same value", async () =>
             await instance.set('88', {from:accounts[0]})
             .then(() => instance.get()).then((r) => {
             assert.equal(88,"the values don't match");
             })

      )
   )}
)

There are several issues I need help with:

  1. Does the plain truffle test of contract handle the OZ relay and hub?
  2. Why does my truffle test on my regular non OZ version throw syntax errors on { } import?
1 Like

Hey @Steeve! If you’re running your tests via truffle test, you may need to somehow have Babel parse your js code (if you’re using an old node.js). This is typically done by adding the following to your truffle config file:

require('babel-register');
require('babel-polyfill');

Note that this is specific to truffle though, as OZ does not play a part when running truffle test.

1 Like

Did that ,and made sure the babel dependencies are in the package.json also
however I get a polyfill error (may be related to Vue framework conflicts):

TypeError: Cannot destructure property `polyfills` of 'undefined' or 'null'. (While processing preset: "/home/workspace/SimpleStorage/node_modules/@vue/babel-preset-app/index.js")
    at module.exports (/home/workspace/SimpleStorage/node_modules/@vue/babel-preset-app/polyfillsPlugin.js:19:5)
1 Like

Hi @Steeve,

Were you able to resolve this?

Yes , after attempting to go with OZ testing I solved by sticking with Truffle test “contract” framework and plain mocha/chai for function logic unit testing.

//const { BN, constants, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
var assert = require('chai').assert
require("babel-register");

const SimpleStorage = artifacts.require("SimpleStorage");
contract("contract test", accounts => {
         beforeEach(async function () {
         });

         it("sets value equal to expected value", async () => {
           let val = 88
           let instance = await SimpleStorage.deployed()
           await instance.set(val)
           await instance.get().then((r) => {
           assert.equal(val,r.toNumber());
         })
      })  
    });
 })

describe("Tests", function () {
it('does something', () => {
        const msg = 'new message'
        assert.equal(msg, 'new message','ok')
        })});

1 Like