Testing - TypeError: Cannot destructure property 'events' of 'e' as it is null

Hello, I’m new to Flow and I’m trying to write a very simple test.
But when I try to use const Alice = await getAccountAddress('alice')
I’m getting the error: "TypeError: Cannot destructure property ‘events’ of ‘e’ as it is null"

My test is very simple. So I’m probably missing something but I don’t know what

Here is my test:


import path from "path";
import { emulator, init, getAccountAddress } from "flow-js-testing";

// Increase timeout if your tests failing due to timeout
jest.setTimeout(10000);

describe("Token", () => {
    beforeEach(async () => {
        const basePath = path.resolve(__dirname, "../cadence");
        // You can specify different port to parallelize execution of describe blocks
        const port = 8888;
        // Setting logging flag to true will pipe emulator output to console
        const logging = false;

        await init(basePath, { port });
        return emulator.start(port, logging);
    });

    // Stop emulator, so it could be restarted
    afterEach(async () => {
        return emulator.stop();
    })

    test("Alice should exist", async () => {
        const Alice = await getAccountAddress('alice')
        expect(Alice).toBeDefined()
    })
});

The error:

› Alice should exist

    TypeError: Cannot destructure property 'events' of 'e' as it is null.

      23 |
      24 |     test("Alice should exist", async () => {
    > 25 |         const Alice = await getAccountAddress('alice')
         |                       ^
      26 |         expect(Alice).toBeDefined()
      27 |     })
      28 | });

      at events (node_modules/flow-js-testing/src/account.js:64:19)
      at Object.<anonymous> (cadence/tests/ZooToken.test.ts:25:23)

do you have flow.json in project root?

I often see this error if I have another instance of the Flow emulator running - check that you don’t have another terminal open with the emulator running inside of it. If you do, shut that one down and run your test again

I have generated my testing suite with npx flow-js-testing init.
I have a flow.json file at my project’s root:

	"emulators": {
		"default": {
			"port": 3569,
			"serviceAccount": "emulator-account"
		}
	},
	"contracts": {
		"ZooToken": "./cadence/contracts/ZooToken.sol",
		"FungibleToken": "./cadence/contracts/FungibleToken.sol"
	},
	"networks": {
		"emulator": "127.0.0.1:3569",
		"mainnet": "access.mainnet.nodes.onflow.org:9000",
		"testnet": "access.devnet.nodes.onflow.org:9000"
	},
	"accounts": {
		"emulator-account": {
			"address": "f8d6e0586b0a20c7",
			"key": "0cea21d8c33b62bd84d5684a2630406b79ed7330558058e1b933b9178382be4e"
		},
		"alice": {
			"address": "179b6b1cb6755e31",
			"key": "bfa4823cec2f11864585124c9ea5d7e7832bd2bb6ce29e3db5f2ae6763a82086"
		}
	},
	"deployments": {
		"emulator-account": ["FungibleToken", "ZooToken"]
	}
}

And I don’t have a running emulator. I’m just running jest.

My test fail only if I add this line: const Alice = await getAccountAddress('alice'). It looks like the function cannot create a new account

Sorry only today had time to debug, just change port number in test from 8888, it is used by REST API on emulator now. 8080 etc should work.

I tried to switch to the port 8088, and verified that it’s not used and still have the same issue

another option can be flow-cli version, this error is mostly related when Emulator is not running on that selected port etc.

you can try to verify but watching ps ax | grep flow and catching launch arguments (quickest way) and trying to run with that arguments to see the error.