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)
1 Like

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'
    
2 Likes