goldenrod

Golden-file expectations and test extensions for Dart and Flutter.


License
MIT

Documentation

Goldenrod

Goldenrod is a simple set of matchers that enables simple golden-file based String assertions for Dart and Flutter testing.

Binary on pub.dev Code coverage Github action status Dartdocs Style guide

Ever tired of writing tests like?

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

void main() {
  test('should output a pretty string', () {
    expect(shrugEmoji(), '¯\_(ツ)_/¯');
  });
}

... and needing to manually update the assertions when you update your app? Automate it using golden-file based matches and package:goldenrod!

NOTE: This package assumes access to File I/O using dart:io, and will not work properly (or at all) in read-only file systems, or in environments where dart:io is not available (such as on the web).

Add @TestOn('vm') to the top of your tests that use package:goldenrod!

@TestOn('vm')
import 'package:app/app.dart';
import 'package:goldenrod/goldenrod.dart';
import 'package:test/test.dart';

void main() {
  test('should output a pretty string', () async {
    expect(shrugEmoji(), await matchesGoldenText(file: 'test/shrug.golden'));
  });
}

To update goldens pass the environment variable GOLDENROD_UPDATE=true:

GOLDENROD_UPDATE=true pub run test

matchesGoldenText

Asserts that the actual String matches the contents of the file specified:

// test/foo_test.dart
@TestOn('vm')
import 'package:app/app.dart';
import 'package:goldenrod/goldenrod.dart';
import 'package:test/test.dart';

void main() {
  test('some computation is stable', () async {
    expect(fooString(), await matchesGoldenText(file: 'test/foo_test.golden'));
  });
}

matchesGoldenKey

Asserts than the actual String matches the value of the JSON file/key:

// test/foo_test.dart
@TestOn('vm')
import 'package:app/app.dart';
import 'package:goldenrod/goldenrod.dart';
import 'package:test/test.dart';

void main() {
  test('some computation is stable', () async {
    expect(a(), await matchesGoldenKey(file: 'test/foo_test.golden', key: 'a'));
    expect(b(), await matchesGoldenKey(file: 'test/foo_test.golden', key: 'b'));
  });
}