Skip to content

Commit

Permalink
chore(tests): backport e2e config (#19654)
Browse files Browse the repository at this point in the history
  • Loading branch information
markkaylor committed Mar 5, 2024
1 parent 20c4c0d commit 97eb67b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .github/actions/run-e2e-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ inputs:
description: 'Should run EE or CE e2e tests'
jestOptions:
description: 'Jest options'
enableFutureFeatures:
description: 'Enable future unstable features'
runs:
using: 'composite'
steps:
- run: $GITHUB_ACTION_PATH/script.sh
env:
RUN_EE: ${{ inputs.runEE }}
JEST_OPTIONS: ${{ inputs.jestOptions }}
STRAPI_FEATURES_FUTURE_RELEASES_SCHEDULING: ${{ inputs.enableFutureFeatures }}
shell: bash
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ jobs:
uses: ./.github/actions/run-e2e-tests
with:
runEE: true
enableFutureFeatures: true
jestOptions: --project=${{ matrix.project }}

- uses: actions/upload-artifact@v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ front-workspace.code-workspace
playwright-report
test-results
!e2e/data/*.tar
e2e/.env

############################
# Strapi
Expand Down
8 changes: 8 additions & 0 deletions docs/docs/guides/e2e/00-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ Playwright enables reliable end-to-end testing for modern web apps. It's cross b

For more information check out their [docs](https://playwright.dev/docs/intro). If you're struggling with their APIs, then check out their specific [API documentation](https://playwright.dev/docs/api/class-playwright).

## Running tests with environment variables

To set specific environment variables for your tests, a `.env` file can be created in the root of the e2e folder. This is useful if you need to run tests with a Strapi license or set future flags.

## Running tests with future flags

If you are writing tests for an unstable future feature you will need to add `app-template/config/features.js`. Currently the app template generation does not take the config folder into consideration. However, the run-e2e-tests script will apply the features config to the generated app. See the documentation for [features.js](https://docs.strapi.io/dev-docs/configurations/features#enabling-a-future-flag)

## What makes a good end to end test?

This is the million dollar question. E2E tests typically test complete user flows that touch numerous points of the application it's testing, we're not interested in what happens during a process, only the user perspective and end results. Consider writing them with your story hat on. E.g. "As a user I want to create a new entity, publish that entity, and then be able to retrieve its data from the content API".
Expand Down
5 changes: 5 additions & 0 deletions e2e/app-template/config/features.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = ({ env }) => ({
future: {
contentReleasesScheduling: env.bool('STRAPI_FEATURES_FUTURE_RELEASES_SCHEDULING', false),
},
});
7 changes: 0 additions & 7 deletions e2e/tests/content-releases/releases-page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ describeOnCondition(edition === 'EE')('Releases page', () => {
await resetDatabaseAndImportDataFromPath('./e2e/data/with-admin.tar');
await page.goto('/admin');
await login({ page });

await page.evaluate(() => {
// Remove after Scheduling Beta release
window.strapi.future = {
isEnabled: () => true,
};
});
});

test('A user should be able to create a release without scheduling it and view their pending and done releases', async ({
Expand Down
62 changes: 51 additions & 11 deletions test/scripts/run-e2e-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,55 @@ const execa = require('execa');
const fs = require('node:fs/promises');
const yargs = require('yargs');

const chalk = require('chalk');
const dotenv = require('dotenv');
const { cleanTestApp, generateTestApp } = require('../helpers/test-app');
const { createConfig } = require('../../playwright.base.config');
const chalk = require('chalk');

const cwd = path.resolve(__dirname, '../..');
const testAppDirectory = path.join(cwd, 'test-apps', 'e2e');
const testRoot = path.join(cwd, 'e2e');
const templateDir = path.join(testRoot, 'app-template');

const pathExists = async (path) => {
try {
await fs.access(path);
return true;
} catch (err) {
return false;
}
};

/**
* Updates the env file for a generated test app
* - Removes the PORT key/value from generated app .env
* - Uses e2e/app-template/config/features.js to enable future features in the generated app
*/
const setupTestEnvironment = async (generatedAppPath) => {
/**
* Because we're running multiple test apps at the same time
* and the env file is generated by the generator with no way
* to override it, we manually remove the PORT key/value so when
* we set it further down for each playwright instance it works.
*/
const pathToEnv = path.join(generatedAppPath, '.env');
const envFile = (await fs.readFile(pathToEnv)).toString();
const envWithoutPort = envFile.replace('PORT=1337', '');
await fs.writeFile(pathToEnv, envWithoutPort);

/*
* Enable future features in the generated app manually since a template
* does not allow the config folder.
*/
const testRootFeaturesConfigPath = path.join(templateDir, 'config', 'features.js');
const hasFeaturesConfig = await pathExists(testRootFeaturesConfigPath);

if (!hasFeaturesConfig) return;

const configFeatures = await fs.readFile(testRootFeaturesConfigPath);
const appFeaturesConfigPath = path.join(generatedAppPath, 'config', 'features.js');
await fs.writeFile(appFeaturesConfigPath, configFeatures);
};

yargs
.parserConfiguration({
Expand Down Expand Up @@ -51,6 +94,11 @@ yargs
},
handler: async (argv) => {
try {
if (await pathExists(path.join(testRoot, '.env'))) {
// Run tests with the env variables specified in the e2e/app-template/.env
dotenv.config({ path: path.join(testRoot, '.env') });
}

const { concurrency, domains, setup } = argv;

/**
Expand Down Expand Up @@ -109,16 +157,8 @@ yargs
template: path.join(cwd, 'e2e', 'app-template'),
link: true,
});
/**
* Because we're running multiple test apps at the same time
* and the env file is generated by the generator with no way
* to override it, we manually remove the PORT key/value so when
* we set it further down for each playwright instance it works.
*/
const pathToEnv = path.join(appPath, '.env');
const envFile = (await fs.readFile(pathToEnv)).toString();
const envWithoutPort = envFile.replace('PORT=1337', '');
await fs.writeFile(pathToEnv, envWithoutPort);

await setupTestEnvironment(appPath);
})
);

Expand Down

0 comments on commit 97eb67b

Please sign in to comment.