Take pr review into account

This commit is contained in:
Convly 2021-01-05 14:24:53 +01:00
parent 579e4c32e9
commit b9edf6ba56
5 changed files with 99 additions and 57 deletions

View File

@ -95,8 +95,6 @@ jobs:
with:
node-version: ${{ matrix.node }}
- uses: ./.github/actions/install-modules
with:
globalPackages: wait-on
- uses: ./.github/actions/run-e2e-tests
with:
dbOptions: '--dbclient=postgres --dbhost=localhost --dbport=5432 --dbname=strapi_test --dbusername=strapi --dbpassword=strapi'
@ -132,8 +130,6 @@ jobs:
with:
node-version: ${{ matrix.node }}
- uses: ./.github/actions/install-modules
with:
globalPackages: wait-on
- uses: ./.github/actions/run-e2e-tests
with:
dbOptions: '--dbclient=mysql --dbhost=localhost --dbport=3306 --dbname=strapi_test --dbusername=strapi --dbpassword=strapi'
@ -152,8 +148,6 @@ jobs:
with:
node-version: ${{ matrix.node }}
- uses: ./.github/actions/install-modules
with:
globalPackages: wait-on
- uses: ./.github/actions/run-e2e-tests
with:
dbOptions: '--dbclient=sqlite --dbfile=./tmp/data.db'
@ -177,8 +171,6 @@ jobs:
with:
node-version: ${{ matrix.node }}
- uses: ./.github/actions/install-modules
with:
globalPackages: wait-on
- uses: ./.github/actions/run-e2e-tests
with:
dbOptions: '--dbclient=mongo --dbhost=localhost --dbport=27017 --dbname=strapi_test'
@ -219,8 +211,6 @@ jobs:
with:
node-version: ${{ matrix.node }}
- uses: ./.github/actions/install-modules
with:
globalPackages: wait-on
- uses: ./.github/actions/run-e2e-tests
with:
dbOptions: '--dbclient=postgres --dbhost=localhost --dbport=5432 --dbname=strapi_test --dbusername=strapi --dbpassword=strapi'
@ -260,8 +250,6 @@ jobs:
with:
node-version: ${{ matrix.node }}
- uses: ./.github/actions/install-modules
with:
globalPackages: wait-on
- uses: ./.github/actions/run-e2e-tests
with:
dbOptions: '--dbclient=mysql --dbhost=localhost --dbport=3306 --dbname=strapi_test --dbusername=strapi --dbpassword=strapi'
@ -284,8 +272,6 @@ jobs:
with:
node-version: ${{ matrix.node }}
- uses: ./.github/actions/install-modules
with:
globalPackages: wait-on
- uses: ./.github/actions/run-e2e-tests
with:
dbOptions: '--dbclient=sqlite --dbfile=./tmp/data.db'
@ -313,8 +299,6 @@ jobs:
with:
node-version: ${{ matrix.node }}
- uses: ./.github/actions/install-modules
with:
globalPackages: wait-on
- uses: ./.github/actions/run-e2e-tests
with:
dbOptions: '--dbclient=mongo --dbhost=localhost --dbport=27017 --dbname=strapi_test'

View File

@ -10,6 +10,12 @@ const filePattern = `*${fileExtension}`;
const defaultPaths = ['packages/'];
/**
* `STRAPI_TEST_PATHS` is an env variable used internally to filter which test are going to be run.
* The expected format is a string containing a space separated list of file or directory.
* eg: STRAPI_TEST_PATHS="packages/strapi-foo/file.test.e2e.js packages/strapi-bar"
* If not initialized, default paths are used (see the `defaultPaths` variable declared above)
*/
const testPaths = process.env.STRAPI_TEST_PATHS;
const paths = testPaths ? testPaths.split(' ') : defaultPaths;

View File

@ -1,6 +1,6 @@
'use strict';
const { concat, merge, isFunction, map } = require('lodash/fp');
const { isFunction, map } = require('lodash/fp');
const modelsUtils = require('../models');
const stringifyDates = object =>
@ -16,14 +16,14 @@ const stringifyDates = object =>
const formatFixtures = map(stringifyDates);
module.exports = {
ct: {
contentType: {
create: contentType => {
let createdModel;
return {
async build(state) {
async build(ctx) {
createdModel = await modelsUtils.createContentType(contentType);
return { ...state, models: [...state.models, createdModel] };
ctx.addModel(createdModel);
},
cleanup: () => modelsUtils.deleteContentType(createdModel.modelName),
};
@ -33,9 +33,9 @@ module.exports = {
let createdModels = [];
return {
async build(state) {
async build(ctx) {
createdModels = await modelsUtils.createContentTypes(contentTypes);
return { ...state, models: concat(state.models, createdModels) };
createdModels.forEach(ctx.addModel);
},
async cleanup() {
for (const model of createdModels) {
@ -49,12 +49,13 @@ module.exports = {
const createdModels = [];
return {
async build(state) {
async build(ctx) {
for (const contentType of contentTypes) {
createdModels.push(await modelsUtils.createContentType(contentType));
}
const model = await modelsUtils.createContentType(contentType);
return { ...state, models: concat(state.models, createdModels) };
createdModels.push(model);
ctx.addModel(model);
}
},
async cleanup() {
for (const model of createdModels) {
@ -64,14 +65,14 @@ module.exports = {
};
},
},
comp: {
component: {
create: component => {
let createdModel;
return {
async build(state) {
async build(ctx) {
createdModel = await modelsUtils.createComponent(component);
return { ...state, models: [...state.models, createdModel] };
ctx.addModel(createdModel);
},
cleanup: () => modelsUtils.deleteComponent(createdModel.uid),
};
@ -82,14 +83,15 @@ module.exports = {
let createdEntries = [];
return {
async build(state) {
async build(ctx) {
createdEntries = formatFixtures(
await modelsUtils.createFixturesFor(
modelName,
isFunction(entries) ? entries(getFixtures()) : entries
)
);
return { ...state, fixtures: merge(state.fixtures, { [modelName]: createdEntries }) };
ctx.addFixtures(modelName, createdEntries);
},
cleanup: () => modelsUtils.deleteFixturesFor(modelName, createdEntries),
};

View File

@ -0,0 +1,43 @@
'use strict';
const { merge } = require('lodash/fp');
const getDefaultState = () => ({ actions: [], models: [], fixtures: {} });
const createContext = initialState => {
let state;
const contextApi = {
get state() {
return state;
},
addAction(action) {
state.actions.push(action);
return this;
},
addModel(model) {
state.models.push(model);
return this;
},
addFixtures(modelName, entries) {
state.fixtures = merge(state.fixtures, { [modelName]: entries });
return this;
},
resetState() {
return this.setState({ ...getDefaultState(), ...initialState });
},
setState(newState) {
state = newState;
return this;
},
};
return contextApi.resetState();
};
module.exports = { createContext };

View File

@ -1,30 +1,24 @@
'use strict';
const { omit, get } = require('lodash/fp');
const { get } = require('lodash/fp');
const _ = require('lodash');
const modelsUtils = require('../models');
const { sanitizeEntity } = require('../../../packages/strapi-utils');
const actionRegistry = require('./action-registry');
const { createContext } = require('./context');
const createTestBuilder = (options = {}) => {
const { initialState } = options;
const omitActions = omit('actions');
const getDefaultState = () => ({ actions: [], models: [], fixtures: {}, ...initialState });
const state = getDefaultState();
const addAction = (code, ...params) => {
const action = get(code, actionRegistry);
state.actions.push(action(...params));
};
const ctx = createContext(initialState);
return {
get models() {
return state.models;
return ctx.state.models;
},
get fixtures() {
return state.fixtures;
return ctx.state.fixtures;
},
sanitizedFixtures(strapi) {
@ -42,45 +36,58 @@ const createTestBuilder = (options = {}) => {
return this.fixtures[modelName];
},
addContentType(contentType) {
addAction('ct.create', contentType);
addAction(code, ...params) {
const actionCreator = get(code, actionRegistry);
ctx.addAction(actionCreator(...params));
return this;
},
addContentType(contentType) {
return this.addAction('contentType.create', contentType);
},
addContentTypes(contentTypes, { batch = true } = {}) {
addAction(batch ? 'ct.createBatch' : 'ct.createMany', contentTypes);
return this;
return this.addAction(
batch ? 'contentType.createBatch' : 'contentType.createMany',
contentTypes
);
},
addComponent(component) {
addAction('comp.create', component);
return this;
return this.addAction('component.create', component);
},
addFixtures(model, entries) {
addAction('fixtures.create', model, entries, () => this.fixtures);
return this;
return this.addAction('fixtures.create', model, entries, () => this.fixtures);
},
async build() {
for (const action of state.actions) {
const newState = await action.build(omitActions(state));
Object.assign(state, newState);
for (const action of ctx.state.actions) {
await action.build(ctx);
}
return this;
},
async cleanup(options = {}) {
const { enableTestDataAutoCleanup = true } = options;
const { models, actions } = ctx.state;
if (enableTestDataAutoCleanup) {
for (const model of state.models.reverse()) {
for (const model of models.reverse()) {
await modelsUtils.cleanupModel(model.uid || model.modelName);
}
}
for (const action of state.actions.reverse()) {
await action.cleanup();
for (const action of actions.reverse()) {
await action.cleanup(ctx);
}
ctx.resetState();
return this;
},
};
};