How to test themes or theme components?

I found this topic about testing plugins (automatically), but haven’t come across any mentioning how to test themes or theme components. Are there any examples of how to use QUnit (or similar) with themes?

2 个赞

You can add it to a user selectable theme or use the test link on the theme admin page.

Sorry, I meant automated testing – I will amend my previous post. I was thinking of something like QUnit that some plugins already use.

4 个赞

No, it’s not possible to add QUnit tests to themes/components at the moment…

@david how possible would it be possible to add support for a /test folder like plugins? we’d also have to enable the theme when the tests are run because the core tests run without any theme, right?

9 个赞

Certainly possible, but will need some work. Right now all the theme javascript files are bundled together into one file. We would need to make sure tests are put in a new, separate bundle, so that they don’t get served to regular visitors. And then once that’s done, make a way to run qunit tests for a single theme.

The other thing to consider is that we don’t make the /qunit route available on production servers. Since themes are often developed on production servers, we may need to rethink this :thinking:

10 个赞

This is one of the major downsides of theme components in my opinion. They are super easy to deploy which is fantastic, but testing often gets put aside.

10 个赞

If I understand correctly, anything that can be done with a theme component can be done with a plugin. The former is easier to deploy, while the latter allows testing.

2 个赞

Yes that is generally accurate. It ends up being a trade off in terms of what you are customising. For example adding a custom stylesheet probably couldn’t be tested even in a plugin. Adding custom javascript controls and widgets is where things become questionable.

6 个赞

I feel like we should consider this issue when we do the ember cli work. There is nothing impossible about having some sort of test runner for themes, and we could ship some of the basics in the discourse-theme gem to set up local testing using ember cli.

8 个赞

Running theme tests would require a full installation of Discourse though, right? There are so many interdependencies I don’t think we could test themes independently :thinking:

Maybe the theme-cli could have some logic to pull the discourse_dev docker image and run qunit tests inside that?

2 个赞

The whole idea with ember cli is that we are cleanly splitting server from client. We could ship enough of the JS side of things to test the client without running a rails server. You would still need node and ember cli installed for sure, but you may get away without installing a full Discourse stack including Redis and Postgres.

6 个赞

It might be tricky but something we can definitely keep in mind.

5 个赞

我们现在支持主题中的测试(这是较晚的更新,主题测试的支持是在 2021 年中期添加的)。您可以前往本地环境或生产站点中的 /theme-qunit,任何已安装且具有测试的主题/组件都将在此列出。请参阅 Discourse Tab Bar for MobileTag icons component 以获取示例。

6 个赞

这太棒了。这会扩展到测试插件中的 JavaScript 吗?

3 个赞

您是指能够在生产环境中运行测试吗?目前这仅限于主题。\n\n(当然,您可以在本地运行插件 JS 测试。)

1 个赞

我认为目标将是能够在 GitHub CI 中运行它们,就像我们目前可以使用 specs(Theme.和 Plugin JS)一样?

2 个赞

是的,我们为所有官方插件在 CI 中运行测试。

1 个赞