Skip to content

Commit

Permalink
Fix flaky test-cases in HtmlFormatterTest (#2939)
Browse files Browse the repository at this point in the history
Co-authored-by: M.P. Korstanje <[email protected]>
  • Loading branch information
hermya and mpkorstanje authored Nov 15, 2024
1 parent d83b51b commit 1dca05d
Showing 1 changed file with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import java.time.Clock;
import java.util.Collections;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.cucumber.core.plugin.Bytes.bytes;
import static org.hamcrest.CoreMatchers.containsString;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;

class HtmlFormatterTest {

Expand All @@ -38,11 +41,11 @@ void writes_index_html() throws Throwable {
TestRunFinished testRunFinished = new TestRunFinished(null, true, new Timestamp(15L, 0L), null);
bus.send(Envelope.of(testRunFinished));

assertThat(bytes, bytes(containsString("" +
"window.CUCUMBER_MESSAGES = [" +
"{\"testRunStarted\":{\"timestamp\":{\"seconds\":10,\"nanos\":0}}}," +
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"seconds\":15,\"nanos\":0}}}" +
"];\n")));
assertEquals("[" +
"{\"testRunStarted\":{\"timestamp\":{\"nanos\":0,\"seconds\":10}}}," +
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"nanos\":0,\"seconds\":15}}}" +
"]",
extractCucumberMessages(bytes), STRICT);
}

@Test
Expand Down Expand Up @@ -86,11 +89,17 @@ void ignores_step_definitions() throws Throwable {
null);
bus.send(Envelope.of(testRunFinished));

assertThat(bytes, bytes(containsString("" +
"window.CUCUMBER_MESSAGES = [" +
"{\"testRunStarted\":{\"timestamp\":{\"seconds\":10,\"nanos\":0}}}," +
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"seconds\":15,\"nanos\":0}}}" +
"];\n")));
assertEquals("[" +
"{\"testRunStarted\":{\"timestamp\":{\"nanos\":0,\"seconds\":10}}}," +
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"nanos\":0,\"seconds\":15}}}" +
"]",
extractCucumberMessages(bytes), STRICT);
}

private static String extractCucumberMessages(ByteArrayOutputStream bytes) {
Pattern pattern = Pattern.compile("^.*window\\.CUCUMBER_MESSAGES = (\\[.+]);.*$", Pattern.DOTALL);
Matcher matcher = pattern.matcher(new String(bytes.toByteArray(), UTF_8));
assertThat("bytes must match " + pattern, matcher.find());
return matcher.group(1);
}
}

0 comments on commit 1dca05d

Please sign in to comment.