Starchart: Playwright and Chakra UI Setup
Published: 2023-02-04 04:38:53

The past week I worked on setting up [Playwright](https://playwright.dev/), and [Chakra UI](https://chakra-ui.com/) with [starchart](https://github.com/Seneca-CDOT/starchart). Chakra UI is needed so that their components could be used to build the web pages, and Playwright is to be used for end-to-end testing.

### Chakra UI Setup

Setting up Chakra UI was relatively simple. I followed their [instructions](https://chakra-ui.com/getting-started/remix-guide) for the most part. I also found [this example](https://github.com/remix-run/examples/tree/main/chakra-ui) to be really helpful. My process for this went like this: I looked at the instructions in Chakra UI docs for the client, server and root files, and compared them with the existing code in starchart. Then, I also referenced them against the example code. 

Not an ideal process to follow, but since I do not understand Chakra UI or Remix that well yet, I felt that this was the best way to do the setup. It worked, and I created a [PR](https://github.com/Seneca-CDOT/starchart/pull/75). Based on some feedback, I added some basic components (instead of using html tags) to the index page so some manual testing could be done. 

Now, if you followed the PR link, you would have seen that it was merged but had a failing check. And that is what was addressed later while adding Playwright. Since the project was setup using the [Remix blues stack](https://github.com/remix-run/blues-stack), it shipped with [cypress](https://www.cypress.io/) for end-to-end testing. The cypress folder had some tests but these were not modified when the app was setup since we intended to use Playwright. This resulted in the typecheck job failing, since it was also checking the cypress folder: `tsc && tsc cypress`

### Playwright Setup

Setting up Playwright took longer than I had expected. First, I made a list of node modules that needed to be removed. These included `cypress`, `@faker-js/faker`, `@testing-library/cypress`, and `eslint-plugin-cypress`. These were only used by cypress so I uninstalled them using `npm uninstall`.

Next, I deleted the cypress folder, and the configuration file for cypress (`cypress.config.ts`). I installed playwright by using `npm init playwright@latest`, and following the prompts ([docs](https://playwright.dev/docs/intro)). This also generated a config file for playwright, some simple tests in the specified test folder, and some more detailed examples of tests. I deleted the folder containing the examples since it was simply for reference, and added some basic tests.

For the configuration, I ended up using the defaults for most of the options. I also uncommented the configuration for mobile viewpoints, branded browsers, and output directory for test artifacts; and added the following based on some feedback:
```ts
baseURL: `http://localhost:${process.env.PORT}`
...
video: 'on-first-retry'
```
Setting a `baseURl` allows navigating to a page with a relative URL:
```ts
await page.goto('/');
```
The second configuration [records a video](https://playwright.dev/docs/videos) of the test on the first retry after it fails.

#### Trying to Setup CI
So far, this process was pretty simple. Installing playwright also auto generates a GitHub workflow file. I tried adding a new job based on this auto generated file, and updated some npm scripts that were previously being used for cypress to run the tests. However, to run the tests, the app must be started. And to do that, the mysql database container must be running. I tried using the defined docker script, which uses `docker-compose up -d` to start the container but `prisma db push` failed with the following error:

![Error Server has closed the connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78xf12vb9z6md64kuakq.png)

I tried a bunch of things to try and fix this. I posted about this in Slack, and found out that mysql can take a while to start. So, I tried adding a wait before running the tests based on [this discussion](https://github.com/docker-library/mysql/issues/547#issuecomment-1002685663). However, this still resulted in the same error. I even tried doing `prisma db pull` before `prisma db push`. However, I got a different error this time:

![Error THe introspected database was empty](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktsss4boomn5at8z2xqz.png)

This was really strange, since the database should have been setup. Still I did not know why this was failing. 

#### Using Service Containers
Since I was basically out of ideas on how to fix this, I tried completely reworking the job by using a [service container](https://docs.github.com/en/actions/using-containerized-services/about-service-containers) for mysql instead: 

```yml
  E2E-Test:
    timeout-minutes: 15
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:8
        ports:
          - 3306:3306
        env:
          MYSQL_DATABASE: starchart
          MYSQL_USER: starchart
          MYSQL_PASSWORD: starchart_password
          MYSQL_ROOT_PASSWORD: root_password

    steps:
    ...
```

And the tests worked! I am still not sure why they were failing before. Maybe docker-compose doesn't work with GitHub actions? 
With this, I marked the [PR](https://github.com/Seneca-CDOT/starchart/pull/85) ready for review, and the broken CI was fixed.