Skip to content

Commit

Permalink
feat: support --bail and --silent for "test" command
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsManta committed Feb 12, 2024
1 parent 5c086fa commit 419ae65
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const fs = require('fs')
const path = require('path')
const test = require('./test')
const { parseArguments } = require('@thisismanta/pessimist')

const rulePath = findRulePath(__dirname)
if (!rulePath) {
Expand All @@ -19,13 +20,19 @@ module.exports.meta.name = name
module.exports.meta.version = version
module.exports.rules = module.exports.rules || {}

if (process.argv.includes('test')) {
const { bail, silent, ...args } = parseArguments(process.argv.slice(2), {
bail: false,
silent: false,
})

if (args[0] === 'test') {
if (Object.keys(module.exports.rules).length === 0) {
throw new Error('Could not find any rules.')
}

const errorCount = test(module.exports.rules, {
log: console.log,
bail,
log: silent ? () => { } : console.log,
err: console.error,
})
if (errorCount > 0) {
Expand Down
2 changes: 2 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const fs = require('fs')
const path = require('path')
const { jest, afterEach, it, expect } = require('@jest/globals')

jest.mock('@thisismanta/pessimist', () => ({ parseArguments: () => ({}) }))

afterEach(() => {
jest.resetModules()

Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"lefthook": "^1.6.1"
},
"dependencies": {
"@thisismanta/pessimist": "^1.2.0",
"chalk": "^4.0.0"
}
}
7 changes: 5 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function only(item) {
*/
module.exports = function test(
rules,
{ log, err } = { log: console.log, err: console.error }
{ bail, log, err } = { bail: false, log: console.log, err: console.error }
) {
// See https://eslint.org/docs/latest/integrate/nodejs-api#ruletester
const tester = new RuleTester()
Expand Down Expand Up @@ -88,7 +88,10 @@ module.exports = function test(
err(offset(getPrettyCode(testCase.code), chalk.bgRed))
err('')
err(offset(error.message, chalk.red))
return 1

if (bail) {
return 1
}
}

} else if (totalItems.length === runningItems.length) {
Expand Down
56 changes: 56 additions & 0 deletions test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,62 @@ it('returns non-zero errors, given any failing test case', () => {
const err = jest.fn()
const errorCount = test(rules, { log, err })

expect(errorCount).toBe(2)
expect(log.mock.calls.join('\n')).toMatchInlineSnapshot(`
"
PASS 0
FAIL 2"
`)
expect(err.mock.calls.join('\n')).toMatchInlineSnapshot(`
"🔴 foo
void(0)
Should have no errors but had 1: [
{
ruleId: 'foo',
severity: 1,
message: 'bar',
line: 1,
column: 1,
nodeType: 'Program',
endLine: 1,
endColumn: 8
}
] (1 strictEqual 0)
Should have 1 error but had 0: [] (0 strictEqual 1)"
`)
})

it('returns at most one error, given bailing out', () => {
const rules = {
foo: {
create(context) {
return {
Program(node) {
if (node.body.length > 0) {
context.report({
node,
message: 'bar'
})
}
}
}
},
tests: {
valid: [{ code: 'void(0)' }],
invalid: [{ code: '', errors: [{ message: 'bar' }] }],
}
}
}

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

expect(errorCount).toBe(1)
expect(log.mock.calls.join('\n')).toMatchInlineSnapshot(`""`)
expect(err.mock.calls.join('\n')).toMatchInlineSnapshot(`
Expand Down

0 comments on commit 419ae65

Please sign in to comment.