- Building Enterprise JavaScript Applications
- Daniel Li
- 253字
- 2021-07-23 16:31:30
Making a standalone E2E test script
But, we're not done yet! We can definitely improve our testing workflow even further. At the moment, to run our E2E test we have to ensure the following:
- An Elasticsearch instance is running
- We use dotenv-cli to load our test environment and then run our API server
While we could simply note down these instructions in a README.md file, it'll provide a better developer experience if we provide a single command to run, which will automatically load up Elasticsearch, set the right environment, run our API server, run our tests, and tear everything down once it's done.
This seems too much logic to fit into one line of npm script; instead, we can write a shell script, which allows us to specify this logic inside a file. We will use a Bash as the shell language, as it is the most popular and widely-supported shell.
Let's begin by creating a new directory called scripts, adding a new file inside it called e2e.test.sh, and setting its file permission so it's executable:
$ mkdir scripts && touch scripts/e2e.test.sh && chmod +x scripts/e2e.test.sh
Then, update our test:e2e npm script to execute the shell script instead of running the cucumber-js command directly:
"test:e2e": "dotenv -e envs/test.env -e envs/.env ./scripts/e2e.test.sh",