feat: Setup tests

This commit is contained in:
Baboo7 2022-09-03 15:48:15 +02:00
parent 435b5f9b8b
commit 8d044cf1e5
19 changed files with 11417 additions and 79 deletions

View File

@ -14,7 +14,8 @@
"commonjs": true,
"es6": true,
"browser": true,
"node": true
"node": true,
"jest/globals": true
},
"globals": {
"strapi": true

3
.gitignore vendored
View File

@ -129,4 +129,5 @@ dist
.yarn/install-state.gz
.pnp.*
.DS_Store
.DS_Store
.strapi-updater.json

10
jest.config.js Normal file
View File

@ -0,0 +1,10 @@
module.exports = {
testRegex: '(/__tests__/.*|(\\.|/)(spec))\\.[jt]sx?$',
fakeTimers: {
enableGlobally: true,
now: new Date('2022-09-01T09:00:00.000+00:00').getTime(),
},
testPathIgnorePatterns: ['/node_modules/', '.tmp', '.cache'],
testEnvironment: 'node',
verbose: true,
};

View File

@ -12,7 +12,9 @@
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"prettier:check": "yarn prettier --check .",
"prettier:write": "yarn prettier --write ."
"prettier:write": "yarn prettier --write .",
"test": "jest --forceExit --detectOpenHandles --runInBand",
"test:watch": "jest --forceExit --detectOpenHandles --runInBand --watch"
},
"dependencies": {
"@monaco-editor/react": "4.4.5",
@ -27,9 +29,17 @@
"@babel/core": "^7.18.5",
"@babel/eslint-parser": "^7.18.2",
"@babel/preset-react": "^7.17.12",
"eslint": "^8.18.0",
"@strapi/plugin-i18n": "^4.3.6",
"@strapi/plugin-seo": "^1.7.4",
"@strapi/plugin-users-permissions": "^4.3.6",
"@strapi/strapi": "^4.3.6",
"eslint": "^8.23.0",
"eslint-plugin-jest": "^27.0.1",
"eslint-plugin-react": "^7.30.1",
"prettier": "^2.7.1"
"jest": "^29.0.2",
"prettier": "^2.7.1",
"sqlite3": "^5.0.11",
"supertest": "^6.2.4"
},
"peerDependencies": {
"@strapi/strapi": "^4.0.0"

17
tests/app.spec.js Normal file
View File

@ -0,0 +1,17 @@
const { setupStrapi, cleanupStrapi, cleanupDatabase } = require('./helpers/strapi');
beforeAll(async () => {
await setupStrapi();
});
afterAll(async () => {
await cleanupStrapi();
});
afterEach(async () => {
cleanupDatabase();
});
it('strapi should be defined', () => {
expect(strapi).toBeDefined();
});

60
tests/helpers/strapi.js Normal file
View File

@ -0,0 +1,60 @@
const Strapi = require('@strapi/strapi');
const fs = require('fs');
const DELETE_DB_ENABLED = false;
const CLEANUP_DB_ENABLED = true;
let instance;
async function setupStrapi() {
if (!instance) {
await Strapi({
appDir: `${__dirname}/test-app`,
serveAdminPanel: false,
autoReload: false,
}).load();
instance = strapi;
await instance.server.mount();
}
return instance;
}
async function cleanupStrapi() {
//close server to release the db-file
await strapi.server.httpServer.close();
//delete test database after all tests have completed
const dbSettings = strapi.config.get('database.connection.connection');
if (DELETE_DB_ENABLED && dbSettings && dbSettings.filename) {
const tmpDbFile = dbSettings.filename;
if (fs.existsSync(tmpDbFile)) {
fs.unlinkSync(tmpDbFile);
}
}
// close the connection to the database
await strapi.db.connection.destroy();
}
async function cleanupDatabase() {
if (CLEANUP_DB_ENABLED) {
const cleaningCollections = Array.from(strapi.db.metadata)
.map(([collectionName]) => collectionName)
.filter((collectionName) => collectionName.startsWith('api::'))
.map((collectionName) =>
strapi.db.query(collectionName).deleteMany({
where: {
createdAt: { $gt: '1900-01-01T00:00:00.000Z' },
},
}),
);
await Promise.all(cleaningCollections);
}
}
module.exports = {
cleanupDatabase,
setupStrapi,
cleanupStrapi,
};

Binary file not shown.

View File

@ -0,0 +1,8 @@
module.exports = ({ env }) => ({
auth: {
secret: 'ZMnEjgypXhdIkDmOSQ+fdQ==',
},
apiToken: {
salt: 'XmXugdchV1oo4H5QnwJFSg==',
},
});

View File

@ -0,0 +1,7 @@
module.exports = {
rest: {
defaultLimit: 25,
maxLimit: 100,
withCount: true,
},
};

View File

@ -0,0 +1,11 @@
const path = require('path');
module.exports = ({ env }) => ({
connection: {
client: 'sqlite',
connection: {
filename: path.join(__dirname, '..', '.tmp/data.sqlite'),
},
useNullAsDefault: true,
},
});

View File

@ -0,0 +1,17 @@
module.exports = [
'strapi::errors',
'strapi::security',
'strapi::cors',
'strapi::poweredBy',
'strapi::logger',
'strapi::query',
{
name: 'strapi::body',
config: {
jsonLimit: '10mb',
},
},
'strapi::session',
'strapi::favicon',
'strapi::public',
];

View File

@ -0,0 +1,19 @@
const { join } = require('path');
module.exports = ({ env }) => ({
'import-export-entries': {
enabled: true,
resolve: resolveFromRoot(),
config: {
serverPublicHostname: 'http://localhost:1337',
},
},
seo: {
enabled: true,
resolve: resolveFromRoot('node_modules/@strapi/plugin-seo'),
},
});
const resolveFromRoot = (path = '') => {
return join(__dirname, '../../../../', path);
};

View File

@ -0,0 +1,7 @@
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
app: {
keys: ['refAMfTXgOzzoyZgNzAM7Q==', 'chxdTpEu9mpAjrikTsXCJw==', 'ATFaTMWzubRix0aQequdhw==', 'PF+DucUasw98EdH9ysY+ig=='],
},
});

View File

@ -0,0 +1,5 @@
{
"strapi": {
"uuid": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
}
}

View File

@ -0,0 +1,20 @@
"use strict";
module.exports = {
/**
* An asynchronous register function that runs before
* your application is initialized.
*
* This gives you an opportunity to extend code.
*/
register(/*{ strapi }*/) {},
/**
* An asynchronous bootstrap function that runs before
* your application gets started.
*
* This gives you an opportunity to set up your data model,
* run jobs, or perform some special logic.
*/
bootstrap(/*{ strapi }*/) {},
};

19
tests/utils/index.js Normal file
View File

@ -0,0 +1,19 @@
const pluginId = require('./pluginId');
/**
* ServiceName.
* @typedef {("export"|"import")} ServiceName
*/
/**
* Get a plugin service.
* @param {ServiceName} serviceName
* @returns
*/
const getService = (serviceName) => {
return strapi.plugin(pluginId).service(serviceName);
};
module.exports = {
getService,
};

3
tests/utils/pluginId.js Normal file
View File

@ -0,0 +1,3 @@
const pluginId = 'import-export-entries';
module.exports = pluginId;

11271
yarn.lock

File diff suppressed because it is too large Load Diff