Skip to content

Commit

Permalink
Add loadLint option (#77)
Browse files Browse the repository at this point in the history
This option allows to use the dynamic `import()` function.
  • Loading branch information
ybiquitous committed Oct 5, 2023
1 parent 3cc8e04 commit 9bf2b15
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Head

- Added: `loadLint` option.

## 6.2.0

- Added: `testRuleConfigs` function.
Expand Down
25 changes: 23 additions & 2 deletions __tests__/getTestRule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
const getTestRule = require('../getTestRule.js');

const testRule = getTestRule();
const plugins = [require.resolve('./fixtures/plugin-foo.js')];
const ruleName = 'plugin/foo';

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

accept: [
Expand All @@ -31,3 +33,22 @@ testRule({
},
],
});

testRule({
plugins,
ruleName,
config: ['.a'],
loadLint: () => Promise.resolve(require('stylelint').lint),
accept: [{ code: '.a {}' }],
});

const testRuleWithLoadLint = getTestRule({
loadLint: () => Promise.resolve(require('stylelint').lint),
});

testRuleWithLoadLint({
plugins,
ruleName,
config: ['.a'],
accept: [{ code: '.a {}' }],
});
23 changes: 21 additions & 2 deletions __tests__/getTestRuleConfigs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
const getTestRuleConfigs = require('../getTestRuleConfigs.js');

const testRuleConfigs = getTestRuleConfigs();
const plugins = [require.resolve('./fixtures/plugin-foo.js')];
const ruleName = 'plugin/foo';

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

accept: [
{
Expand All @@ -28,3 +30,20 @@ testRuleConfigs({
},
],
});

testRuleConfigs({
plugins,
ruleName,
loadLint: () => Promise.resolve(require('stylelint').lint),
accept: [{ config: 'a' }],
});

const testRuleConfigsWithLoadLint = getTestRuleConfigs({
loadLint: () => Promise.resolve(require('stylelint').lint),
});

testRuleConfigsWithLoadLint({
plugins,
ruleName,
accept: [{ config: 'a' }],
});
8 changes: 5 additions & 3 deletions getTestRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const util = require('util');
/** @type {import('.').getTestRule} */
module.exports = function getTestRule(options = {}) {
return function testRule(schema) {
const loadLint =
schema.loadLint || options.loadLint || (() => Promise.resolve(require('stylelint').lint)); // eslint-disable-line n/no-unpublished-require -- Avoid auto-install of `stylelint` peer dependency.

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

beforeAll(() => {
// eslint-disable-next-line n/no-unpublished-require -- Avoid auto-install of `stylelint` peer dependency.
lint = require('stylelint').lint;
beforeAll(async () => {
lint = await loadLint();
});

describe(`${schema.ruleName}`, () => {
Expand Down
9 changes: 6 additions & 3 deletions getTestRuleConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ module.exports = function getTestRuleConfigs(options = {}) {
only = false,
skip = false,
plugins = options.plugins,
loadLint: schemaLoadLint,
}) {
if (accept.length === 0 && reject.length === 0) {
throw new TypeError('The either "accept" or "reject" property must not be empty');
}

const loadLint =
schemaLoadLint || options.loadLint || (() => Promise.resolve(require('stylelint').lint)); // eslint-disable-line n/no-unpublished-require -- Avoid auto-install of `stylelint` peer dependency.

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

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

const testGroup = only ? describe.only : skip ? describe.skip : describe;
Expand Down
16 changes: 13 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ export type TestSchema = {
* @see https://jestjs.io/docs/api#testskipname-fn
*/
skip?: boolean;

/**
* Loads the lint function.
*/
loadLint?: () => Promise<import('stylelint').lint>;
};

type GetTestRuleOptions = {
plugins?: TestSchema['plugins'];
loadLint?: TestSchema['loadLint'];
};

/**
Expand All @@ -154,7 +164,7 @@ export type TestRule = (schema: TestSchema) => void;
/**
* Create a `testRule()` function with any specified plugins.
*/
export function getTestRule(options?: { plugins?: TestSchema['plugins'] }): TestRule;
export function getTestRule(options?: GetTestRuleOptions): TestRule;

export type ConfigCase = {
config: unknown;
Expand All @@ -167,7 +177,7 @@ export type ConfigCase = {
* Test configurations for a rule.
*/
export type TestRuleConfigs = (
schema: Pick<TestSchema, 'ruleName' | 'plugins' | 'only' | 'skip'> & {
schema: Pick<TestSchema, 'ruleName' | 'plugins' | 'only' | 'skip' | 'loadLint'> & {
accept?: ConfigCase[];
reject?: ConfigCase[];
},
Expand All @@ -176,7 +186,7 @@ export type TestRuleConfigs = (
/**
* Create a `testRuleConfigs()` function with any specified plugins.
*/
export function getTestRuleConfigs(options?: { plugins?: TestSchema['plugins'] }): TestRuleConfigs;
export function getTestRuleConfigs(options?: GetTestRuleOptions): TestRuleConfigs;

declare global {
var testRule: TestRule;
Expand Down

0 comments on commit 9bf2b15

Please sign in to comment.