discourse-writing-js-tests

Write QUnit unit, component, and acceptance tests for Discourse core, plugins, and themes.

47.8k|9.0k|Updated Jan 12, 2013
One-click install
npx skills add https://github.com/discourse/discourse --skill discourse-writing-js-tests
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: discourse-writing-js-tests
Source: https://github.com/discourse/discourse/tree/main/.skills/discourse-writing-js-tests
Command: npx skills add https://github.com/discourse/discourse --skill discourse-writing-js-tests

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Writing JavaScript tests for Discourse requires knowing its specific QUnit conventions, helper wrappers, module naming rules, and network stubbing patterns, which are easy to get wrong and cause flaky or inconsistent tests.

Core Features & Use Cases

  • Test Structure Guidance: Covers unit tests with setupTest, component rendering tests with setupRenderingTest, and full acceptance tests with the acceptance() helper and needs configuration.
  • Naming & Assertion Conventions: Enforces pipe-separated module naming (PascalCase for components, kebab-case for libs/services) and qunit-dom assertions via assert.dom.
  • Network Stubbing & Fixtures: Explains pretender-based API stubbing, canned fixtures, and the qunit-helpers toolbox for users, MessageBus, fake time, and input simulation.
  • Use Case: When adding a new component to a Discourse plugin, use this Skill to scaffold a .gjs rendering test with the correct module name, setupRenderingTest import, and pretender routes, then run it with bin/qunit.

Quick Start

Write a QUnit component rendering test for my Discourse plugin's new PollInfo component following the project conventions.

Frequently Asked Questions about discourse-writing-js-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write a QUnit component test in Discourse?

Import setupRenderingTest from discourse/tests/helpers/component-test, not directly from ember-qunit, and use a .gjs file with an inline template to render the real component. Assert on the output with assert.dom from qunit-dom.

How do I write acceptance tests for a Discourse plugin?

Use the acceptance() helper from discourse/tests/helpers/qunit-helpers, which wires up setupApplicationTest, the default pretender, and cleanup. Configure the scenario with needs.user(), needs.settings(), needs.site(), and needs.pretender() before calling visit().

How do I stub API requests in Discourse JavaScript tests?

Discourse ships a default Pretender server that answers common endpoints. Add or override routes with needs.pretender() in acceptance tests, using helper.response(body) or helper.response(statusCode, body) to shape responses.

What module naming convention do Discourse QUnit tests use?

Module titles are pipe-separated hierarchies where the last segment is the subject. Components use PascalCase like Integration | Component | PollInfo, while libs, utilities, services, and models stay kebab-case matching their filenames.

How do I run a single QUnit test file in Discourse?

Run bin/qunit with the file or directory path, or use --filter with a case-insensitive substring or slash-wrapped regex to match module and test names. A running Rails server is required unless you pass --standalone.

Why are my Discourse QUnit tests flaky?

Flakiness usually comes from forgetting to await async interactions like render, click, fillIn, and visit, or from leaking state between tests. Always await test-helper calls and rely on testCleanup to reset global state.