Skip to content

Commit

Permalink
fix: could not use only function to filter test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsManta committed Feb 5, 2024
1 parent dca7304 commit 4720aeb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const fs = require('fs')
const path = require('path')
const test = require('./test')

const rulePath = findRulePath(__dirname)
if (!rulePath) {
Expand All @@ -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)
}
Expand Down
27 changes: 17 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, import('eslint').Rule.RuleModule & { tests?: TestCases }>} rules
* @returns {void | false}
Expand All @@ -18,20 +29,17 @@ 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') {
console.log('⚪ ' + ruleName)
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: [] }
Expand All @@ -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
}

Expand All @@ -65,7 +72,7 @@ module.exports = function test(rules) {
}
}

console.log((skipped ? '🟡' : '✅') + ' ' + ruleName)
console.log((oneOrMoreTestCaseIsSkipped ? '🟡' : '✅') + ' ' + ruleName)
}

console.log('')
Expand Down
26 changes: 26 additions & 0 deletions test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit 4720aeb

Please sign in to comment.