Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/content/contribute/writing-a-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,42 @@
vendor.js
styles.css
```

## Testing your plugin

Testing plugins is similar to testing any other build tool. The recommended approach is to run a webpack compilation with your plugin enabled and verify that the compilation produces the expected result.

A typical plugin test should:

- Create a webpack configuration that includes your plugin.
- Run a compilation using the Node.js API.
- Assert the expected compilation output, generated assets, warnings, or errors depending on the plugin's behavior.

For example, using Jest:

```js
import webpack from "webpack";
import MyPlugin from "../src/MyPlugin.js";

test("runs the plugin successfully", (done) => {
const compiler = webpack({
mode: "development",
entry: "./fixtures/index.js",
plugins: [new MyPlugin()],
});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(false);

// Add assertions that verify your plugin's behavior.

done();
});
});
```

The exact assertions depend on what your plugin does. For example, you might verify emitted assets, transformed source code, warnings, errors, or changes made to the compilation.

For real-world examples of plugin tests, browse the webpack repository and webpack ecosystem plugins to see how they compile fixtures and assert the resulting output.

Check failure on line 546 in src/content/contribute/writing-a-plugin.mdx

View workflow job for this annotation

GitHub Actions / Lint (ubuntu-latest, lts/*)

Delete `⏎`
Loading