Skip to content

Commit

Permalink
Improve code examples in README (#86)
Browse files Browse the repository at this point in the history
Prefer ESM, considering Stylelint 16.0.0.
  • Loading branch information
ybiquitous committed Nov 23, 2023
1 parent 9693b30 commit 27a208a
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions README.md
Expand Up @@ -22,24 +22,29 @@ Add the preset to your `jest.config.js` or `jest` field in `package.json`:
}
```

### Adjust setup globally

Optionally, you can avoid specifying `plugins` in every schema by defining your own setup file to configure the `testRule`/`testRuleConfigs` functions.
This is useful if you have many tests. There are two additional steps to do this:

1. Create `jest.setup.js` in the root of your project. Provide `plugins` option to `getTestRule`/`getTestRuleConfigs`:

```js
const { getTestRule, getTestRuleConfigs } = require("jest-preset-stylelint");
import { getTestRule, getTestRuleConfigs } from "jest-preset-stylelint";
import myPlugin from "./my-plugin.js";

const plugins = [myPlugin];

global.testRule = getTestRule({ plugins: ["./"] });
global.testRuleConfigs = getTestRuleConfigs({ plugins: ["./"] });
global.testRule = getTestRule({ plugins });
global.testRuleConfigs = getTestRuleConfigs({ plugins });
```

2. Add `jest.setup.js` to your `jest.config.js` or `jest` field in `package.json`:

```json
{
"preset": "jest-preset-stylelint",
"setupFiles": ["jest.setup.js"]
"setupFiles": ["<rootDir>/jest.setup.js"]
}
```

Expand All @@ -57,10 +62,16 @@ For example, we can test a plugin that enforces and autofixes kebab-case class s

```js
// my-plugin.test.js
const { messages, ruleName } = require(".");
import myPlugin from "./my-plugin.js";

const plugins = [myPlugin];
const {
ruleName,
rule: { messages }
} = myPlugin;

testRule({
plugins: ["."],
plugins,
ruleName,
config: [true, { type: "kebab" }],
fix: true,
Expand Down Expand Up @@ -120,7 +131,7 @@ For example:

```js
testRuleConfigs({
plugins: ["."],
plugins,
ruleName,

accept: [
Expand Down

0 comments on commit 27a208a

Please sign in to comment.