Skip to content

Commit 0961b39

Browse files
Merge pull request #4 from dennisbergevin/feat/flagging-prompts
feat: Adding specs titles tags flags
2 parents 662e962 + 688204a commit 0961b39

7 files changed

+282
-49
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,35 @@ For selecting component specs:
7171
npx cypress-cli-select run --component
7272
```
7373

74+
If you want to skip straight to selecting specs, titles or tags:
75+
76+
```bash
77+
npx cypress-cli-select run --specs
78+
# skips straight to spec selection
79+
```
80+
81+
```bash
82+
npx cypress-cli-select run --titles
83+
# skips to test title selection
84+
```
85+
86+
```bash
87+
npx cypress-cli-select run --tags
88+
# skips to tag selection
89+
```
90+
91+
```bash
92+
npx cypress-cli-select run --specs --tags
93+
# skips to spec selection, followed by tag selection
94+
```
95+
96+
```bash
97+
npx cypress-cli-select run --specs --titles
98+
# skips to spec selection, followed by title selection
99+
```
100+
101+
**Note**: You cannot pass both the `--titles` and `--tags` arguments.
102+
74103
You can also include more cli arguments similar to `cypress run`, as the command harnesses the power of [Cypress module API](https://docs.cypress.io/guides/guides/module-api):
75104

76105
```bash

cypress.config.js

-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ module.exports = defineConfig({
44
e2e: {
55
trashAssetsBeforeRuns: false,
66
setupNodeEvents(on, config) {
7-
// on("before:run", (details) => {
8-
// console.log(details);
9-
// });
107
require("@bahmutov/cy-grep/src/plugin")(config);
118
on("task", {
129
log(message) {

index.js

+105-43
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ async function runSelectedSpecs() {
115115
process.env.SUBMIT_FOCUSED = true;
116116
}
117117

118+
if (process.argv.includes("--titles") && process.argv.includes("--tags")) {
119+
console.log("\n");
120+
console.log(pc.redBright(pc.bold(` Cannot choose both titles and tags `)));
121+
process.exit();
122+
}
123+
124+
if (process.argv.includes("--titles")) {
125+
findAndRemoveArgv("--titles");
126+
process.env.TEST_TITLES = true;
127+
process.env.CY_GREP_FILTER_METHOD = "Titles";
128+
}
129+
130+
if (process.argv.includes("--specs")) {
131+
findAndRemoveArgv("--specs");
132+
process.env.TEST_SPECS = true;
133+
}
134+
135+
if (process.argv.includes("--tags")) {
136+
findAndRemoveArgv("--tags");
137+
process.env.TEST_TAGS = true;
138+
process.env.CY_GREP_FILTER_METHOD = "Tags";
139+
}
140+
118141
// set the testing type
119142
// this is used by find-cypress-specs package to get the appropriate spec list
120143
if (process.argv.includes("--component")) {
@@ -125,6 +148,30 @@ async function runSelectedSpecs() {
125148

126149
try {
127150
// help menu options
151+
yarg
152+
.completion("--specs", false)
153+
.option("specs", {
154+
desc: "Skips to spec selection prompt",
155+
type: "boolean",
156+
})
157+
.example("npx cypress-cli-select run --specs");
158+
159+
yarg
160+
.completion("--titles", false)
161+
.option("titles", {
162+
desc: "Skips to test title selection prompt",
163+
type: "boolean",
164+
})
165+
.example("npx cypress-cli-select run --titles");
166+
167+
yarg
168+
.completion("--tags", false)
169+
.option("tags", {
170+
desc: "Skips to tag selection prompt",
171+
type: "boolean",
172+
})
173+
.example("npx cypress-cli-select run --tags");
174+
128175
yarg
129176
.completion("--print-selected", false)
130177
.option("print-selected", {
@@ -174,51 +221,66 @@ async function runSelectedSpecs() {
174221
* Test titles/tags requires the cy-grep package
175222
*/
176223
// Prompt for use to select spec and test titles or tags option
177-
const specAndTestPrompt = await select({
178-
message: "Choose to filter by specs, specific test titles or tags: ",
179-
multiple: disableTitleTagChoice ? false : true,
180-
defaultValue: disableTitleTagChoice ? "Specs" : null,
181-
clearInputWhenSelected: true,
182-
selectFocusedOnSubmit: process.env.SUBMIT_FOCUSED,
183-
canToggleAll: true,
184-
options: [
185-
{
186-
name: "Specs",
187-
value: "Specs",
188-
},
189-
{
190-
name: "Test titles or tags (requires cy-grep)",
191-
value: "Tests or tags",
192-
disabled: disableTitleTagChoice,
193-
},
194-
],
195-
required: true,
196-
});
197-
198-
/*
199-
200-
/*
201-
* NOTE:: Choose test titles or tags
202-
* This requires the cy-grep package
203-
*/
204-
if (specAndTestPrompt.includes("Tests or tags")) {
205-
// Prompt for use to select test titles or tags option
206-
const titleOrTagPrompt = await select({
207-
message: "Choose to filter by specific test titles or tags: ",
208-
multiple: false,
224+
if (
225+
!process.env.TEST_TITLES &&
226+
!process.env.TEST_SPECS &&
227+
!process.env.TEST_TAGS
228+
) {
229+
const specAndTestPrompt = await select({
230+
message: "Choose to filter by specs, specific test titles or tags: ",
231+
multiple: disableTitleTagChoice ? false : true,
232+
defaultValue: disableTitleTagChoice ? "Specs" : null,
233+
clearInputWhenSelected: true,
234+
selectFocusedOnSubmit: process.env.SUBMIT_FOCUSED,
235+
canToggleAll: true,
209236
options: [
210237
{
211-
name: "Test titles",
212-
value: "Titles",
238+
name: "Specs",
239+
value: "Specs",
213240
},
214241
{
215-
name: "Test tags",
216-
value: "Tags",
242+
name: "Test titles or tags (requires cy-grep)",
243+
value: "Tests or tags",
244+
disabled: disableTitleTagChoice,
217245
},
218246
],
219247
required: true,
220248
});
221-
process.env.CY_GREP_FILTER_METHOD = titleOrTagPrompt;
249+
if (specAndTestPrompt.includes("Specs")) {
250+
process.env.TEST_SPECS = true;
251+
}
252+
253+
/*
254+
255+
/*
256+
* NOTE:: Choose test titles or tags
257+
* This requires the cy-grep package
258+
*/
259+
if (specAndTestPrompt.includes("Tests or tags")) {
260+
// Prompt for use to select test titles or tags option
261+
const titleOrTagPrompt = await select({
262+
message: "Choose to filter by specific test titles or tags: ",
263+
multiple: false,
264+
options: [
265+
{
266+
name: "Test titles",
267+
value: "Titles",
268+
},
269+
{
270+
name: "Test tags",
271+
value: "Tags",
272+
},
273+
],
274+
required: true,
275+
});
276+
process.env.CY_GREP_FILTER_METHOD = titleOrTagPrompt;
277+
if (titleOrTagPrompt.includes("Titles")) {
278+
process.env.TEST_TITLES = true;
279+
}
280+
if (titleOrTagPrompt.includes("Tags")) {
281+
process.env.TEST_TAGS = true;
282+
}
283+
}
222284
}
223285
// Arrays for storing specs and/or tests
224286
// If user passes --print-selected
@@ -228,7 +290,7 @@ async function runSelectedSpecs() {
228290
/*
229291
* NOTE:: Spec section
230292
*/
231-
if (specAndTestPrompt.includes("Specs")) {
293+
if (process.env.TEST_SPECS) {
232294
const specs = getSpecs(undefined, process.env.TESTING_TYPE, false);
233295

234296
if (specs.length > 0) {
@@ -334,7 +396,7 @@ async function runSelectedSpecs() {
334396
/*
335397
* NOTE:: Test Title section
336398
*/
337-
if (process.env.CY_GREP_FILTER_METHOD === "Titles") {
399+
if (process.env.TEST_TITLES) {
338400
const specs = getSpecs(undefined, process.env.TESTING_TYPE, false);
339401

340402
if (specs.length > 0) {
@@ -463,7 +525,7 @@ async function runSelectedSpecs() {
463525
/*
464526
* NOTE:: Tags section
465527
*/
466-
if (process.env.CY_GREP_FILTER_METHOD === "Tags") {
528+
if (process.env.TEST_TAGS) {
467529
const specs = getSpecs(undefined, process.env.TESTING_TYPE, false);
468530

469531
if (specs.length > 0) {
@@ -532,19 +594,19 @@ async function runSelectedSpecs() {
532594
// NOTE : --print-selected used to show all selected specs/titles/tags
533595
if (process.argv.includes("--print-selected")) {
534596
findAndRemoveArgv("--print-selected");
535-
if (specAndTestPrompt.includes("Specs")) {
597+
if (process.env.TEST_SPECS) {
536598
console.log("\n");
537599
console.log(pc.bgGreen(pc.black(pc.bold(` Spec(s) selected: `))));
538600
console.log("\n");
539601
console.log(specArr);
540602
}
541-
if (process.env.CY_GREP_FILTER_METHOD === "Titles") {
603+
if (process.env.TEST_TITLES) {
542604
console.log("\n");
543605
console.log(pc.bgGreen(pc.black(pc.bold(` Test(s) selected: `))));
544606
console.log("\n");
545607
console.log(testArr);
546608
}
547-
if (process.env.CY_GREP_FILTER_METHOD === "Tags") {
609+
if (process.env.TEST_TAGS) {
548610
console.log("\n");
549611
console.log(pc.bgGreen(pc.black(pc.bold(` Tag(s) selected: `))));
550612
console.log("\n");

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cypress-cli-select",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "A Cypress cli prompt to select and run specs, tests or tags",
55
"main": "index.js",
66
"bin": {

tests/cli-component.spec.js

+76
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,82 @@ describe("component: basic input prompt flows", () => {
120120
});
121121
});
122122

123+
describe("component: prompt flags skip beginning prompts", () => {
124+
it("handles --specs flag", async () => {
125+
const { findByText, userEvent } = await render("cd ../../../ && node", [
126+
resolve(__dirname, "../index.js"),
127+
["--submit-focused"],
128+
["--component"],
129+
["--specs"],
130+
]);
131+
132+
expect(await findByText("Select specs to run")).toBeInTheConsole();
133+
expect(await findByText("src/components/Clock.cy.js")).toBeInTheConsole();
134+
expect(await findByText("src/components/Stepper.cy.js")).toBeInTheConsole();
135+
136+
userEvent.keyboard("[Enter]");
137+
expect(
138+
await findByText("Select specs to run: src/components/Clock.cy.js"),
139+
).toBeInTheConsole();
140+
expect(await findByText("Running Cypress")).toBeInTheConsole();
141+
});
142+
143+
it("handles --titles flag", async () => {
144+
const { findByText, userEvent } = await render("cd ../../../ && node", [
145+
resolve(__dirname, "../index.js"),
146+
["--submit-focused"],
147+
["--component"],
148+
["--titles"],
149+
]);
150+
151+
expect(await findByText("Select tests to run")).toBeInTheConsole();
152+
expect(
153+
await findByText("Clock.cy.js > <Clock> > mounts"),
154+
).toBeInTheConsole();
155+
expect(
156+
await findByText("Stepper.cy.js > <Stepper> > mounts"),
157+
).toBeInTheConsole();
158+
159+
userEvent.keyboard("[Enter]");
160+
expect(
161+
await findByText("Select tests to run: Clock.cy.js > <Clock> > mounts"),
162+
);
163+
expect(await findByText("Running Cypress")).toBeInTheConsole();
164+
});
165+
166+
it("handles --tags flag", async () => {
167+
const { findByText, userEvent } = await render("cd ../../../ && node", [
168+
resolve(__dirname, "../index.js"),
169+
["--submit-focused"],
170+
["--component"],
171+
["--tags"],
172+
]);
173+
174+
expect(await findByText("Select tags to run")).toBeInTheConsole();
175+
expect(await findByText("@p3")).toBeInTheConsole();
176+
177+
userEvent.keyboard("[ArrowDown]");
178+
userEvent.keyboard("[Enter]");
179+
180+
expect(await findByText("Select tags to run: @p3"));
181+
expect(await findByText("Running Cypress")).toBeInTheConsole();
182+
});
183+
184+
it("cannot pass both --titles and --tags", async () => {
185+
const { findByText, userEvent } = await render("cd ../../../ && node", [
186+
resolve(__dirname, "../index.js"),
187+
["--submit-focused"],
188+
["--component"],
189+
["--titles"],
190+
["--tags"],
191+
]);
192+
193+
expect(
194+
await findByText("Cannot choose both titles and tags"),
195+
).toBeInTheConsole();
196+
});
197+
});
198+
123199
describe("component: print selected displays prior to run", () => {
124200
it("handles spec display", async () => {
125201
const { findByText, userEvent } = await render("cd ../../../ && node", [

0 commit comments

Comments
 (0)