Setup test project

Change test structure
Generate new testApp from node script and launch app
Add new feature: Be able to user strapi start and specify an app path (WIP) configs are loaded
Generate test API with relations - articles, tag and category
This commit is contained in:
Jim Laurie 2018-05-21 17:04:25 +02:00
parent 6c194ff333
commit e6b4b2a214
8 changed files with 288 additions and 49 deletions

View File

@ -3,6 +3,7 @@
"version": "3.0.0-alpha.12.2",
"devDependencies": {
"assert": "~1.3.0",
"axios": "^0.18.0",
"babel-eslint": "^6.1.2",
"chalk": "^2.4.1",
"eslint": "^3.12.2",
@ -30,7 +31,7 @@
"setup:build": "npm run setup --build",
"setup": "npm run clean:all && npm install ./packages/strapi-lint --save-dev && npm install && node ./scripts/setup.js && npm run clean",
"lint": "node ./scripts/lint.js",
"test": "jest",
"test": "node ./test/start.js",
"prettier": "node ./packages/strapi-lint/lib/internals/prettier/index.js"
},
"author": {

View File

@ -24,16 +24,18 @@ const { cli, logger } = require('strapi-utils');
* (fire up the application in our working directory).
*/
module.exports = function() {
module.exports = function(appPath = '') {
// Check that we're in a valid Strapi project.
if (!cli.isStrapiApp()) {
return logger.error('This command can only be used inside a Strapi project.');
}
appPath = path.join(process.cwd(), appPath);
try {
const strapi = function () {
try {
return require(path.resolve(process.cwd(), 'node_modules', 'strapi'));
return require(path.resolve(appPath, 'node_modules', 'strapi'));
} catch (e) {
return require('strapi'); // eslint-disable-line import/no-unresolved
}
@ -46,7 +48,7 @@ module.exports = function() {
// Require server configurations
const server = require(path.resolve(
process.cwd(),
appPath,
'config',
'environments',
'development',
@ -81,7 +83,7 @@ module.exports = function() {
});
};
setFilesToWatch(process.cwd());
setFilesToWatch(appPath);
@ -124,7 +126,9 @@ module.exports = function() {
}
});
return strapi.start(afterwards);
return strapi.start({
appPath
}, afterwards);
} else {
return;
}
@ -133,7 +137,9 @@ module.exports = function() {
// Otherwise, if no workable local `strapi` module exists,
// run the application using the currently running version
// of `strapi`. This is probably always the global install.
strapi.start(afterwards);
strapi.start({
appPath
}, afterwards);
} catch (e) {
logger.error(e);
process.exit(0);

View File

@ -64,9 +64,11 @@ program
// `$ strapi start`
program
.command('start')
.command('start [appPath]')
.description('start your Strapi application')
.action(require('./strapi-start'));
.action((appPath) => {
require('./strapi-start')(appPath);
});
// `$ strapi generate:api`
program

View File

@ -25,7 +25,8 @@ module.exports = function() {
'./node_modules/strapi-middleware-*',
'./node_modules/strapi-upload-*',
'./node_modules/strapi-lint'
]
],
cwd: this.config.appPath
}, (err, files) => {
if (err) {
return reject(err);

123
test/helper/generators.json Normal file
View File

@ -0,0 +1,123 @@
{
"article": {
"attributes":[
{
"name":"title",
"params":{
"appearance":{
"WYSIWYG":false
},
"multiple":false,
"type":"string"
}
},
{
"name":"content",
"params":{
"appearance":{
"WYSIWYG":true
},
"multiple":false,
"type":"text"
}
},
{
"name":"published",
"params":{
"appearance":{
"WYSIWYG":false
},
"multiple":false,
"type":"boolean"
}
},
{
"name":"cover",
"params":{
"appearance":{
"WYSIWYG":false
},
"multiple":false,
"type":"media"
}
},
{
"name":"author",
"params":{
"nature":"manyToOne",
"target":"user",
"pluginValue":"users-permissions",
"key":"articles",
"plugin":true
}
}
],
"connection":"default",
"name":"article",
"description":"",
"collectionName":""
},
"tag": {
"attributes":[
{
"name":"name",
"params":{
"appearance":{
"WYSIWYG":false
},
"multiple":false,
"type":"string"
}
},
{
"name":"articles",
"params":{
"dominant":true,
"nature":"manyToMany",
"target":"article",
"key":"tags"
}
}
],
"connection":"default",
"name":"tag",
"description":"",
"collectionName":""
},
"category": {
"attributes":[
{
"name":"name",
"params":{
"appearance":{
"WYSIWYG":false
},
"multiple":false,
"type":"string"
}
},
{
"name":"images",
"params":{
"appearance":{
"WYSIWYG":false
},
"multiple":true,
"type":"media"
}
},
{
"name":"articles",
"params":{
"nature":"oneToMany",
"target":"article",
"key":"category"
}
}
],
"connection":"default",
"name":"category",
"description":"",
"collectionName":""
}
}

20
test/helper/restart.js Normal file
View File

@ -0,0 +1,20 @@
module.exports = function (request) {
return new Promise(async (resolve) => {
const ping = async () => {
try {
await request.get('');
} catch (err) {
if (err.response) {
return resolve();
} else {
return setTimeout(() => {
ping();
}, 4000);
}
}
};
ping();
});
};

View File

@ -1,46 +1,48 @@
const exec = require('child_process').exec;
const fs = require('fs-extra');
const path = require('path');
const axios = require('axios');
const bin = path.resolve('./packages/strapi/bin/strapi.js');
const appName = 'testApp';
const request = axios.create({
baseURL: 'http://localhost:1337',
});
describe('Basic setup', () => {
beforeAll(() => {
return new Promise(resolve => {
fs.exists(appName, exists => {
if (exists) {
fs.removeSync(appName);
}
resolve();
});
});
});
const params = require('./helper/generators');
const restart = require('./helper/restart');
describe('App setup auth', () => {
test(
'Create new test app',
() => {
return expect(
new Promise(resolve => {
const appCreation = exec(
`node ${bin} new ${appName} --dev --dbclient=mongo --dbhost=localhost --dbport=27017 --dbname=strapi-test-${new Date().getTime()} --dbusername="" --dbpassword=""`,
);
'Register admin user',
async () => {
const body = await request.post('/auth/local/register', {
username: 'admin',
email: 'admin@strapi.io',
password: 'pcw123'
});
appCreation.stdout.on('data', data => {
if (data.includes('is ready at')) {
appCreation.kill();
return resolve(true);
}
if (data.includes('Database connection has failed')) {
appCreation.kill();
return resolve(false);
}
});
}),
).resolves.toBeTruthy();
},
120000,
axios.defaults.headers.common['Authorization'] = `Bearer ${body.data.jwt}`;
}
);
});
describe('Generate test APIs', () => {
beforeEach(async () => {
await restart(request);
}, 60000);
test(
'Create new article API',
async () => {
await request.post('/content-type-builder/models', params.article);
}
);
test(
'Create new tag API',
async () => {
await request.post('/content-type-builder/models', params.tag);
}
);
test(
'Create new category API',
async () => {
await request.post('/content-type-builder/models', params.category);
}
);
});

84
test/start.js Normal file
View File

@ -0,0 +1,84 @@
const exec = require('child_process').exec;
const fs = require('fs-extra');
const path = require('path');
const strapiBin = path.resolve('./packages/strapi/bin/strapi.js');
const appName = 'testApp';
let appStart;
const {runCLI: jest} = require('jest-cli/build/cli');
const main = async () => {
const clean = () => {
return new Promise((resolve) => {
fs.exists(appName, exists => {
if (exists) {
fs.removeSync(appName);
}
resolve();
});
});
};
const generate = () => {
return new Promise((resolve) => {
const appCreation = exec(
`node ${strapiBin} new ${appName} --dev --dbclient=mongo --dbhost=localhost --dbport=27017 --dbname=strapi-test-${new Date().getTime()} --dbusername="" --dbpassword=""`,
);
appCreation.stdout.on('data', data => {
console.log(data.toString());
if (data.includes('is ready at')) {
appCreation.kill();
return resolve();
}
if (data.includes('Database connection has failed')) {
appCreation.kill();
return reject();
}
});
});
};
const start = () => {
return new Promise((resolve) => {
appStart = exec(
`node ${strapiBin} start ${appName}`,
);
appStart.stdout.on('data', data => {
console.log(data.toString());
if (data.includes('To shut down your server')) {
return resolve();
}
});
});
};
const test = () => {
console.log('Launch test suits');
return new Promise(async (resolve) => {
const options = {
projects: [process.cwd()],
silent: false
};
await jest(options, options.projects);
resolve();
});
};
await clean();
await generate();
await start();
await test();
appStart.kill();
};
main();