Skip to content

Commit

Permalink
feat: support string in valid test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsManta committed Feb 13, 2024
1 parent 1c3af90 commit 4529598
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
19 changes: 13 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function only(item) {
}

/**
* @param {Record<string, import('eslint').Rule.RuleModule & { tests?: { valid: Array<import('eslint').RuleTester.ValidTestCase>, invalid: Array<import('eslint').RuleTester.InvalidTestCase> } }>} rules
* @param {Record<string, import('eslint').Rule.RuleModule & { tests?: Parameters<import('eslint').RuleTester['run']>['2'] }>} rules
* @returns {number} number of error test cases
*/
module.exports = function test(
Expand All @@ -40,26 +40,33 @@ module.exports = function test(
const tester = new RuleTester()

const oneOrMoreTestCaseIsSkipped = Object.values(rules).some(ruleModule =>
ruleModule.tests?.valid.some(testCase => testCase[Exclusiveness]) ||
ruleModule.tests?.invalid.some(testCase => testCase[Exclusiveness])
ruleModule.tests?.valid?.some(testCase => testCase[Exclusiveness]) ||
ruleModule.tests?.invalid?.some(testCase => testCase[Exclusiveness])
)

const stats = { pass: 0, fail: 0, skip: 0 }
for (const ruleName in rules) {
const ruleModule = rules[ruleName]
if (!ruleModule.tests || typeof ruleModule.tests !== 'object') {
if (
!ruleModule.tests ||
typeof ruleModule.tests !== 'object' ||
!ruleModule.tests.valid && !ruleModule.tests.invalid
) {
log('⚪ ' + ruleName)
continue
}

for (const testCase of ruleModule.tests.invalid) {
for (const testCase of ruleModule.tests.invalid || []) {
testCase.errors = testCase.errors ?? []
}

/**
* @type {Array<TestCase>}
*/
const totalItems = [...ruleModule.tests.valid, ...ruleModule.tests.invalid]
const totalItems = [
...(ruleModule.tests.valid || []).map(testCase => typeof testCase === 'string' ? { code: testCase } : testCase),
...(ruleModule.tests.invalid || []),
]
const runningItems = totalItems.filter(testCase => oneOrMoreTestCaseIsSkipped ? !!testCase[Exclusiveness] : true)

const errors = runningItems.reduce((results, testCase) => {
Expand Down
37 changes: 37 additions & 0 deletions test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,40 @@ it('runs only the test case wrapped with `only` function', () => {
SKIP 3"
`)
})

it('supports string in valid test cases', () => {
const rules = {
foo: {
create(context) {
return {
Program(node) {
if (node.body.length > 0) {
context.report({
node,
message: 'bar'
})
}
}
}
},
tests: {
valid: [
'',
{ code: '' }
],
}
}
}

const log = jest.fn()
const err = jest.fn()
const errorCount = test(rules, { log, err })

expect(errorCount).toBe(0)
expect(log.mock.calls.join('\n')).toMatchInlineSnapshot(`
"🟢 foo
PASS 2"
`)
expect(err).not.toHaveBeenCalled()
})

0 comments on commit 4529598

Please sign in to comment.