In Tailwind 0.x, there was a list-reset utility that reset the list style and
padding on a HTML list, though it was removed prior to 1.0 and moved into
Tailwind’s base styles and applied by default.
However, on a few projects I use Tailwind in addition to either existing custom
styling or another CSS framework, and don’t use @tailwind base (formerly
@tailwind preflight) so don’t get the base styles.
Whilst I could re-create this by replacing it with two other classes
(list-none and p-0), I decided to write my own Tailwind CSS plugin
to re-add the list-reset class. This way I could keep backwards compatibility
in my projects and only need to add one class in other future instances.
In this post, I’ll use this as an example to show how to write tests for
Tailwind CSS plugins with a JavaScript testing framework called Jest.
More information about plugins for Tailwind CSS themselves can be found on the
Tailwind website.
Add dependencies
To start, we need to include jest as a dependency of the plugin, as well as
jest-matcher-css to perform assertions against the CSS that the plugin
generates.
We also need to add tailwindcss and postcss so that we can use them within
the tests.
yarn add -D jest jest-matcher-css postcss tailwindcss@next
This could be done with yarn add or npm install.
Writing the first test
In this plugin, the tests are going to be added into a new file called
test.js. This file is automatically loaded by Jest based on it’s testRegex
setting.
This is the format for writing test methods:
test('a description of the test', () => {
// Perform tasks and write assertions
});
The first test is to ensure that the correct CSS is generated from the plugin
using no options.
We do this by generating the plugin’s CSS, and asserting that it matches the
expected CSS within the test.
Now we need a way to generate the CSS so assertions can be written against it.
In this case, I’ve created a function called generatePluginCss that accepts
some optional options, processes PostCSS and Tailwind, and returns the CSS.
Whilst from: undefined isn’t required, if it’s not included you will get this
message:
Without from option PostCSS could generate wrong source map and will not
find Browserslist config. Set it to CSS file path or to undefined to prevent
this warning.
Configuring Tailwind
In order for the plugin to generate CSS, it needs to be enabled within the test,
and Tailwind’s core plugins need to be disabled so that we can assert against
just the output from the plugin.
As of Tailwind 1.0.0-beta5, this can be done as follows:
Now that we have tests, we need to be able to run them.
With Jest included as a dependency, we can update the test script within
package.json to execute it rather than returning a stub message.
- "test": "echo \"Error: no test specified\" && exit 1"
+ "test": "jest"
This means that as well as running the jest command directly to run the tests,
we can also run npm test or yarn test.
After running the tests, Jest will display a summary of the results:
Running tests automatically with Travis CI
As well as running the tests locally, they can also be run automatically via
services like Travis CI when a new pull request is submitted or each
time new commits are pushed.
This is done by adding a .travis-ci.yml file to the repository, like this one
which is based on the JavaScript and Node.js example:
I'm an Acquia-certified Drupal Triple Expert with 17
years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.