mirror of
https://github.com/Baboo7/strapi-plugin-import-export-entries.git
synced 2025-09-04 00:02:40 -04:00
feat: Setup tests
This commit is contained in:
parent
435b5f9b8b
commit
8d044cf1e5
@ -14,7 +14,8 @@
|
||||
"commonjs": true,
|
||||
"es6": true,
|
||||
"browser": true,
|
||||
"node": true
|
||||
"node": true,
|
||||
"jest/globals": true
|
||||
},
|
||||
"globals": {
|
||||
"strapi": true
|
||||
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -129,4 +129,5 @@ dist
|
||||
.yarn/install-state.gz
|
||||
.pnp.*
|
||||
|
||||
.DS_Store
|
||||
.DS_Store
|
||||
.strapi-updater.json
|
10
jest.config.js
Normal file
10
jest.config.js
Normal 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,
|
||||
};
|
16
package.json
16
package.json
@ -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
17
tests/app.spec.js
Normal 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
60
tests/helpers/strapi.js
Normal 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,
|
||||
};
|
BIN
tests/helpers/test-app/.tmp/data.sqlite
Normal file
BIN
tests/helpers/test-app/.tmp/data.sqlite
Normal file
Binary file not shown.
8
tests/helpers/test-app/config/admin.js
Normal file
8
tests/helpers/test-app/config/admin.js
Normal file
@ -0,0 +1,8 @@
|
||||
module.exports = ({ env }) => ({
|
||||
auth: {
|
||||
secret: 'ZMnEjgypXhdIkDmOSQ+fdQ==',
|
||||
},
|
||||
apiToken: {
|
||||
salt: 'XmXugdchV1oo4H5QnwJFSg==',
|
||||
},
|
||||
});
|
7
tests/helpers/test-app/config/api.js
Normal file
7
tests/helpers/test-app/config/api.js
Normal file
@ -0,0 +1,7 @@
|
||||
module.exports = {
|
||||
rest: {
|
||||
defaultLimit: 25,
|
||||
maxLimit: 100,
|
||||
withCount: true,
|
||||
},
|
||||
};
|
11
tests/helpers/test-app/config/database.js
Normal file
11
tests/helpers/test-app/config/database.js
Normal 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,
|
||||
},
|
||||
});
|
17
tests/helpers/test-app/config/middlewares.js
Normal file
17
tests/helpers/test-app/config/middlewares.js
Normal 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',
|
||||
];
|
19
tests/helpers/test-app/config/plugins.js
Normal file
19
tests/helpers/test-app/config/plugins.js
Normal 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);
|
||||
};
|
7
tests/helpers/test-app/config/server.js
Normal file
7
tests/helpers/test-app/config/server.js
Normal 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=='],
|
||||
},
|
||||
});
|
5
tests/helpers/test-app/package.json
Normal file
5
tests/helpers/test-app/package.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"strapi": {
|
||||
"uuid": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
|
||||
}
|
||||
}
|
0
tests/helpers/test-app/public/uploads/dummytxt
Normal file
0
tests/helpers/test-app/public/uploads/dummytxt
Normal file
20
tests/helpers/test-app/src/index.js
Normal file
20
tests/helpers/test-app/src/index.js
Normal 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
19
tests/utils/index.js
Normal 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
3
tests/utils/pluginId.js
Normal file
@ -0,0 +1,3 @@
|
||||
const pluginId = 'import-export-entries';
|
||||
|
||||
module.exports = pluginId;
|
Loading…
x
Reference in New Issue
Block a user