Skip to content

Commit

Permalink
Rename testInvalidRuleConfigs to testRuleConfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Aug 10, 2023
1 parent 36a1bc5 commit b77ecc8
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 93 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Head

- Added: `testInvalidRuleConfigs` function.
- Added: `testRuleConfigs` function.

## 6.1.1

Expand Down
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ Add the preset to your `jest.config.js` or `jest` field in `package.json`:
}
```

Optionally, you can avoid specifying `plugins` in every schema by defining your own setup file to configure the `testRule`/`testInvalidRuleConfigs` functions.
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`/`getTestInvalidRuleConfigs`:
1. Create `jest.setup.js` in the root of your project. Provide `plugins` option to `getTestRule`/`getTestRuleConfigs`:

<!-- prettier-ignore -->
```js
const { getTestRule } = require("jest-preset-stylelint");

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

2. Add `jest.setup.js` to your `jest.config.js` or `jest` field in `package.json`:
Expand Down Expand Up @@ -113,9 +112,9 @@ testRule({
});
```

### `testInvalidRuleConfigs`
### `testRuleConfigs`

The `testInvalidRuleConfigs` function enables you to test invalid configs for a rule.
The `testRuleConfigs` function enables you to test invalid configs for a rule.

For example:

Expand All @@ -124,7 +123,13 @@ testInvalidRuleConfigs({
plugins: ["."],
ruleName,

configs: [
accept: [
{
config: "valid"
}
],

reject: [
{
config: "invalid"
},
Expand Down
20 changes: 0 additions & 20 deletions __tests__/getTestInvalidRuleConfigs.test.js

This file was deleted.

30 changes: 30 additions & 0 deletions __tests__/getTestRuleConfigs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const getTestRuleConfigs = require('../getTestRuleConfigs.js');

const testRuleConfigs = getTestRuleConfigs();

testRuleConfigs({
plugins: [require.resolve('./fixtures/plugin-foo.js')],
ruleName: 'plugin/foo',

accept: [
{
config: 'a',
},
{
config: ['b'],
description: 'string is allowed',
},
],

reject: [
{
config: 123,
},
{
config: [/foo/],
description: 'regex is not allowed',
},
],
});
49 changes: 0 additions & 49 deletions getTestInvalidRuleConfigs.js

This file was deleted.

68 changes: 68 additions & 0 deletions getTestRuleConfigs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const { inspect } = require('util');

/** @type {import('.').getTestRuleConfigs} */
module.exports = function getTestRuleConfigs(options = {}) {
return function testRuleConfigs({
ruleName,
accept = [],
reject = [],
only = false,
skip = false,
plugins = options.plugins,
}) {
if (accept.length === 0 && reject.length === 0) {
throw new TypeError('The either "accept" or "reject" property must not be empty');
}

/** @type {import('stylelint').lint} */
let lint;

beforeAll(() => {
// eslint-disable-next-line n/no-unpublished-require
lint = require('stylelint').lint;
});

const testGroup = only ? describe.only : skip ? describe.skip : describe;

testGroup(`${ruleName} configs`, () => {
/**
* @param {import('.').ConfigCase} case
* @param {(warnings: unknown[]) => void} comparison
*/
function testConfig({ config, description, only: onlyTest, skip: skipTest }, comparison) {
const testFn = onlyTest ? test.only : skipTest ? test.skip : test;

testFn(`${description || inspect(config)}`, async () => {
const lintConfig = {
plugins,
rules: { [ruleName]: config },
};
const { results } = await lint({ code: '', config: lintConfig });

expect(results).toHaveLength(1);
comparison(results[0].invalidOptionWarnings);
});
}

describe('accept', () => {
accept.forEach((c) => {
testConfig(c, (warnings) => {
// eslint-disable-next-line jest/no-standalone-expect
expect(warnings).toEqual([]);
});
});
});

describe('reject', () => {
reject.forEach((c) => {
testConfig(c, (warnings) => {
// eslint-disable-next-line jest/no-standalone-expect
expect(warnings).toEqual([{ text: expect.stringMatching(`"${ruleName}"`) }]);
});
});
});
});
};
};
27 changes: 14 additions & 13 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,29 @@ export type TestRule = (schema: TestSchema) => void;
*/
export function getTestRule(options?: { plugins?: TestSchema['plugins'] }): TestRule;

export type ConfigCase = {
config: unknown;
description?: string;
only?: boolean;
skip?: boolean;
};

/**
* Test invalid configurations for a rule.
* Test configurations for a rule.
*/
export type TestInvalidRuleConfigs = (
export type TestRuleConfigs = (
schema: Pick<TestSchema, 'ruleName' | 'plugins' | 'only' | 'skip'> & {
configs: {
config: unknown;
description?: string;
only?: boolean;
skip?: boolean;
}[];
accept?: ConfigCase[];
reject?: ConfigCase[];
},
) => void;

/**
* Create a `testInvalidRuleConfigs()` function with any specified plugins.
* Create a `testRuleConfigs()` function with any specified plugins.
*/
export function getTestInvalidRuleConfigs(options?: {
plugins?: TestSchema['plugins'];
}): TestInvalidRuleConfigs;
export function getTestRuleConfigs(options?: { plugins?: TestSchema['plugins'] }): TestRuleConfigs;

declare global {
var testRule: TestRule;
var testInvalidRuleConfigs: TestInvalidRuleConfigs;
var testRuleConfigs: TestRuleConfigs;
}
4 changes: 2 additions & 2 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const getTestRule = require('./getTestRule.js');
const getTestInvalidRuleConfigs = require('./getTestInvalidRuleConfigs.js');
const getTestRuleConfigs = require('./getTestRuleConfigs.js');

global.testRule = getTestRule();
global.testInvalidRuleConfigs = getTestInvalidRuleConfigs();
global.testRuleConfigs = getTestRuleConfigs();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"getTestInvalidRuleConfigs.js",
"getTestRuleConfigs.js",
"getTestRule.js",
"jest-preset.js",
"jest-setup.js",
Expand Down

0 comments on commit b77ecc8

Please sign in to comment.