From 4529598a491da11ab2a5efa3abf740a1610390cc Mon Sep 17 00:00:00 2001 From: "Anantachai Saothong (Manta)" Date: Tue, 13 Feb 2024 17:38:50 +0700 Subject: [PATCH] feat: support string in valid test cases --- test.js | 19 +++++++++++++------ test.test.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/test.js b/test.js index 18471f8..8b55553 100644 --- a/test.js +++ b/test.js @@ -29,7 +29,7 @@ function only(item) { } /** - * @param {Record, invalid: Array } }>} rules + * @param {Record['2'] }>} rules * @returns {number} number of error test cases */ module.exports = function test( @@ -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} */ - 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) => { diff --git a/test.test.js b/test.test.js index 53ff9f2..fec9fa4 100644 --- a/test.test.js +++ b/test.test.js @@ -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() +})