question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use golang fuzzing-like api for seeds instead of requiring duplicating prop tests

See original GitHub issue

💡 Idea

Currently if you want to check a prop test seed in, you need to duplicate the prop test to keep the unit test plus the prop test.

It’d be great if there was an api like go’s F.add() api that allowed you to provide seed test cases that are tested in addition to properties being tested.

see https://tip.golang.org/doc/tutorial/fuzz#fuzz_test

func FuzzReverse(f *testing.F) {
    testcases := []string{"Hello, world", " ", "!12345"}
    for _, tc := range testcases {
        f.Add(tc)  // Use f.Add to provide a seed corpus
    }
    f.Fuzz(func(t *testing.T, orig string) {
        rev := Reverse(orig)
        doubleRev := Reverse(rev)
        if orig != doubleRev {
            t.Errorf("Before: %q, after: %q", orig, doubleRev)
        }
        if utf8.ValidString(orig) && !utf8.ValidString(rev) {
            t.Errorf("Reverse produced invalid UTF-8 string %q", rev)
        }
    })
}

Motivation

this would allow minimizing LOC for a property test while allowing for both property testing and checking in old found bugs

Example

A prototype api might be:

it("should add properties successfully", async () => {
    expect(
      await overrideSvgAttributes("<SVG></SVG>", {
        height: null,
        width: null,
        "aria-hidden": true,
        "aria-label": null,
      })
    ).toBe('<svg aria-hidden="true"></svg>');

    const svgArbitrary = fc
      .record({
        start: fc.mixedCase(fc.constantFrom("<svg>")),
        middle: fc.fullUnicodeString(),
        end: fc.mixedCase(fc.constantFrom("</svg>")),
      })
      .map(({ start, middle, end }) => `${start}${middle}${end}`);

    await fc.assert(
      fc.asyncProperty(
        svgArbitrary,
        fc.record<SVGAttributes>({
          height: fc.option(fc.integer()),
          width: fc.option(fc.integer()),
          "aria-hidden": fc.option(fc.boolean()),
          "aria-label": fc.option(fc.string()),
        }),
        fc.context(),
        async (svgSource, overrides, ctx) => {
          ctx.log(svgSource);
          const transformedSource = await overrideSvgAttributes(
            svgSource,
            overrides
          );
          ctx.log(transformedSource);
          expect(transformedSource).toBeTruthy();
          // every truthy override should exist in the transformed source
          Object.entries(overrides)
            .filter(([, value]) => {
              ctx.log(`${value}, ${!!value}`);
              return !!value;
            })
            .forEach(([override]) => {
              expect(transformedSource).toContain(override);
            });
        }
      ),
	{
	seeds: ["<svG></svg>", {}]
    );
  });

more concisely:

fc.assert(fc.property(testCase()), { seeds: [...list of seeds] })

I don’t think that’s a great api, but something like it would be amazing

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jasikparkcommented, Oct 21, 2022

thx for working on all this again btw!

1reaction
jasikparkcommented, Oct 21, 2022

partial examples would be really funny, true

Read more comments on GitHub >

github_iconTop Results From Across the Web

Guarantee that an arbitrary produces at least the specified set ...
I use it a lot in both unit and end-to-end testing, and it's a cool experience ... Instead of numRuns , use shouldRunMore...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found