chore(tests): backport e2e config (#19654)

This commit is contained in:
markkaylor 2024-03-05 09:09:44 +01:00 committed by GitHub
parent 20c4c0d001
commit 97eb67b367
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 69 additions and 18 deletions

View File

@ -5,6 +5,8 @@ inputs:
description: 'Should run EE or CE e2e tests'
jestOptions:
description: 'Jest options'
enableFutureFeatures:
description: 'Enable future unstable features'
runs:
using: 'composite'
steps:
@ -12,4 +14,5 @@ runs:
env:
RUN_EE: ${{ inputs.runEE }}
JEST_OPTIONS: ${{ inputs.jestOptions }}
STRAPI_FEATURES_FUTURE_RELEASES_SCHEDULING: ${{ inputs.enableFutureFeatures }}
shell: bash

View File

@ -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

1
.gitignore vendored
View File

@ -146,6 +146,7 @@ front-workspace.code-workspace
playwright-report
test-results
!e2e/data/*.tar
e2e/.env
############################
# Strapi

View File

@ -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".

View File

@ -0,0 +1,5 @@
module.exports = ({ env }) => ({
future: {
contentReleasesScheduling: env.bool('STRAPI_FEATURES_FUTURE_RELEASES_SCHEDULING', false),
},
});

View File

@ -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 ({

View File

@ -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({
@ -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;
/**
@ -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);
})
);