Functional testing harness for templates / partials

not really sure where this aught live … so putting it in feature

there have been several messages here asking in various ways about “unit testing” spanning quite some time

an example:

(there are numerous others but hard to link them all on mobile)

i’ve seen a few stubs and projects that appear discarded, or not actively in use any longer.

it does seem to me that it’d be useful to have a happypath of
“how to design/test your partials” such that one can create scenario tests to ensure output is as expected across versions as things evolve and change.

yes, its complicated and there’s no OSFA philosophy… but there are certainly several antipatterns…

y’kno?

Barring an actual testing harness, is there a not horrible way to test a partial, or set of partials, in conjunction…

Perhaps something that copies a set of partials into a dir and runs hugo against just those components with some form of ‘should emit…’ data?

or am I just looking at this completely wrong?

2 Likes

jest-hugo

one of the frameworks like this i’ve found

is anyone aware of others?

anyone have thots?

You could use something like this to unit test partials and shortcodes:

git clone --single-branch -b hugo-forum-topic-49094 https://github.com/jmooring/hugo-testing hugo-forum-topic-49094
cd hugo-forum-topic-49094
hugo server

The above gives you a descriptive warning if a test (compare to gold) fails, but it doesn’t show you a diff. I’m hopeful this proposal will be accepted: https://github.com/gohugoio/hugo/issues/12330.

In content/tests, the first two test a partial by wrapping the partial call within an inline shortcode… very handy. The other two test a shortcode. So, there’s one test for every page under content/tests.

The gold data (“want”) is in assets/gold.

The test itself is in layouts/tests/single.html, and you could obviously pull the shortcodes and partials from one or more:

  • Themes
  • Modules
  • Other projects in your development environment (module mounts)
2 Likes

I’m interested in this approach, I have a question is that some partials rely on the site.Params, is it possible to override the Params in Hugo (runtime)?

No. You’d need to code your partial to conditionally look at something else when you’re testing it.

I’ve updated the example above to be a bit prettier. See the README.

1 Like

is it possible to override the Params in Hugo (runtime)?

Well, you could do it by environment, but that would get out of control pretty quickly.

1 Like

If you can build from source, strings.Diff is now available. Pull changes from the test site…

git clone --single-branch -b hugo-forum-topic-49094 https://github.com/jmooring/hugo-testing hugo-forum-topic-49094
cd hugo-forum-topic-49094
hugo server

…and you will see something like:

You can pull the shortcodes and partials to be tested from one or more:

  • Themes

  • Modules

  • Projects in your development environment (use module mounts)

    [[module.mounts]]
    source = 'layouts'
    target = 'layouts'
    
    [[module.mounts]]
    source = '/home/user/projects/project-a/layouts/partials'
    target = 'layouts/partials'
    
    [[module.mounts]]
    source = '/home/user/projects/project-b/layouts/shortcodes'
    target = 'layouts/shortcodes'
    

Edit: the strings.Diff function is available in v0.125.0 and later.

2 Likes

Does this feel, to you, like the best way forward for testing partials/templates moving forward?

or is this simply what may be done today?

I’m happy to start running down this path if yer sayin it’s likely to be the cleanest way to test stuff long term

But I’m REALLY not sure what the “right” solution here is bigger picture…

essentially, I want to help come up with a pattern that’d allow one to build out a suite of functional exemplars for each of the things they depend on within their hugo site, and have an elegant (or as close to it as possible) way of validating that nothin’s busted as changes are made over time…

in a fashion that serves as wide a variety of hugo users as possible, so the path becomes well trodden…

This is a subject that there’s not a lot of documentation on / around… which kinda confuses me, but maybe I’m just totally doing it wrong…

:slight_smile:

I don’t think there is a setup that will be right for everyone. I suggest you try the above. If it does what you want, great, otherwise look for something else.