diff --git a/.eslintrc.js b/.eslintrc.js index 90caaa39e3..7a3bb2f789 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -21,7 +21,7 @@ module.exports = { allowImportExportEverywhere: true, }, globals: { - testRule: true, + testStylelintRule: true, }, extends: ['airbnb', 'prettier', 'prettier/react'], plugins: ['react-hooks', 'header'], @@ -39,17 +39,10 @@ module.exports = { 'block', [ '*', - { - pattern: ' * Copyright \\(c\\) Facebook, Inc. and its affiliates.', - }, + ' * Copyright (c) Facebook, Inc. and its affiliates.', ' *', - { - pattern: - ' * This source code is licensed under the MIT license found in the', - }, - { - pattern: ' * LICENSE file in the root directory of this source tree.', - }, + ' * This source code is licensed under the MIT license found in the', + ' * LICENSE file in the root directory of this source tree.', ' ', ], ], diff --git a/jest.config.js b/jest.config.js index 072838e751..8bb2ddeeff 100644 --- a/jest.config.js +++ b/jest.config.js @@ -27,5 +27,5 @@ module.exports = { transform: { '^.+\\.[jt]sx?$': 'babel-jest', }, - setupFiles: ['./jest-setup.js'], + setupFiles: ['./jest/stylelint-rule-test.js'], }; diff --git a/jest-setup.js b/jest/stylelint-rule-test.js similarity index 54% rename from jest-setup.js rename to jest/stylelint-rule-test.js index 10541990a9..679d0f5cda 100644 --- a/jest-setup.js +++ b/jest/stylelint-rule-test.js @@ -10,68 +10,55 @@ const stylelint = require('stylelint'); function getOutputCss(output) { const result = output.results[0]._postcssResult; - const css = result.root.toString(result.opts.syntax); - - return css; + return result.root.toString(result.opts.syntax); } -global.testRule = (rule, schema) => { - describe(schema.ruleName, () => { - const stylelintConfig = { - plugins: ['./packages/stylelint-copyright'], - rules: { - [schema.ruleName]: schema.config, - }, - }; - +global.testStylelintRule = (config, tests) => { + describe(tests.ruleName, () => { const checkTestCaseContent = testCase => testCase.description || testCase.code || 'no description'; - if (schema.accept && schema.accept.length) { - describe('accept', () => { - schema.accept.forEach(testCase => { + if (tests.accept && tests.accept.length) { + describe('accept cases', () => { + tests.accept.forEach(testCase => { const spec = testCase.only ? it.only : it; spec(checkTestCaseContent(testCase), () => { const options = { code: testCase.code, - config: stylelintConfig, - syntax: schema.syntax, + config, + syntax: tests.syntax, }; return stylelint.lint(options).then(output => { expect(output.results[0].warnings).toEqual([]); - if (!schema.fix) { - return; + if (!tests.fix) { + return null; } - // Check the fix - // eslint-disable-next-line consistent-return + // Check the fix. return stylelint .lint({...options, fix: true}) - .then(fixedOutput => { - const fixedCode = getOutputCss(fixedOutput); - - expect(fixedCode).toBe(testCase.code); - }); + .then(fixedOutput => getOutputCss(fixedOutput)) + .then(fixedCode => expect(fixedCode).toBe(testCase.fixed)); }); }); }); }); } - if (schema.reject && schema.reject.length) { - describe('reject', () => { - schema.reject.forEach(testCase => { - // eslint-disable-next-line no-nested-ternary - const spec = testCase.only ? it.only : testCase.skip ? it.skip : it; + if (tests.reject && tests.reject.length) { + describe('reject cases', () => { + tests.reject.forEach(testCase => { + const skip = testCase.skip ? it.skip : it; + const spec = testCase.only ? it.only : skip; spec(checkTestCaseContent(testCase), () => { const options = { code: testCase.code, - config: stylelintConfig, - syntax: schema.syntax, + config, + syntax: tests.syntax, }; return stylelint.lint(options).then(output => { @@ -81,44 +68,42 @@ global.testRule = (rule, schema) => { expect(warnings.length).toBeGreaterThanOrEqual(1); expect(testCase).toHaveMessage(); - if (testCase.message !== undefined) { + if (testCase.message != null) { expect(warning.text).toBe(testCase.message); } - if (testCase.line !== undefined) { + if (testCase.line != null) { expect(warning.line).toBe(testCase.line); } - if (testCase.column !== undefined) { + if (testCase.column != null) { expect(warning.column).toBe(testCase.column); } - if (!schema.fix) { - return; + if (!tests.fix) { + return null; } if (!testCase.fixed) { throw new Error( - 'If using { fix: true } in test schema, all reject cases must have { fixed: .. }', + 'If using { fix: true } in test tests, all reject cases must have { fixed: .. }', ); } - // Check the fix - // eslint-disable-next-line consistent-return + + // Check the fix. return stylelint .lint({...options, fix: true}) - .then(fixedOutput => { - const fixedCode = getOutputCss(fixedOutput); - - expect(fixedCode).toBe(testCase.fixed); - }); + .then(fixedOutput => getOutputCss(fixedOutput)) + .then(fixedCode => expect(fixedCode).toBe(testCase.fixed)); }); }); }); }); } + expect.extend({ toHaveMessage(testCase) { - if (testCase.message === undefined) { + if (testCase.message == null) { return { message: () => 'Expected "reject" test case to have a "message" property', diff --git a/packages/stylelint-copyright/__tests__/index.js b/packages/stylelint-copyright/__tests__/index.js index 9b4827693b..1da3eedc36 100644 --- a/packages/stylelint-copyright/__tests__/index.js +++ b/packages/stylelint-copyright/__tests__/index.js @@ -9,41 +9,49 @@ const rule = require('..'); const {ruleName, messages} = rule; -testRule(rule, { - ruleName, - fix: false, - accept: [ - { - code: ` +testStylelintRule( + { + // Relative to repo root. + plugins: ['./packages/stylelint-copyright'], + rules: { + [ruleName]: true, + }, + }, + { + ruleName, + fix: false, + accept: [ + { + code: ` /** * Copyright */ .foo {}`, - }, - { - code: ` + }, + { + code: ` /** * copyright */ .foo {}`, - }, - ], - reject: [ - { - code: ` + }, + ], + reject: [ + { + code: ` /** * Copyleft */ .foo {}`, - message: messages.rejected, - line: 2, - column: 1, - }, - { - code: ` + message: messages.rejected, + line: 2, + column: 1, + }, + { + code: ` /** * Copyleft */ @@ -52,9 +60,10 @@ testRule(rule, { * Copyright */ .foo {}`, - message: messages.rejected, - line: 2, - column: 1, - }, - ], -}); + message: messages.rejected, + line: 2, + column: 1, + }, + ], + }, +);