jan-molak / npm-failsafe

Executes a sequence of npm scripts and returns the correct exit code should any of them fail.
Apache License 2.0
10 stars 5 forks source link

What about flagless arguments? #585

Open TurekBot opened 6 months ago

TurekBot commented 6 months ago

If I want to pass my script a flagless argument (help me out with a better phrase if there is one), how do I do that?

For example, Playwright takes folders or files as a flagless argument: npx playwright test tests/todo-page.spec.ts

I tried

But it still ran all the tests

npm test -- spec/recording_items.spec.ts

> serenity-js-playwright-test-template@3.0.0 test
> failsafe clean test:execute [...] test:report spec/recording_items.spec.ts

[clean] 
[clean] > serenity-js-playwright-test-template@3.0.0 clean
[clean] > rimraf playwright-report target
[clean] 
[failsafe] Script 'clean' exited with code 0
[test:execute] 
[test:execute] > serenity-js-playwright-test-template@3.0.0 test:execute
[test:execute] > playwright test
[test:execute] 
[test:execute] 
[test:execute] Running 33 tests using 5 workers
               [4/33] [chromium] › multi-actor_scenarios.spec.ts:39:41 › Multi-actor scenarios › Todo List App › supports multiple actors using separate browsers › Alice starts with a list 
               [4/33] [chromium] › multi-actor_scenarios.spec.ts:18:9 › Multi-actor scenarios › Todo List App › supports multiple actors using separate browsers › Alice starts with a list c
               [4/33] [chromium] › multi-actor_scenarios.spec.ts:9:18 › Multi-actor scenarios › Todo List App › supports multiple actors using separate browsers › Alice starts with a list c
               [4/33] [chromium] › blended_testing.spec.ts:34:30 › 

How would you recommend I approach a situation like this?

TurekBot commented 6 months ago

I did find that if I offer a second --, it works as I hoped it would—but is that the way? Or is there a better approach?

npm test -- -- spec/recording_items.spec.ts

> serenity-js-playwright-test-template@3.0.0 test
> failsafe clean test:execute [...] test:report -- spec/recording_items.spec.ts

[clean] 
[clean] > serenity-js-playwright-test-template@3.0.0 clean
[clean] > rimraf playwright-report target
[clean] 
[failsafe] Script 'clean' exited with code 0
[test:execute] 
[test:execute] > serenity-js-playwright-test-template@3.0.0 test:execute
[test:execute] > playwright test spec/recording_items.spec.ts
[test:execute] 
[test:execute] 
[test:execute] Running 12 tests using 5 workers
               [5/12] [chromium] › recording_items.spec.ts:83:37 › Recording items › Todo List App › should show #main and #footer sections only when list contains items › Alice starts with
               [5/12] [chromium] › recording_items.spec.ts:9:18 › Recording items › Todo List App › should show #main and #footer sections only when list contains items › Alice starts with 
               [5/12] [chromium] › recording_items.spec.ts:61:37 › Recording
TurekBot commented 2 months ago

@jan-molak, do you know if using a second -- is the only way, or are there other options?

jan-molak commented 2 months ago

Hey @TurekBot, I believe using the double separator -- is the only option, given how NPM works.

When your NPM script is defined as follows:

"test": "failsafe clean test:execute [...] test:report",

Running npm test -- tests/todo-page.spec.ts makes NPM append tests/todo-page.spec.ts to the original script command, which then becomes equivalent to:

"test": "failsafe clean test:execute [...] test:report tests/todo-page.spec.ts",

This makes Failsafe "think" that tests/todo-page.spec.ts is another script you want to execute as part of your chain, rather than a positional argument as you intended.

However, if you run npm test -- -- tests/todo-page.spec.ts, it will make NPM append -- tests/todo-page.spec.ts to the original script command, which then becomes:

"test": "failsafe clean test:execute [...] test:report -- tests/todo-page.spec.ts",

... and now tests/todo-page.spec.ts is passed correctly.

Having said that, maybe @muhqu has some idea how we could support this behaviour?