anilhelvaci / dapp-pool-lending-protocol

The core of the Agoric economy
9 stars 1 forks source link

Inconsistent Behavior When Preparing Ava Environment #49

Open anilhelvaci opened 1 year ago

anilhelvaci commented 1 year ago

What is the best way to setup test environment with ses and ava?

Important !!! Don't forget to checkout bugfix/ava-ses-env branch

When I was trying to find an answer to this question I've come across two main ways to setup ava;

  1. import '@agoric/zoe/tools/prepare-test-env.js';
    import test from 'ava';
  2. import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
    • I also saw a prepare-test-env-ava.js at the notifier package which is similar to the one from zoe. I'll drop it's content anyway.

      // @ts-check
      import '@endo/init';
      
      import { wrapTest } from '@endo/ses-ava';
      import rawTest from 'ava';
      
      export const test = wrapTest(rawTest);

Environement

Problem

I can only make my tests work following the first approach but I'm concerned that I might be missing somethings by not making the tests work with approach two and it's sub-approach.

In order to narrow down the problem I've prepared a small test with multiple ava environments. The actual code is here.

// @ts-check

// import { test } from '@agoric/notifier/test/prepare-test-env-ava.js';
// import { test } from './prepare-test-env-ava.js';

import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';

test('ses-ava', async t => {
    const test = harden({
        hello: 'hello',
    });

    t.log(test);
    t.is('is', 'is');
});

This is the command I use to run the test;

cd dapp-pool-lending-protocol
npx ava -v contract/test/avaSesEnv/test-avaSesEnv.js -m ses-ava

Below are the results;

  1. When we fetch initiate test environment importing from import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js'; the console output;

    Uncaught exception in contract/test/avaSesEnv/test-avaSesEnv.js
    
    AssertionError [ERR_ASSERTION] [ERR_ASSERTION]: The expression evaluated to a falsy value:
    
    assert(refs.runnerChain)
    
    at async Promise.all (index 0)
    
    SES_UNHANDLED_REJECTION: (AssertionError#1)
     ✖ contract/test/avaSesEnv/test-avaSesEnv.js exited with a non-zero exit code: 1
     ✖ Couldn’t find any matching tests
    
  2. When we fetch initiate test environment importing from import { test } from '@agoric/notifier/test/prepare-test-env-ava.js'; the console output;

    Uncaught exception in contract/test/avaSesEnv/test-avaSesEnv.js
    
    SES_UNHANDLED_REJECTION: (AssertionError#1)
    ✖ contract/test/avaSesEnv/test-avaSesEnv.js exited with a non-zero exit code: 1
    ✖ Couldn’t find any matching tests
  3. When I copy/paste the contents of prepare-test-env-ava.js to a file in the same directory and import it like this import { test } from './prepare-test-env-ava.js'; the console output is;

     Uncaught exception in contract/test/avaSesEnv/test-avaSesEnv.js
    
    ✖ contract/test/avaSesEnv/test-avaSesEnv.js exited with a non-zero exit code: 1
    ✖ Couldn’t find any matching tests
    
  4. And if I do an import like this;
    import '@agoric/zoe/tools/prepare-test-env.js';
    import test from 'ava';

    The console output;

     ✔ ses-ava
    ℹ {
        hello: 'hello',
      }
       ─

    Seems to be working.

It's seems very strange to me that I get different outputs every time I use different imports. What do you think? @dckc

dckc commented 1 year ago

When I agoric init a new project, the contract/test/test-contract.js file starts with:

import { test } from './prepare-test-env-ava.js';

where ./prepare-test-env-ava.js contains:

/**
 * Like prepare-test-env but also sets up ses-ava and provides
 * the ses-ava `test` function to be used as if it is the ava
 * `test` function.
 */

import '@endo/init/pre-bundle-source.js';
import '@agoric/zoe/tools/prepare-test-env.js';
// eslint-disable-next-line import/no-unresolved -- https://github.com/avajs/ava/issues/2951
import rawTest from 'ava';

// XXX wrapTest not working https://github.com/endojs/endo/issues/1235
// import { wrapTest } from '@endo/ses-ava';

export const test = rawTest;

Try that?

anilhelvaci commented 1 year ago

So import { wrapTest } from '@endo/ses-ava'; is not mandatory? @dckc

I observed that wrapTest fails in my dapp but I'm able to run tests that use it. What could be causing that?