Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to access logs in sendTransaction #68

Closed
janhesters opened this issue Dec 9, 2021 · 0 comments · Fixed by #170
Closed

Add the ability to access logs in sendTransaction #68

janhesters opened this issue Dec 9, 2021 · 0 comments · Fixed by #170
Assignees
Labels
p-med Medium Priority

Comments

@janhesters
Copy link

Issue To Be Solved

When you log inside a transaction there is no easy way to access those logs inside of your code / tests.

(Optional): Suggest A Solution

Solution 1: add logs to the return value of sendTransaction

Currently, sendTransaction returns an object with 4 keys.

type ReturnValue = {
  status: number;
  statusCode: number;
  errorMessage: string;
  events: Events[];
}

We could add a fifth value logs

type ReturnValue = {
  // ...
  logs: string[];
}

Solution 2: use spyOn on console.log

You can spy on console.log after activating emulator.start to read logs in tests:

it('given a transaction: logs out the signers', async () => {
  const message = 'Hello from Emulator';
  const Alice = await getAccountAddress('Alice');
  const Bob = await getAccountAddress('Bob');
  const signers = [Alice, Bob];

  const consoleSpy = jest
    .spyOn(console, 'log')
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    .mockImplementation(() => {});
  emulator.setLogging(true);

  await sendTransaction({
    name: 'log-signers',
    args: [message],
    signers,
  });

  const transactionLogs = consoleSpy.mock.calls.toString();
  consoleSpy.mockRestore();

  const actual = [
    transactionLogs.includes(message),
    transactionLogs.includes(Alice),
    transactionLogs.includes(Bob),
  ];
  const expected = [true, true, true];

  expect(actual).toEqual(expected);
});

Trade-offs between 1 and 2

Solution 2 works already (except for a bug that malformats addresses in logs).

Solution 1 would be more clean and doesn't need extensive knowledge of your testing framework. However, it would require additional dev work to implement. It would also make debugging easier.

(Optional): Context

I'm writing tests for a project to get familiar with Flow. In the future these tests will assert program correctness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p-med Medium Priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants