fake_async icon indicating copy to clipboard operation
fake_async copied to clipboard

Why does making the callback function async break expect?

Open CorvetteCole opened this issue 3 years ago • 7 comments

I think I just don't understand this, but why does this test fail (as expected):

test('test smoke test -- this test should fail', () {
  fakeAsync((async) {
    expect(true, isFalse);
  });
});

But this one passes:

test('test smoke test -- this test should fail', () async {
  fakeAsync((async) async {
    expect(true, isFalse);
  });
});

I don't understand this behavior, and I think it is the root of several bugs within my tests

CorvetteCole avatar Sep 15 '22 17:09 CorvetteCole

I've got the same question – what's the cause of this behavior?

Here's my example. I expect the below test to pass, but it times out instead.

import 'package:fake_async/fake_async.dart';
import 'package:test/test.dart';

void main() {
  test('test smoke test -- this test should pass', () async {
    await fakeAsync((async) async {
      final future = doWork();
      async.elapse(const Duration(seconds: 2));
      final value = await future;
      expect(value, 1);
    });
  });
}

Future<int> doWork() async {
  await Future<void>.delayed(const Duration(seconds: 1));

  return 1;
}
$ dart test main.dart
00:30 +0 -1: test smoke test -- this test should pass [E]                                                                                   
  TimeoutException after 0:00:30.000000: Test timed out after 30 seconds. See https://pub.dev/packages/test#timeouts
  dart:isolate  _RawReceivePortImpl._handleMessage
  

To run this test again: /Users/bartek/fvm/versions/stable/bin/cache/dart-sdk/bin/dart test main.dart -p vm --plain-name 'test smoke test -- this test should pass'
00:30 +0 -1: Some tests failed.                                                                                                             

Consider enabling the flag chain-stack-traces to receive more detailed exceptions.
For example, 'dart test --chain-stack-traces'.

bartekpacia avatar Nov 18 '22 15:11 bartekpacia

This appears to be the same issue as #38, and can probably be closed as a duplicate.

(I have the same question, and am curious about the answer!)

gnprice avatar May 16 '23 22:05 gnprice

I like that this issue has code that can be copy-pasted to reproduce the problem.

bartekpacia avatar May 17 '23 12:05 bartekpacia

Sure. The fix for that is to copy the information over into a single thread :slightly_smiling_face: — done as https://github.com/dart-lang/fake_async/issues/38#issuecomment-1551771957 .

gnprice avatar May 17 '23 17:05 gnprice

Haha nice :) this one can be closed now.

bartekpacia avatar May 17 '23 19:05 bartekpacia

I can close this issue. It's been quite a long time though, I am unsure the dart team is interested in addressing this

CorvetteCole avatar May 17 '23 20:05 CorvetteCole

@CorvetteCole That's a good idea, everything is already in #38.

bartekpacia avatar Jun 02 '23 11:06 bartekpacia