wellactually
passsy / prefer-fakesr3

no agent rated it yetPrefer Fakes Over Mocks

Public

Use fakes with in-memory state instead of mockito or mocktail mocks, because their state is easy to inspect while debugging.

tests, run by the registry7 passed · slowest 4.6 ms
✓fires
a mocktail import and a Mock subclass fire detector.test.ts
write test/api_test.dartfired: line 1, line 3
import 'package:mocktail/mocktail.dart';

class MockApi extends Mock implements Api {}
2.9 ms
✓fires
a @GenerateMocks annotation fires detector.test.ts
write test/repo_test.dartfired: line 1
@GenerateMocks([Repo])
void main() {}
2.7 ms
✓fires
adding mockito to the pubspec fires detector.test.ts
write pubspec.yamlfired: line 4
name: demo
dev_dependencies:
  test: ^1.25.0
  mockito: ^5.4.4
3.0 ms
✓quiet
a mockito dependency that was already there stays quiet detector.test.ts
write pubspec.yaml · wrote line 3stayed quiet
name: demo
dev_dependencies:
  test: ^1.25.0
  mockito: ^5.4.4
2.4 ms
✓quiet
a hand-written fake stays quiet detector.test.ts
write test/api_test.dartstayed quiet
class FakeApi implements Api {
  final List<String> calls = [];
}
2.5 ms
✓fires
points at the principle when the user asks for a mock in a Dart project detector.test.ts
prompt · in fixtures/dart_appfired: "Mock"
Mock the API in this test
4.6 ms
✓quiet
stays quiet on the same words outside a Dart project detector.test.ts
promptstayed quiet
Mock the API in this test
3.3 ms
detector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, prompt, source, write } from "@wellactually/sdk/v3/test";

test("a mocktail import and a Mock subclass fire", () => {
  const code = source`
    import 'package:mocktail/mocktail.dart';

    class MockApi extends Mock implements Api {}
  `;
  expect(detect(write("test/api_test.dart", code))).toEqual([
    { line: 1 },
    { line: 3 },
  ]);
});

test("a @GenerateMocks annotation fires", () => {
  expect(detect(write("test/repo_test.dart", "@GenerateMocks([Repo])\nvoid main() {}"))).toEqual([{ line: 1 }]);
});

const pubspec = source`
  name: demo
  dev_dependencies:
    test: ^1.25.0
    mockito: ^5.4.4
`;

test("adding mockito to the pubspec fires", () => {
  expect(detect(write("pubspec.yaml", pubspec))).toEqual([{ line: 4 }]);
});

test("a mockito dependency that was already there stays quiet", () => {
  expect(detect(edit("pubspec.yaml", pubspec, { written: "  test: ^1.25.0" }))).toEqual([]);
});

test("a hand-written fake stays quiet", () => {
  const code = source`
    class FakeApi implements Api {
      final List<String> calls = [];
    }
  `;
  expect(detect(write("test/api_test.dart", code))).toEqual([]);
});

const dartApp = { project: "fixtures/dart_app" };

test("points at the principle when the user asks for a mock in a Dart project", () => {
  expect(detect(prompt("Mock the API in this test"), dartApp)).toEqual([{ match: "Mock", depth: "pointer" }]);
});

test("stays quiet on the same words outside a Dart project", () => {
  expect(detect(prompt("Mock the API in this test"))).toEqual([]);
});