Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing arguments to test script #88

Closed
bahmutov opened this issue Aug 10, 2018 · 9 comments
Closed

Allow passing arguments to test script #88

bahmutov opened this issue Aug 10, 2018 · 9 comments

Comments

@bahmutov
Copy link
Owner

And maybe to start script to make it simple to refine commands without defining new npm scripts

Currently, let's say we have npm test command

{
  "scripts": {
    "start": "server",
    "cy": "cypress run",
    "test": "start-test :3000 cy"
  }
}

and the CI build command could be simply npm test

Now let's say we want to record tests on Cypress dashboard, this would be simply cypress run --record, but how to pass this from npm test? Right now need to define custom commands

{
  "scripts": {
    "start": "server",
    "cy": "cypress run",
    "cy:record": "cypress run --record",
    "test": "start-test :3000 cy",
    "test:ci": "start-test :3000 cy:record"
  }
}

and set CI script to use npm run test:ci

So too much effort to pass --record argument. It would be much nicer to pass something like this from CI script

npm test -- --test-args "--record"

which this package would append to its "test" command and would run npm run cy -- --record

@12finger
Copy link

trying to understand what is going on here... something fishy, or i just don't see the whole picture, yet?

this lazyAss throws false positive when the arguments are 4 ! how can that be ?
so it seems to be checking something wrong..

notification-service-template-engine 2019-08-28 16-08-58

} else {
la(
cliArgs.length === 3,
'expected <NPM script name that starts server> <url or port> <NPM script name that runs tests>\n',
'example: start-test start 8080 test\n',
'see https://github.com/bahmutov/start-server-and-test#use\n'
)
start = cliArgs[0]
url = normalizeUrl(cliArgs[1])
test = cliArgs[2]
}

so when uncommenting the call to lazy ass (lines 35-40) and when passing extra args
eg.

npm run test:visual -- -u

to update Jest snapshots, all works nice and smoothly,

while scripts being:

"preview": "node -r esm local-live-preview/server.js"
"jest:visual": "jest -c jest-visual.config.js",
"test:visual": "NODE_ENV=test start-server-and-test preview 8885 jest:visual",

can anybody with more knowledge about both start-server-and-test and lazy-ass chime in, and elaborate on this behavior, possibly @bahmutov ? to me it seems like there is something going wrong in lazyAss which hinders `start-server-and-test' functionality.

@bahmutov
Copy link
Owner Author

bahmutov commented Aug 28, 2019

@12finger right, the -- -u NPM script argument, unfortunately, adds 4th argument to the command, NOT appending to the 3rd argument. My suggestion in your case: just add another script to update jest snapshots

"jest:visual:update": "jest -c jest-visual.config.js -u",
"test:visual:update": "NODE_ENV=test start-server-and-test preview 8885 jest:visual:update"

@12finger
Copy link

@bahmutov sure, that's exactly what i did in the end. i wrote extra extra "helpers".
there actually is a charm to it as well, as it is nice on the eyes to simply type

npm run test:snapshot:update

instead of typing ... -- --u all the time to pass those extra arguments around is ugly.

But really, my question was about understanding what goes on there, as clearly the test says

63863429-8555dc80-c9ae-11e9-9c39-656bd513b048 png (1369×843) 2019-08-29 17-13-14

length = 4

and it still fires the Error! ?

I'll might have a look at la and give it a shot at adding this func to it.

@SubjectiveReality
Copy link

Looks like neither fork fix has merged in yet... anybody still looking at this? I'm looking at a use case of specifying specs when running cypress, for which there doesn't seem to be a good alternative. Particularly not if the spec file arguments are passed in dynamically.

@uni0ue
Copy link

uni0ue commented Jan 27, 2020

Got the same issue at the moment. Trying to pass a "spec" dynamically as an argument to run a specific cypress test only and lazyAss is failing.

@bahmutov
Copy link
Owner Author

Please use the command form shown in https://github.com/bahmutov/start-server-and-test#commands For example to run a single spec

{
  "scripts": {
    "ci": "start-server-and-test 'http-server -c-1 --silent' 8080 'cypress run --spec cypress/integration/location.spec.js'"
  }
}

Or you can move http-server part into its own start script, which is used by default and have the equivalent JSON

{
  "scripts": {
    "start": "http-server -c-1 --silent",
    "ci": "start-server-and-test 8080 'cypress run --spec cypress/integration/location.spec.js'"
  }
}

@DanaGoyette
Copy link

This is closed, yet neither fixed nor marked "won't fix."

I'd like to be able to have scripts something like this, while being able to pass random args on the fly to cypress.

"local": "cross-env API_URL=http://localhost:1234 UI_PORT=8080 react-scripts start",
"remote": "cross-env API_URL=https://remote.example.com UI_PORT=8080 react-scripts start",
"cypress:local": "cross-env API_URL=http://localhost:1234 cypress"
"cypress:remote": "cross-env API_URL=https://remote.example.com cypress",
"cypress:local:open": "start-server-and-test 'local' 8080 'cypress:local open'"
"cypress:remote:open": "start-server-and-test 'remote' 8080 'cypress:local open'",

@bmmathe
Copy link

bmmathe commented Feb 12, 2021

I would also like this fixed as we are passing in optional test values from the command line to manipulate the test.

@flaviencathala
Copy link

As a workaround to inject parameters to a command, we can use an environment variable
"test": "cross-env-shell 'start-server-and-test server-command 8080 \"test-command $TEST_PARAMS\"'"
and run
TEST_PARAMS="--spec my-spec-file" npm run test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants