Skip to content

Commit

Permalink
test(content-releases): add end-to-end tests (#19407)
Browse files Browse the repository at this point in the history
  • Loading branch information
markkaylor committed Feb 6, 2024
1 parent 53caa29 commit bbc0605
Show file tree
Hide file tree
Showing 43 changed files with 2,686 additions and 226 deletions.
15 changes: 15 additions & 0 deletions .github/actions/run-e2e-tests/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: 'Run e2e tests'
description: 'Run e2e tests'
inputs:
runEE:
description: 'Should run EE or CE e2e tests'
jestOptions:
description: 'Jest options'
runs:
using: 'composite'
steps:
- run: $GITHUB_ACTION_PATH/script.sh
env:
RUN_EE: ${{ inputs.runEE }}
JEST_OPTIONS: ${{ inputs.jestOptions }}
shell: bash
10 changes: 10 additions & 0 deletions .github/actions/run-e2e-tests/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## disable EE if options not set
if [[ -z "$RUN_EE" ]]; then
export STRAPI_DISABLE_EE=true
else
export STRAPI_DISABLE_LICENSE_PING=true
fi

jestOptions=($JEST_OPTIONS)

yarn test:e2e --setup --concurrency=1 "${jestOptions[@]}"
55 changes: 50 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ jobs:
- name: Run test
run: yarn nx affected --target=test:front --nx-ignore-cycles -- --runInBand

e2e:
e2e_ce:
timeout-minutes: 60
needs: [changes, build, typescript, unit_front]
name: 'e2e (browser: ${{ matrix.project }})'
name: '[CE] e2e (browser: ${{ matrix.project }})'
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -179,13 +179,58 @@ jobs:
- name: Monorepo build
uses: ./.github/actions/run-build

- name: Run E2E tests
run: yarn test:e2e --setup --concurrency=1 --project=${{ matrix.project }}
- name: Run [CE] E2E tests
uses: ./.github/actions/run-e2e-tests
with:
runEE: false
jestOptions: --project=${{ matrix.project }}

- uses: actions/upload-artifact@v4
if: failure()
with:
name: ce-playwright-trace
path: test-apps/e2e/**/test-results/**/trace.zip
retention-days: 1

e2e_ee:
timeout-minutes: 60
needs: [changes, build, typescript, unit_front]
name: '[EE] e2e (browser: ${{ matrix.project }})'
runs-on: ubuntu-latest
env:
STRAPI_LICENSE: ${{ secrets.strapiLicense }}
strategy:
fail-fast: false
matrix:
project: ['chromium', 'webkit', 'firefox']
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-node@v4
with:
node-version: 20

- name: Monorepo install
uses: ./.github/actions/yarn-nm-install

- name: Install Playwright Browsers
run: npx playwright@1.38.1 install --with-deps

- name: Monorepo build
uses: ./.github/actions/run-build

- name: Run [EE] E2E tests
uses: ./.github/actions/run-e2e-tests
with:
runEE: true
jestOptions: --project=${{ matrix.project }}

- uses: actions/upload-artifact@v4
if: failure()
with:
name: playwright-trace
name: ee-playwright-trace
path: test-apps/e2e/**/test-results/**/trace.zip
retention-days: 1

Expand Down
29 changes: 24 additions & 5 deletions docs/docs/guides/e2e/01-app-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,36 @@ An app template has been created in `e2e/app-template` which provide some custom

Here you can read about what content schemas the test instance has & the API customisations we've built (incl. why we built them).

## Update the app template

To update the app template:

- run the tests to create a Strapi app based on the existing template at `test-apps/e2e/test-app-<number>`.
- Move into this folder and run `yarn develop`.
- Login using the credentials found in `e2e/constants.js`.
- Make any changes you need (i.e. create a content-type).
- Kill the server and run [[`yarn strapi templates:generate <path>`]](https://docs.strapi.io/dev-docs/cli#strapi-templatesgenerate).
- Replace the existing template in `e2e/app-template` with the newly generated one.

## Content Schemas

:::note
There's no content yet!
:::
### Article

A collection type, the schema can be found in: `e2e/app-template/template/src/api/article/content-types/article/schema.json`

### Author

A collection type, the schema can be found in `e2e/app-template/template/src/api/article/content-types/author/schema.json`

### Homepage

A single type, the schema can be found in `e2e/app-template/template/src/api/homepage/content-types/homepage/schema.json`

## API Customisations

### Database

found at `template/src/api/database`
Found at `template/src/api/database`

#### Usage

Expand All @@ -46,7 +65,7 @@ This endpoint `DELETES` every row from every table _excluding_ the "core" tables
#### Why do we have it?

This lets us wipe the entire test instance _if_ we need to. DTS does technically
does this already for us. But nonetheless, its useful.
do this already for us. But nonetheless, its useful.

### Config

Expand Down
29 changes: 14 additions & 15 deletions docs/docs/guides/e2e/02-data-transfer.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ This document looks particularly at the `@strapi/data-transfer` package framed a

## Why use Data Transfer?

Each test should be isolated and contained e.g. if you edit an entity in one test, the next test shouldn't know or care about it otherwise your tests need to be ran in a specific order and become flakey quite quickly.
Each test should be isolated and contained e.g. if you edit an entity in one test, the next test shouldn't know or care about it otherwise your tests need to run in a specific order and become flakey quite quickly.

So to solve this, you could use custom API endpoints of the application and whilst this isn't a poor solution it would _most likely_ require some code writing to set up the data for the schema entries. However, in `4.6.0` Strapi released the `DTS` feature, DTS – Data transfer system. This means any member of Strapi can export the data of their instance producing a `.tar` that we can then import programatically restoring the database to this point in time and ensuring a "pure" test environment.
So to solve this, you could use custom API endpoints of the application and whilst this isn't a poor solution it would _most likely_ require some code writing to set up the data for the schema entries. However, in `4.6.0` Strapi released the `DTS` feature (DTS – Data Transfer System). This means any member of Strapi can export the data of their instance producing a `.tar` that we can then import programatically restoring the database to this point in time and ensuring a "pure" test environment.

## Using Data Transfer

The full documentation of the feature can be seen [here](https://docs.strapi.io/developer-docs/latest/developer-resources/data-management.html). Below are a couple of scenarios you might find yourself.

### Creating a data packet

Because we're using an Strapi template for the test instance, it makes the most sense to add/edit the dataset in said templated instance. Begin by creating the instance:
Because we're using a Strapi template for the test instance, it makes the most sense to add/edit the dataset in said templated instance. Begin by creating the instance:

```shell
yarn test:e2e
Expand All @@ -32,7 +32,7 @@ yarn test:e2e
Then, you should be able to navigate to the app – `cd ./test-apps/test-app-XX`, the current content schemas should already be defined in there so you will be able to instantly import the current data packet to bring to life the test instance (instead of it being fresh):

```shell
yarn strapi import --file ../../../e2e/data/backup.tar
yarn strapi import --file ../../../e2e/data/<backup-file-name>.tar
```

Once that's completed, you should be able to run your Strapi instance as usual:
Expand All @@ -41,27 +41,26 @@ Once that's completed, you should be able to run your Strapi instance as usual:
yarn develop
```

If you change any of the content schemas (including adding new ones) be sure to update the `app-template` otherwise DTS may have trouble importing the data as it cannot create schema files on the fly.
If you change any of the content schemas (including adding new ones) be sure to [update the `app-template`](./01-app-template.md) otherwise DTS will fail to import the data for schemas that do not exist.

### Exporting a data packet

Once you've created your new data from the test instance, you'll need to export
it. Data can be exported using the DTS CLI. e.g.
it. Since the Strapi CLI will use `@strapi/data-transfer` directly it will by default not export admin users, API tokens, or any other features that have been included in its exclusion list. For this reason, do not use the export command on the strapi test instance. A DTS engine has been created specifically for our tests cases. This allows us to redefine what should be included in the export for our tests. The script can be found in `/e2e/scripts/dts-export.js`

Be sure to include the content types you would like exported in the `ALLOWED_CONTENT_TYPES` array found in `e2e/constants.js`.

The script accepts the backup destination filename as a parameter. Run it from the directory
of your strapi test insance to create the backup.

```shell
yarn strapi export --file backup --no-encrypt --no-compress
node <PATH_TO_SCRIPT>/dts-export.js backup-with-admin-user
```

This may be sufficient in some cases. However, by default the strapi CLI does
not export admin users or API tokens. There may be cases where we need this data
in our backups (e.g. a backup that contains admin users for testing login). For
this purpose we have the script `/e2e/scripts/dts-export.js`

The script accepts the backup filename as a parameter. Run it from the directory
of your strapi application to create a backup. e.g.
If you are exporting data for an EE feature you will need to run the script with the `STRAPI_LICENSE` env

```shell
node PATH_TO_SCRIPT/dts-export.js backup-with-admin-user
STRAPI_LICENSE=<license-with-ee-feature> node <PATH_TO_SCRIPT>/dts-export.js backup-with-admin-user
```

Once this has been done, add the `.tar` backup to `/e2e/data` so the helper
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"kind": "collectionType",
"collectionName": "articles",
"info": {
"singularName": "article",
"pluralName": "articles",
"displayName": "Article",
"description": ""
},
"options": {
"draftAndPublish": true
},
"pluginOptions": {
"i18n": {
"localized": true
}
},
"attributes": {
"title": {
"type": "string"
},
"content": {
"type": "blocks"
},
"authors": {
"type": "relation",
"relation": "manyToMany",
"target": "api::author.author",
"inversedBy": "articles"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

/**
* testing controller
* article controller
*/

const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::testing.testing');
module.exports = createCoreController('api::article.article');

0 comments on commit bbc0605

Please sign in to comment.