Skip to content

Commit 0bb540d

Browse files
committed
feat(config): rules
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent ff66c7f commit 0bb540d

File tree

17 files changed

+1093
-3
lines changed

17 files changed

+1093
-3
lines changed

.eslintrc.base.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ const config = {
717717
'unicorn/no-static-only-class': 0,
718718
'unicorn/no-thenable': 2,
719719
'unicorn/no-this-assignment': 2,
720-
'unicorn/no-unreadable-array-destructuring': 2,
720+
'unicorn/no-unreadable-array-destructuring': 0,
721721
'unicorn/no-unsafe-regex': 0,
722722
'unicorn/no-unused-properties': 2,
723723
'unicorn/no-useless-fallback-in-spread': 2,

__fixtures__/signed-off-by.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @file Test Fixtures - SIGNED_OFF_BY
3+
* @module fixtures/SIGNED_OFF_BY
4+
*/
5+
6+
export default 'Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>'

__tests__/setup/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
*/
55

66
import './chai'
7+
import './matchers'
78
import './serializers'

__tests__/setup/matchers/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @file Entry Point - Custom Matchers
3+
* @module tests/setup/matchers
4+
* @see https://vitest.dev/guide/extending-matchers.html
5+
*/
6+
7+
import './level'
8+
import './rule-outcome'

__tests__/setup/matchers/level.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @file Custom Matchers - ruleOutcome
3+
* @module tests/setup/matchers/ruleOutcome
4+
*/
5+
6+
import type { MatcherState, SyncExpectationResult } from '@vitest/expect'
7+
import { get } from 'radash'
8+
9+
/**
10+
* Expects a commit linting rule to be of a certain severity level.
11+
*
12+
* @this {MatcherState}
13+
*
14+
* @param {MatcherState} this - Matcher state
15+
* @param {unknown} received - Received value
16+
* @param {0 | 1 | 2} level - Expected rule config severity
17+
* @return {SyncExpectationResult} Expectation result
18+
*/
19+
function level(
20+
this: MatcherState,
21+
received: unknown,
22+
level: 0 | 1 | 2
23+
): SyncExpectationResult {
24+
/**
25+
* Constructs an expectation result message.
26+
*
27+
* @return {string} Expectation result message
28+
*/
29+
const message = (): string => {
30+
return `expected rule to have severity ${level}`
31+
}
32+
33+
return Array.isArray(received)
34+
? {
35+
actual: received,
36+
expected: level,
37+
message,
38+
pass: get(received, received.length === 1 ? '0' : '2') === level
39+
}
40+
: { actual: received, expected: level, message, pass: false }
41+
}
42+
43+
expect.extend({ level })
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @file Custom Matchers - ruleOutcome
3+
* @module tests/setup/matchers/ruleOutcome
4+
*/
5+
6+
import type cl from '@commitlint/types'
7+
import type { MatcherState, SyncExpectationResult } from '@vitest/expect'
8+
9+
/**
10+
* Expects an array of {@linkcode cl.RuleOutcome} objects to include a specific
11+
* {@linkcode cl.LintRuleOutcome}.
12+
*
13+
* @this {MatcherState}
14+
*
15+
* @param {MatcherState} this - Matcher state
16+
* @param {unknown} received - Received value
17+
* @param {string} name - Name of expected {@linkcode cl.LintRuleOutcome}
18+
* @param {0 | 1 | 2} level - Expected {@linkcode cl.RuleConfigSeverity}
19+
* @return {SyncExpectationResult} Expectation result
20+
*/
21+
function ruleOutcome(
22+
this: MatcherState,
23+
received: unknown,
24+
name: string,
25+
level: 0 | 1 | 2
26+
): SyncExpectationResult {
27+
/**
28+
* Constructs an expectation result message.
29+
*
30+
* @return {string} Expectation result message
31+
*/
32+
const message = (): string => {
33+
/**
34+
* Stringified version of {@linkcode received}.
35+
*
36+
* @const {string} rs
37+
*/
38+
const rs: string = `${this.utils.printReceived(received)}`
39+
40+
return `expected ${rs} to have rule outcome "${name}" of severity ${level}`
41+
}
42+
43+
/**
44+
* Expected values.
45+
*
46+
* @const {[Pick<cl.LintRuleOutcome, 'name' | 'level'>]} expected
47+
*/
48+
const expected: [Pick<cl.LintRuleOutcome, 'level' | 'name'>] = [
49+
{ level: level, name }
50+
]
51+
52+
return Array.isArray(received)
53+
? {
54+
actual: received,
55+
expected,
56+
message,
57+
pass: received.find((outcome: cl.LintRuleOutcome): boolean => {
58+
return outcome.name === name && outcome.level === level
59+
})
60+
}
61+
: { actual: received, expected, message, pass: false }
62+
}
63+
64+
expect.extend({ ruleOutcome })

__tests__/ts/v4/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"noUnusedParameters": false,
3030
"outDir": "../../../dist",
3131
"paths": {
32+
"#fixtures/*": ["__fixtures__/*"],
3233
"#src": ["src/index"],
3334
"#src/*": ["src/*"],
3435
"#tests/*": ["__tests__/*"]

__tests__/utils/signoff.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @file Test Utilities - signoff
3+
* @module tests/utils/signoff
4+
*/
5+
6+
import SIGNED_OFF_BY from '#fixtures/signed-off-by'
7+
8+
/**
9+
* Adds a `Signed-off-by` trailer to the given `commit` message.
10+
*
11+
* @param {string} commit - Commit to sign
12+
* @param {number?} [lines=1] - Number of leading blank lines
13+
* @return {string} `commit` with `Signed-off-by` trailer
14+
*/
15+
const signoff = (commit: string, lines: number = 1): string => {
16+
return commit + `\n${'\n'.repeat(lines)}${SIGNED_OFF_BY}`
17+
}
18+
19+
export default signoff

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"@typescript-eslint/parser": "5.53.0",
106106
"@vates/toggle-scripts": "1.0.0",
107107
"@vitest/coverage-c8": "0.29.1",
108+
"@vitest/expect": "0.29.1",
108109
"@vitest/ui": "0.29.1",
109110
"add-stream": "1.0.0",
110111
"chai": "4.3.7",

0 commit comments

Comments
 (0)