Skip to content

Commit

Permalink
Cleanup testing helpers and configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebodin committed Mar 12, 2019
1 parent a0b45e2 commit 5c6618c
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 138 deletions.
24 changes: 15 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,25 @@ before_script:
- psql -c 'create database strapi_test;' -U postgres

script:
# run snyk
- npm run -s test:snyk

# default unit test
- npm run test:unit
- npm run -s test:unit

# e2e trests
- node test/createTestApp.js --dbclient=postgres --dbhost=localhost --dbport=5432 --dbname=strapi_test --dbusername=postgres --dbpassword=
- node test/startTestApp.js & wait-on http://localhost:1337
- npm run test:e2e
- npm run -s test:generate-app --dbclient=postgres --dbhost=localhost --dbport=5432 --dbname=strapi_test --dbusername=postgres --dbpassword=
- npm run -s test:start-app & wait-on http://localhost:1337
- npm run -s test:e2e

# cypress tests
- node test/createTestApp.js --dbclient=mongo --dbhost=localhost --dbport=27017 --dbname=strapi_test --dbusername= --dbpassword=
- node test/startTestApp.js & wait-on http://localhost:1337
- node test/cypress.js
- npm run -s test:generate-app --dbclient=mongo --dbhost=localhost --dbport=27017 --dbname=strapi_test --dbusername= --dbpassword=
- npm run -s test:start-app & wait-on http://localhost:1337
- npm run -s cypress:run

# lint and doc
- npm run lint
- npm run doc
- npm run -s lint
- npm run -s doc

cache:
directories:
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@
"removesymlinkdependencies": "node ./scripts/removeSymlinkDependencies.js",
"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",
"prettier": "node ./packages/strapi-lint/lib/internals/prettier/index.js",
"lint": "node ./scripts/lint.js",
"snyk": "node ./scripts/snyk.js",
"test:snyk": "snyk test",
"test:unit": "FORCE_COLOR=true lerna run test --parallel",
"prettier": "node ./packages/strapi-lint/lib/internals/prettier/index.js",
"snyk": "node ./scripts/snyk.js",
"test:e2e": "FORCE_COLOR=true jest --config jest.config.e2e.js --runInBand --verbose --forceExit --detectOpenHandles --no-cache"
"test:e2e": "FORCE_COLOR=true jest --config jest.config.e2e.js --runInBand --verbose --forceExit --detectOpenHandles",
"test:generate-app": "node test/createTestApp.js",
"test:start-app": "node test/startTestApp.js",
"cypress:run": "node test/cypress"
},
"author": {
"email": "[email protected]",
Expand Down
11 changes: 5 additions & 6 deletions packages/strapi-generate-api/test/endpoint.test.e2e.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Helpers.
const { auth, login } = require('../../../test/helpers/auth');
const form = require('../../../test/helpers/generators');
const restart = require('../../../test/helpers/restart');
const waitRestart = require('../../../test/helpers/waitRestart');
const createRequest = require('../../../test/helpers/request');

const cleanDate = entry => {
Expand All @@ -13,7 +13,6 @@ const cleanDate = entry => {

let data;
let rq;
jest.setTimeout(30000);

describe('Create Strapi API End to End', () => {
beforeAll(async () => {
Expand All @@ -33,8 +32,8 @@ describe('Create Strapi API End to End', () => {
});

describe('Generate test APIs', () => {
beforeEach(() => restart(), 30000);
afterAll(() => restart(), 30000)
beforeEach(() => waitRestart(), 30000);
afterAll(() => waitRestart(), 30000);

test('Create new article API', async () => {
await rq({
Expand Down Expand Up @@ -667,8 +666,8 @@ describe('Create Strapi API End to End', () => {
});

describe('Delete test APIs', () => {
beforeEach(() => restart(), 30000);
afterAll(() => restart(), 30000)
beforeEach(() => waitRestart(), 30000);
afterAll(() => waitRestart(), 30000);

test('Delete article API', async () => {
await rq({
Expand Down
16 changes: 8 additions & 8 deletions packages/strapi-plugin-content-manager/test/index.test.e2e.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Helpers.
const { auth, login } = require('../../../test/helpers/auth');
const form = require('../../../test/helpers/generators');
const restart = require('../../../test/helpers/restart');
const waitRestart = require('../../../test/helpers/waitRestart');
const createRequest = require('../../../test/helpers/request');

const cleanDate = entry => {
Expand All @@ -14,15 +14,16 @@ const cleanDate = entry => {
let data;

let rq;
jest.setTimeout(30000);

describe('Content Manager End to End', () => {
beforeAll(async () => {
await createRequest()({
url: '/auth/local/register',
method: 'POST',
body: auth,
}).catch(() => {});
}).catch(err => {
console.log(err);
});

const body = await login();

Expand All @@ -34,8 +35,8 @@ describe('Content Manager End to End', () => {
});

describe('Generate test APIs', () => {
beforeEach(() => restart(), 30000);
afterAll(() => restart(), 30000)
beforeEach(() => waitRestart(), 30000);
afterAll(() => waitRestart(), 30000);

test('Create new article API', async () => {
await rq({
Expand Down Expand Up @@ -90,7 +91,6 @@ describe('Content Manager End to End', () => {

describe('Test manyToMany relation (article - tag) with Content Manager', () => {
beforeAll(async () => {

data = {
articles: [],
tags: [],
Expand Down Expand Up @@ -729,8 +729,8 @@ describe('Content Manager End to End', () => {
});

describe('Delete test APIs', () => {
beforeEach(() => restart(), 30000);
afterAll(() => restart(), 30000)
beforeEach(() => waitRestart(), 30000);
afterAll(() => waitRestart(), 30000);

test('Delete article API', async () => {
await rq({
Expand Down
17 changes: 9 additions & 8 deletions packages/strapi/bin/strapi-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const { cli, logger } = require('strapi-utils');
* @param {Strapi} options.strapi - Strapi instance
*/
const watchFileChanges = ({ appPath, strapi }) => {
const restart = path => {
const restart = () => {
if (strapi.reload.isWatching && !strapi.reload.isReloading) {
strapi.reload.isReloading = true;
strapi.reload();
Expand All @@ -35,7 +35,8 @@ const watchFileChanges = ({ appPath, strapi }) => {
const watcher = chokidar.watch(appPath, {
ignoreInitial: true,
ignored: [
/(^|[\/\\])\../,
/(^|[/\\])\../,
'**/tmp',
'**/admin',
'**/components',
'**/documentation',
Expand All @@ -44,22 +45,22 @@ const watchFileChanges = ({ appPath, strapi }) => {
'**/index.html',
'**/public',
'**/cypress',
'**/*.db',
'**/*.db*',
],
});

watcher
.on('add', path => {
strapi.log.info(`File created: ${path}`);
restart(path);
restart();
})
.on('change', path => {
strapi.log.info(`File changed: ${path}`);
restart(path);
restart();
})
.on('unlink', path => {
strapi.log.info(`File deleted: ${path}`);
restart(path);
restart();
});
};

Expand Down Expand Up @@ -115,7 +116,7 @@ module.exports = function(appPath = '') {
break;
case 'stop':
worker.kill();
process.exit(0);
process.exit(1);
break;
default:
return;
Expand Down Expand Up @@ -162,7 +163,7 @@ module.exports = function(appPath = '') {
);
} catch (e) {
logger.error(e);
process.exit(0);
process.exit(1);
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/strapi/lib/Strapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class Strapi extends EventEmitter {
}

// Kill process.
process.exit(0);
process.exit(1);
}

async load() {
Expand Down
2 changes: 1 addition & 1 deletion test/createTestApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const databases = {
};

const main = async () => {
const database = process.argv.length > 2 ? process.argv.slice(2).join(' ') : databases.mongo;
const database = process.argv.length > 2 ? process.argv.slice(2).join(' ') : databases.postgres;

try {
await cleanTestApp(appName);
Expand Down
11 changes: 9 additions & 2 deletions test/cypress.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ const config = Object.assign(

cypress
.run(config)
.then(() => {
.then(results => {
if (results.totalFailed > 0) {
process.stdout.write('Cypress tests finished with errors\n', () => {
process.exit(1);
});
}
process.exit(0);
})
.catch(err => {
console.error(err);
process.exit(1);
process.stdout.write('Error running cypress', () => {
process.exit(1);
});
});
52 changes: 19 additions & 33 deletions test/e2e.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const fs = require('fs');
const path = require('path');
const { cleanTestApp, generateTestApp, startTestApp } = require('./helpers/testAppGenerator');
const shelljs = require('shelljs');
const execa = require('execa');
const waitOn = require('wait-on');

const appName = 'testApp';

Expand All @@ -11,58 +10,45 @@ const databases = {
postgres:
'--dbclient=postgres --dbhost=127.0.0.1 --dbport=5432 --dbname=strapi_test --dbusername=strapi --dbpassword=strapi',
mysql:
'--dbclient=mysql --dbhost=127.0.0.1 --dbport=3306 --dbname=strapi-test --dbusername=root --dbpassword=root',
'--dbclient=mysql --dbhost=127.0.0.1 --dbport=3306 --dbname=strapi_test --dbusername=strapi --dbpassword=strapi',
sqlite: '--dbclient=sqlite --dbfile=./tmp/data.db',
};

const { runCLI: jest } = require('jest-cli/build/cli');

const test = async () => {
const child = execa.shell('npm run test:e2e', {
stdio: 'pipe',
return execa.shell('npm run -s test:e2e', {
stdio: 'inherit',
cwd: path.resolve(__dirname, '..'),
env: {
CI: true,
FORCE_COLOR: 1,
},
});

child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);

return child;
};

const main = async () => {
const database = process.argv.length > 2 ? process.argv.slice(2).join(' ') : databases.postgres;

await cleanTestApp(appName);
await generateTestApp({ appName, database });
const database = process.argv.length > 2 ? process.argv.slice(2).join(' ') : databases.mysql;

const { testApp, ready, end } = startTestApp({ appName });
try {
await cleanTestApp(appName);
await generateTestApp({ appName, database });
const testAppProcess = startTestApp({ appName });

await ready;
await waitOn({ resources: ['http://localhost:1337'] });

// stop tests if the testApp stops
end
.then(() => {
process.stdout.write('testApp exited before the end', () => {
process.exit(1);
});
})
.catch(err => {
process.stdout.write('testApp exited before the end with error', () => {
await test().catch(() => {
testAppProcess.kill();
process.stdout.write('Tests failed\n', () => {
process.exit(1);
});
});

await test().catch(() => {
process.stdout.write('Tests failed', () => {
testAppProcess.kill();
process.exit(0);
} catch (error) {
process.stdout.write('Tests failed\n', () => {
process.exit(1);
});
});

process.kill(testApp.pid);
process.exit(0);
}
};

main();
Loading

0 comments on commit 5c6618c

Please sign in to comment.