diff --git a/index.js b/index.js index 523c067..cdf4d92 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const fs = require('fs') const path = require('path') +const test = require('./test') const rulePath = findRulePath(__dirname) if (!rulePath) { @@ -23,7 +24,6 @@ if (process.argv.includes('test')) { throw new Error('Could not find any rules.') } - const test = require('./test') if (test(module.exports.rules) === false) { process.exit(1) } diff --git a/test.js b/test.js index 38a036f..69b9411 100644 --- a/test.js +++ b/test.js @@ -10,6 +10,17 @@ const chalk = require('chalk') * }} TestCases */ +const Exclusiveness = Symbol('exclusivenessToken') + +global.only = function only(testCase) { + // Disallow test case exclusiveness in CI + if (!process.env.CI) { + testCase[Exclusiveness] = true + } + + return testCase +} + /** * @param {Record} rules * @returns {void | false} @@ -18,12 +29,6 @@ module.exports = function test(rules) { // See https://eslint.org/docs/latest/integrate/nodejs-api#ruletester const tester = new RuleTester() - const exclusiveTestCases = [] - global.only = function only(testCase) { - exclusiveTestCases.push(testCase) - return testCase - } - for (const ruleName in rules) { const ruleModule = rules[ruleName] if (!ruleModule.tests || typeof ruleModule.tests !== 'object') { @@ -31,7 +36,10 @@ module.exports = function test(rules) { continue } - let skipped = false + const oneOrMoreTestCaseIsSkipped = !!( + ruleModule.tests.valid.some(testCase => testCase[Exclusiveness]) || + ruleModule.tests.invalid.some(testCase => testCase[Exclusiveness]) + ) const validItems = ruleModule.tests.valid.map(testCase => ( { testCase, valid: [testCase], invalid: [] } @@ -41,8 +49,7 @@ module.exports = function test(rules) { )) for (const { testCase, valid, invalid } of [...validItems, ...invalidItems]) { - if (exclusiveTestCases.length > 0 && !exclusiveTestCases.includes(testCase)) { - skipped = true + if (oneOrMoreTestCaseIsSkipped && !testCase[Exclusiveness]) { continue } @@ -65,7 +72,7 @@ module.exports = function test(rules) { } } - console.log((skipped ? '🟡' : '✅') + ' ' + ruleName) + console.log((oneOrMoreTestCaseIsSkipped ? '🟡' : '✅') + ' ' + ruleName) } console.log('') diff --git a/test.test.js b/test.test.js index bd98e94..085bac6 100644 --- a/test.test.js +++ b/test.test.js @@ -72,3 +72,29 @@ it('returns false, given a failing test case', () => { expect(console.log).toHaveBeenCalledWith('🔴 foo') expect(console.error).toHaveBeenCalledWith(expect.stringMatching(/Should have no errors but had 1/)) }) + +it('runs only the test case wrapped with `only` function', () => { + const rules = { + foo: { + create(context) { + return { + Program(node) { + if (node.body.length > 0) { + context.report({ + node, + message: 'bar' + }) + } + } + } + }, + tests: { + valid: [only({ code: '' }), { code: 'void(0)' }], + invalid: [], + } + } + } + + expect(test(rules)).not.toBe(false) + expect(console.error).not.toHaveBeenCalled() +})