rrousselGit/expect_error is one of the open-source repositories TopGit tracks, currently at 42 stars, written primarily in Dart. A Dart testing utility for asserting that some code emits a compilation error.
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
The expect_error package is a testing library inspired by Typescript's
// @expect-error, designed to help package authors to test compilation errors.
Usage
expect_error exposes a compiles matcher, which can be used within unit tests.
A simple example would be:
// test/my_test.dart
import 'package:expect_error/expect_error.dart';
import 'package:test/test.dart';
void main() async {
final library = await Library.parseFromStacktrace();
test('String is not assignable to int', () async {
await expectLater(library.withCode('''
// expect-error: invalid_assignment
int value = "string";
'''), compiles);
});
}
This example tests that the code:
int value = "string";
emits the compilation error "invalid_assignment".
FAQ: Why use a comment in the code block instead of a matcher?
You may wonder why:
await expectLater(library.withCode('''
// expect-error: invalid_assignment
int value = "string";
'''), compiles);
is preferrable to:
await expectLater(library.withCode('''
int value = "string";
'''), throwsCompilationError('invalid_assignment'));
The reason why expect_error relies on a comment is because a comment
doesn't simply communicate what the error is, but also where that error is.
When using // expect-error: x, only the next line is allowed to emit the
compilation error.
As such, if we do:
await expectLater(library.withCode('''
void main() {
// expect-error: invalid_assignment
print('a');
int value = "string";
}
'''), compiles);
Then this test will fail. Because while the code block indeed contains an
INVALID_ASSIGMENT error, the error isn't on the print but instead on
the int value = 'string'.
To fix out test, we would have to move the comment on the line before the error:
await expectLater(library.withCode('''
void main() {
print('a');
// expect-error: invalid_assignment
int value = "string";
}
'''), compiles);
Specifying multiple error codes at the same time.
It is possible for expect-error to specify multiple codes at once by separating
them with a ,:
await expectLater(library.withCode(r'''
String fn(int a) => '';
// expect-error: not_enough_positional_arguments, invalid_assignment
int a = fn();
'''), compiles);
Importing files/packages in code blocks
It is possible to use import directives to import dart code within code blocks:
that library variable we created tells expect_error what imports
the tested code block can use.
In particular, Library.parseFromStacktrace() makes our tested code behave as if
it was defined in a separate file within the same folder our test file.
As such, the following code is valid too:
import 'package:expect_error/expect_error.dart';
import 'package:test/test.dart';
// our test file can use relative import to import another file
import 'relative.dart';
void main() async {
final library = await Library.parseFromStacktrace();
test('...', () async {
await expectLater(library.withCode('''
// our tested code can also import the same file
import 'relative.dart';
'''), compiles);
});
}
Flutter support
Unfortunately, since expect_error is built on top of the analyzer
package, it means that a package cannot both depend on Flutter and expect_error at
the same time.
As such, to use expect_error to test compilation errors when interacting with
Flutter, a workaround is necessary.
The solution is to move your tests that depends on Flutter in a separate package
used only for test purpose, with no dependency on Flutter.
As example, consider a package my_package that depends on Flutter, which exposes a
MyWidget class:
To test this MyWidget class, rather than adding our test
within the my_package/test folder, we could create a new Dart project
such that our folder architecture looks like:
my_package
puspec.yaml
lib
my_widget.dart
expect_error_test
pubspec.yaml
test
my_widget_test.dart
This expect_error_test app would depend on expect_error:
Then, within expect_error_test/test/my_widget_test, we could do:
void main() {
final flutterLibrary = await Library.custom(
packageName: 'my_package', // the name of the package that contains this code block
packageRoot: '..', // the path to the root of this package
path: 'test/my_test.dart', // where the codeblock is located within the package
);
await expectLater(flutterLibrary.withCode(r'''
import 'package:my_package/my_widget.dart';
void main() {
// expect-error: missing_required_argument
MyWidget();
}
'''), compiles);
}
Notice how rather than Library.parseFromStacktrace() we used Library.custom(...).
By doing so, rather than assuming that our code block is within the same
folder as our test, we were able to make it behave as if our code block was
within my_package.
This way, our tests using expect_error are correctly able to import Flutter code.
Does rrousselGit/expect_error have a project website?
No homepage URL was recorded for rrousselGit/expect_error in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
How many stars does rrousselGit/expect_error have?
rrousselGit/expect_error has 42 GitHub stars — refresh the page for the live number, or check github.com/rrousselGit/expect_error. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is rrousselGit/expect_error open source?
Yes — rrousselGit/expect_error ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/rrousselGit/expect_error.
What is rrousselGit/expect_error?
rrousselGit/expect_error (rrousselGit/expect_error) is a Dart project on GitHub. From the project's own README: A Dart testing utility for asserting that some code emits a compilation error.
Where do I read more about rrousselGit/expect_error?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/rrousselGit/expect_error is the definitive source.
Read full README in the tab above.
Curious whether expect_error is right for you?
Let ChatGPT, Claude, or Perplexity look into it — click below and see what AI actually says about expect_error.