Search code examples
javascriptbashplaywright

Send parameter to package.json script


I have this script on package.json file:

"test:debug": "yarn build && playwright test --project=chromium-debug -c build && ts-node ./src/logs/generateLog.ts"

When I run this command: yarn test:debug -g 'myTestName' I see in the terminal that it runs this command:

yarn build && playwright test --project=chromium-debug -c build && ts-node ./src/logs/generateLog.ts -g 'myTestName'

but I want it to run this command:

yarn build && playwright test --project=chromium-debug -c build -g 'myTestName' && ts-node ./src/logs/generateLog.ts

What should I change in my script in order to achieve that?


Solution

  • You can define the command in package.json like that:

    "test:debug": "yarn build && playwright test --project=chromium-debug -c build -g 'myTestName' && ts-node ./src/logs/generateLog.ts"
    

    And then simply just run yarn test:debug


    EDIT

    A workaround would be to use a Makefile to achieve your desired result instead, but first make sure that make is installed on your device.

    In your project root folder. Create a new Makefile and put this simple command in it (don’t forget to save the Makefile):

    test:
        yarn build && playwright test --project=chromium-debug -c build -g '$(g)' && ts-node ./src/logs/generateLog.ts
    

    (Notice the indentation in the second line, in your IDE you should replace the indentation of the second line to use a TAB instead of 4 spaces like I did here otherwise make will fire an error)

    Now calling the command in a terminal would be like that:

    make test g="myTestName"
    

    This will let you to tweak the ”g” variable when calling the command and it will achieve the desired result.