|
| 1 | +import { describe, test, expect } from "vitest"; |
| 2 | +import parse from "@commitlint/parse"; |
| 3 | +import { scopeDelimiterStyle } from "./scope-delimiter-style.js"; |
| 4 | + |
| 5 | +const messages = { |
| 6 | + noScope: "feat: subject", |
| 7 | + |
| 8 | + kebabScope: "feat(lint-staged): subject", |
| 9 | + snakeScope: "feat(my_scope): subject", |
| 10 | + |
| 11 | + defaultSlash: "feat(core/api): subject", |
| 12 | + defaultComma: "feat(core,api): subject", |
| 13 | + defaultCommaSpace: "feat(core, api): subject", |
| 14 | + defaultBackslash: "feat(core\\api): subject", |
| 15 | + |
| 16 | + nonDefaultPipe: "feat(core|api): subject", |
| 17 | + nonDefaultStar: "feat(core*api): subject", |
| 18 | + mixedCustom: "feat(core|api/utils): subject", |
| 19 | +} as const; |
| 20 | + |
| 21 | +describe("Scope Delimiter Validation", () => { |
| 22 | + describe("Messages without scopes", () => { |
| 23 | + test('Succeeds for "always" when there is no scope', async () => { |
| 24 | + const [actual, error] = scopeDelimiterStyle( |
| 25 | + await parse(messages.noScope), |
| 26 | + "always", |
| 27 | + ); |
| 28 | + |
| 29 | + expect(actual).toEqual(true); |
| 30 | + expect(error).toEqual(undefined); |
| 31 | + }); |
| 32 | + |
| 33 | + test('Succeeds for "never" when there is no scope', async () => { |
| 34 | + const [actual, error] = scopeDelimiterStyle( |
| 35 | + await parse(messages.noScope), |
| 36 | + "never", |
| 37 | + ); |
| 38 | + |
| 39 | + expect(actual).toEqual(true); |
| 40 | + expect(error).toEqual(undefined); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('"always" with default configuration', () => { |
| 45 | + test.each([ |
| 46 | + { scenario: "kebab-case scope", commit: messages.kebabScope }, |
| 47 | + { scenario: "snake_case scope", commit: messages.snakeScope }, |
| 48 | + ] as const)( |
| 49 | + "Treats $scenario as part of the scope and not a delimiter", |
| 50 | + async ({ commit }) => { |
| 51 | + const [actual, error] = scopeDelimiterStyle( |
| 52 | + await parse(commit), |
| 53 | + "always", |
| 54 | + ); |
| 55 | + |
| 56 | + expect(actual).toEqual(true); |
| 57 | + expect(error).toEqual("scope delimiters must be one of [/, \\, ,]"); |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + test.each([ |
| 62 | + { scenario: "comma ',' delimiter", commit: messages.defaultComma }, |
| 63 | + { scenario: "slash '/' delimiter", commit: messages.defaultSlash }, |
| 64 | + { |
| 65 | + scenario: "backslash '\\' delimiter", |
| 66 | + commit: messages.defaultBackslash, |
| 67 | + }, |
| 68 | + ] as const)("Succeeds when only $scenario is used", async ({ commit }) => { |
| 69 | + const [actual, error] = scopeDelimiterStyle( |
| 70 | + await parse(commit), |
| 71 | + "always", |
| 72 | + ); |
| 73 | + |
| 74 | + expect(actual).toEqual(true); |
| 75 | + expect(error).toEqual("scope delimiters must be one of [/, \\, ,]"); |
| 76 | + }); |
| 77 | + |
| 78 | + test.each([ |
| 79 | + { scenario: "comma without space", commit: messages.defaultComma }, |
| 80 | + { scenario: "comma with space", commit: messages.defaultCommaSpace }, |
| 81 | + ] as const)( |
| 82 | + "Normalizes $scenario as the same delimiter ','", |
| 83 | + async ({ commit }) => { |
| 84 | + const [actual, error] = scopeDelimiterStyle( |
| 85 | + await parse(commit), |
| 86 | + "always", |
| 87 | + ); |
| 88 | + |
| 89 | + expect(actual).toEqual(true); |
| 90 | + expect(error).toEqual("scope delimiters must be one of [/, \\, ,]"); |
| 91 | + }, |
| 92 | + ); |
| 93 | + |
| 94 | + test("Fails when a non-default delimiter is used", async () => { |
| 95 | + const [actual, error] = scopeDelimiterStyle( |
| 96 | + await parse(messages.nonDefaultStar), |
| 97 | + "always", |
| 98 | + ); |
| 99 | + |
| 100 | + expect(actual).toEqual(false); |
| 101 | + expect(error).toEqual("scope delimiters must be one of [/, \\, ,]"); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('"never" with default configuration', () => { |
| 106 | + test("Fails when scope uses only default delimiters", async () => { |
| 107 | + const [actual, error] = scopeDelimiterStyle( |
| 108 | + await parse(messages.defaultSlash), |
| 109 | + "never", |
| 110 | + ); |
| 111 | + |
| 112 | + expect(actual).toEqual(false); |
| 113 | + expect(error).toEqual("scope delimiters must not be one of [/, \\, ,]"); |
| 114 | + }); |
| 115 | + |
| 116 | + test("Succeeds when scope uses only non-default delimiter", async () => { |
| 117 | + const [actual, error] = scopeDelimiterStyle( |
| 118 | + await parse(messages.nonDefaultPipe), |
| 119 | + "never", |
| 120 | + ); |
| 121 | + |
| 122 | + expect(actual).toEqual(true); |
| 123 | + expect(error).toEqual("scope delimiters must not be one of [/, \\, ,]"); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe("Custom configuration", () => { |
| 128 | + test("Falls back to default delimiters when delimiters is an empty array", async () => { |
| 129 | + const [actual, error] = scopeDelimiterStyle( |
| 130 | + await parse(messages.defaultComma), |
| 131 | + "always", |
| 132 | + [], |
| 133 | + ); |
| 134 | + |
| 135 | + expect(actual).toEqual(true); |
| 136 | + expect(error).toEqual("scope delimiters must be one of [/, \\, ,]"); |
| 137 | + }); |
| 138 | + |
| 139 | + test("Succeeds when a custom single allowed delimiter is used", async () => { |
| 140 | + const [actual, error] = scopeDelimiterStyle( |
| 141 | + await parse(messages.nonDefaultStar), |
| 142 | + "always", |
| 143 | + ["*"], |
| 144 | + ); |
| 145 | + |
| 146 | + expect(actual).toEqual(true); |
| 147 | + expect(error).toEqual("scope delimiters must be one of [*]"); |
| 148 | + }); |
| 149 | + |
| 150 | + test("Fails when ',' is used but only '/' is allowed", async () => { |
| 151 | + const [actual, error] = scopeDelimiterStyle( |
| 152 | + await parse(messages.defaultComma), |
| 153 | + "always", |
| 154 | + ["/"], |
| 155 | + ); |
| 156 | + |
| 157 | + expect(actual).toEqual(false); |
| 158 | + expect(error).toEqual("scope delimiters must be one of [/]"); |
| 159 | + }); |
| 160 | + |
| 161 | + test("Succeeds when both '/' and '|' are allowed and used in the scope", async () => { |
| 162 | + const [actual, error] = scopeDelimiterStyle( |
| 163 | + await parse(messages.mixedCustom), |
| 164 | + "always", |
| 165 | + ["/", "|"], |
| 166 | + ); |
| 167 | + |
| 168 | + expect(actual).toEqual(true); |
| 169 | + expect(error).toEqual("scope delimiters must be one of [/, |]"); |
| 170 | + }); |
| 171 | + |
| 172 | + test('In "never" mode fails when explicitly forbidden delimiter is used', async () => { |
| 173 | + const [actual, error] = scopeDelimiterStyle( |
| 174 | + await parse(messages.nonDefaultPipe), |
| 175 | + "never", |
| 176 | + ["|"], |
| 177 | + ); |
| 178 | + |
| 179 | + expect(actual).toEqual(false); |
| 180 | + expect(error).toEqual("scope delimiters must not be one of [|]"); |
| 181 | + }); |
| 182 | + |
| 183 | + test('In "never" mode succeeds when delimiter is not in the forbidden list', async () => { |
| 184 | + const [actual, error] = scopeDelimiterStyle( |
| 185 | + await parse(messages.nonDefaultPipe), |
| 186 | + "never", |
| 187 | + ["/"], |
| 188 | + ); |
| 189 | + |
| 190 | + expect(actual).toEqual(true); |
| 191 | + expect(error).toEqual("scope delimiters must not be one of [/]"); |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments