From 9bf2b1529705b0c7d5beac1682aaab57e17aabb4 Mon Sep 17 00:00:00 2001 From: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Date: Fri, 6 Oct 2023 08:44:05 +0900 Subject: [PATCH] Add `loadLint` option (#77) This option allows to use the dynamic `import()` function. --- CHANGELOG.md | 4 ++++ __tests__/getTestRule.test.js | 25 +++++++++++++++++++++++-- __tests__/getTestRuleConfigs.test.js | 23 +++++++++++++++++++++-- getTestRule.js | 8 +++++--- getTestRuleConfigs.js | 9 ++++++--- index.d.ts | 16 +++++++++++++--- 6 files changed, 72 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ad8fb4..8004adf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Head + +- Added: `loadLint` option. + ## 6.2.0 - Added: `testRuleConfigs` function. diff --git a/__tests__/getTestRule.test.js b/__tests__/getTestRule.test.js index 6dbf749..df053a7 100644 --- a/__tests__/getTestRule.test.js +++ b/__tests__/getTestRule.test.js @@ -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: [ @@ -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 {}' }], +}); diff --git a/__tests__/getTestRuleConfigs.test.js b/__tests__/getTestRuleConfigs.test.js index 17063b9..beff97a 100644 --- a/__tests__/getTestRuleConfigs.test.js +++ b/__tests__/getTestRuleConfigs.test.js @@ -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: [ { @@ -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' }], +}); diff --git a/getTestRule.js b/getTestRule.js index fb02cfa..3bf98bd 100644 --- a/getTestRule.js +++ b/getTestRule.js @@ -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}`, () => { diff --git a/getTestRuleConfigs.js b/getTestRuleConfigs.js index 02803d9..b8c7d90 100644 --- a/getTestRuleConfigs.js +++ b/getTestRuleConfigs.js @@ -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; diff --git a/index.d.ts b/index.d.ts index 03a4093..3d7d7a3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -144,6 +144,16 @@ export type TestSchema = { * @see https://jestjs.io/docs/api#testskipname-fn */ skip?: boolean; + + /** + * Loads the lint function. + */ + loadLint?: () => Promise; +}; + +type GetTestRuleOptions = { + plugins?: TestSchema['plugins']; + loadLint?: TestSchema['loadLint']; }; /** @@ -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; @@ -167,7 +177,7 @@ export type ConfigCase = { * Test configurations for a rule. */ export type TestRuleConfigs = ( - schema: Pick & { + schema: Pick & { accept?: ConfigCase[]; reject?: ConfigCase[]; }, @@ -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;