Coverage and entry points
Rules answer “is this page sound”. The two checks here answer the other
question: “did the spec actually look at it”. A view can pass every rule while
half of it goes unasserted, because rendering a page is not the same as
testing it. Both come from CoverageChecks in twirl-spec-quality, which
AllChecks includes.
What the spec never asserted
Every id, link and tracked element the page shows has to have been asserted by some test in the spec. Put the check in the last test, because it can only see the assertions that have already run:
"everything this page shows" must {
"have been asserted by one of these tests" in {
render(view(form)) must assertEverything
}
}
When something was missed:
7 things on this page were never asserted:
id (3): #sub-header, #sub-header-link, #upload-file
link (1): link "upload a new file or download one that is waiting." -> /upload
tracking (1): data-journey-click=link - click:File not available:Choose something else to do
Assertions are recorded by what they touch, not by the selector they used, so
asserting #action-list > * does not count as asserting #action-list. An
expectation that looks through several elements to find one counts only what it
found: link("x.help") asserts that link, not every link on the page, and
role("button").named("x.save") that button.
A link is its words and where it goes, so two links reading “Change” and going
to different places are two things to assert. coverageIgnored still takes
link "Change" for every link with those words, or
link "Change" -> /where for one of them.
They count for the page they were made on: every render, in any test of the
spec, that produces exactly that markup. A different view, or the same view in
another state, is a different page with a record of its own, so asserting
#value on the page with errors does not cover #value on the page without,
and one view’s assertions never pass another view that happens to share its
ids. To measure a view’s states as a whole, give them one name:
render(view(form)).coveredAs("name-page")
render(view(form.bind(Map("value" -> "")))).coveredAs("name-page")
TwirlSpec starts each suite with an empty record and clears it when the
suite finishes, so a spec run again in the same JVM cannot pass on what an
earlier run asserted. A page built directly with Page.fromString, rather than
rendered by a spec, keeps a record of its own.
assertEverything sees only the tests that ran before it, so a spec that uses
it runs its tests in order, which is ScalaTest’s default. Under
ParallelTestExecution there is no last test, and the check can run before
the assertions it depends on.
Assertions made through the page model, page.byId, page.css and the
component accessors, count the same as DSL expectations. Rule sets do not
count: they sweep the whole page, and what they touch says nothing about what
the spec asserted.
Four things are adjustable, usually once on a spec base:
override def coverageScope: String = "main, #main-content" // the layout is not the view spec's problem
override def coverageIgnored: Set[String] = Set("#print-link") // what every page inherits, by id
override def coverageIgnoredSelectors: Set[String] = Set(".page-tools") // or by selector, where it has no id
override def trackedAttributes: Set[String] = Set("data-journey-click")
Ignoring a block ignores what it holds, so disclaim only what is inherited:
#content on <main id="content"> would take the whole page out, and
assertEverything would pass with nothing asserted. HmrcServiceChecks already
leaves out play-frontend-hmrc’s report-a-problem link.
assertEverythingExcept("#id") covers the one-off case, and
unassertedContent(page) returns the list without failing.
The Twirl entry points nothing calls
Twirl generates render, f and ref beside apply. Nothing in a normal
service calls them, so a change to a template’s parameters can break them with
no test noticing, and they sit in coverage reports as permanently unreached
lines. One line exercises all three and checks they agree with apply:
"the generated entry points" in {
view must entryPointsAgree(
view(form, edit = false)(request, messages, appConfig),
form, false, request, messages, appConfig
)
}
The arguments after the expected HTML are the ones render takes: every
parameter including the implicit ones, flattened, in order. The check reaches
the methods by reflection, so a spec does not have to spell out each
template’s arity by hand, and a template whose parameters changed fails here
with the entry point named.
haveAgreeingEntryPoints asks the same of every template under a directory, so
no template can be left out of a list. There is one list of stand-ins, and each
template gets the first of each type its render declares, with what apply
gives as the baseline:
"the generated entry points" in {
Path.of("app/views") must haveAgreeingEntryPoints(form, false, "a name", Nil, None, request, messages)
}
The directory is one Twirl compiles from, such as app or src/main/twirl, or
the views directory inside it, and each template’s class is worked out from
its path as Twirl works it out. A type with no stand-in fails, naming the
template and the type, rather than skipping the template, and so does a
directory that is not there or holds no templates. render takes a repeated
parameter as an array, so an array is its stand-in. Form[A] and Form[B] are
one class at runtime, so a template reading its form’s value can be handed the
wrong one and throw. It takes arguments of its own, by the class it compiles to,
and they have to name a template that is there:
Path.of("app/views") must haveAgreeingEntryPoints(form, false, request, messages)
.forTemplate("views.html.register.dateOfBirthView")(dateForm, request, messages)
A template with @this comes from TwirlSpec’s application; a spec base with an
application of its own overrides templateInstance to ask that one. Stand-ins
reach Twirl’s methods without being a good page, so this is no test of what a
template shows.