Add forceExit param to jest runner

This commit is contained in:
Alexandre Bodin 2019-03-08 17:54:45 +01:00
parent 5987357aa7
commit f4b5a487d1
6 changed files with 111 additions and 143 deletions

View File

@ -24,6 +24,7 @@
"pre-commit": "~1.1.2",
"redux-saga": "^0.14.3",
"request": "^2.87.0",
"request-promise-native": "^1.0.7",
"shelljs": "^0.7.7",
"snyk": "^1.99.0",
"strapi-lint": "file:packages/strapi-lint"
@ -41,7 +42,7 @@
"test": "snyk test && node ./test/start.js",
"prettier": "node ./packages/strapi-lint/lib/internals/prettier/index.js",
"snyk": "node ./scripts/snyk.js",
"test:e2e": "jest --config jest.config.e2e.js --runInBand --verbose packages/strapi-generate-api"
"test:e2e": "jest --config jest.config.e2e.js --runInBand --verbose --forceExit packages/strapi-generate-api"
},
"author": {
"email": "hi@strapi.io",

View File

@ -2,7 +2,7 @@
const { login } = require('../../../test/helpers/auth');
const form = require('../../../test/helpers/generators');
const restart = require('../../../test/helpers/restart');
const rq = require('../../../test/helpers/request');
const createRequest = require('../../../test/helpers/request');
const cleanDate = entry => {
delete entry.updatedAt;
@ -13,12 +13,14 @@ const cleanDate = entry => {
let data;
const sleep = time => new Promise(resolve => setTimeout(resolve, time));
let rq;
jest.setTimeout(30000);
beforeAll(async () => {
const body = await login();
rq.defaults({
rq = createRequest({
headers: {
Authorization: `Bearer ${body.jwt}`,
},
@ -28,12 +30,13 @@ beforeAll(async () => {
describe('Generate test APIs', () => {
beforeEach(() => restart(), 60000);
test('Create new article API', async () => {
await rq({
test('Create new article API', () => {
return rq({
url: '/content-type-builder/models',
method: 'POST',
body: form.article,
json: true,
}).then(res => {
expect(res.statusCode).toBe(200);
});
});
@ -42,7 +45,8 @@ describe('Generate test APIs', () => {
url: '/content-type-builder/models',
method: 'POST',
body: form.tag,
json: true,
}).then(res => {
expect(res.statusCode).toBe(200);
});
});
@ -51,7 +55,8 @@ describe('Generate test APIs', () => {
url: '/content-type-builder/models',
method: 'POST',
body: form.category,
json: true,
}).then(res => {
expect(res.statusCode).toBe(200);
});
});
@ -60,7 +65,8 @@ describe('Generate test APIs', () => {
url: '/content-type-builder/models',
method: 'POST',
body: form.reference,
json: true,
}).then(res => {
expect(res.statusCode).toBe(200);
});
});
@ -69,7 +75,8 @@ describe('Generate test APIs', () => {
url: '/content-type-builder/models',
method: 'POST',
body: form.product,
json: true,
}).then(res => {
expect(res.statusCode).toBe(200);
});
});
});
@ -85,13 +92,12 @@ describe('Test manyToMany relation (article - tag) with Content Manager', () =>
});
test('Create tag1', async () => {
let body = await rq({
const { body } = await rq({
url: '/tags',
method: 'POST',
body: {
name: 'tag1',
},
json: true,
});
data.tags.push(body);
@ -102,13 +108,12 @@ describe('Test manyToMany relation (article - tag) with Content Manager', () =>
});
test('Create tag2', async () => {
let body = await rq({
const { body } = await rq({
url: '/tags',
method: 'POST',
body: {
name: 'tag2',
},
json: true,
});
data.tags.push(body);
@ -119,13 +124,12 @@ describe('Test manyToMany relation (article - tag) with Content Manager', () =>
});
test('Create tag3', async () => {
let body = await rq({
const { body } = await rq({
url: '/tags',
method: 'POST',
body: {
name: 'tag3',
},
json: true,
});
data.tags.push(body);
@ -141,11 +145,10 @@ describe('Test manyToMany relation (article - tag) with Content Manager', () =>
content: 'My super content 1',
};
let body = await rq({
const { body } = await rq({
url: '/articles',
method: 'POST',
body: entry,
json: true,
});
data.articles.push(body);
@ -164,11 +167,10 @@ describe('Test manyToMany relation (article - tag) with Content Manager', () =>
tags: [data.tags[0]],
};
let body = await rq({
const { body } = await rq({
url: '/articles',
method: 'POST',
body: entry,
json: true,
});
data.articles.push(body);
@ -188,11 +190,10 @@ describe('Test manyToMany relation (article - tag) with Content Manager', () =>
cleanDate(entry);
let body = await rq({
const { body } = await rq({
url: `/articles/${entry.id}`,
method: 'PUT',
body: entry,
json: true,
});
data.articles[0] = body;
@ -212,11 +213,10 @@ describe('Test manyToMany relation (article - tag) with Content Manager', () =>
cleanDate(entry);
let body = await rq({
const { body } = await rq({
url: `/articles/${entry.id}`,
method: 'PUT',
body: entry,
json: true,
});
data.articles[0] = body;
@ -234,11 +234,10 @@ describe('Test manyToMany relation (article - tag) with Content Manager', () =>
cleanDate(entry);
let body = await rq({
const { body } = await rq({
url: `/articles/${entry.id}`,
method: 'PUT',
body: entry,
json: true,
});
data.articles[0] = body;
@ -257,11 +256,10 @@ describe('Test manyToMany relation (article - tag) with Content Manager', () =>
cleanDate(entry);
let body = await rq({
const { body } = await rq({
url: `/articles/${entry.id}`,
method: 'PUT',
body: entry,
json: true,
});
data.articles[0] = body;
@ -283,13 +281,12 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
});
test('Create cat1', async () => {
let body = await rq({
const { body } = await rq({
url: '/categories',
method: 'POST',
body: {
name: 'cat1',
},
json: true,
});
data.categories.push(body);
@ -300,13 +297,12 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
});
test('Create cat2', async () => {
let body = await rq({
const { body } = await rq({
url: '/categories',
method: 'POST',
body: {
name: 'cat2',
},
json: true,
});
data.categories.push(body);
@ -323,11 +319,10 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
category: data.categories[0],
};
let body = await rq({
const { body } = await rq({
url: '/articles',
method: 'POST',
body: entry,
json: true,
});
data.articles.push(body);
@ -346,11 +341,10 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
cleanDate(entry);
let body = await rq({
const { body } = await rq({
url: `/articles/${entry.id}`,
method: 'PUT',
body: entry,
json: true,
});
data.articles[0] = body;
@ -368,11 +362,10 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
content: 'Content 2',
};
let body = await rq({
const { body } = await rq({
url: '/articles',
method: 'POST',
body: entry,
json: true,
});
data.articles.push(body);
@ -390,11 +383,10 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
cleanDate(entry);
let body = await rq({
const { body } = await rq({
url: `/articles/${entry.id}`,
method: 'PUT',
body: entry,
json: true,
});
data.articles[1] = body;
@ -412,11 +404,10 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
cleanDate(entry);
let body = await rq({
const { body } = await rq({
url: `/categories/${entry.id}`,
method: 'PUT',
body: entry,
json: true,
});
data.categories[0] = body;
@ -433,11 +424,10 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
articles: [data.articles[0]],
};
let body = await rq({
const { body } = await rq({
url: '/categories',
method: 'POST',
body: entry,
json: true,
});
data.categories.push(body);
@ -449,10 +439,9 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
});
test('Get article1 with cat3', async () => {
let body = await rq({
const { body } = await rq({
url: `/articles/${data.articles[0].id}`,
method: 'GET',
json: true,
});
expect(body.id);
@ -460,10 +449,9 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
});
test('Get article2 with cat2', async () => {
let body = await rq({
const { body } = await rq({
url: `/articles/${data.articles[1].id}`,
method: 'GET',
json: true,
});
expect(body.id);
@ -471,10 +459,9 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
});
test('Get cat1 without relations', async () => {
let body = await rq({
const { body } = await rq({
url: `/categories/${data.categories[0].id}`,
method: 'GET',
json: true,
});
expect(body.id);
@ -482,10 +469,9 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
});
test('Get cat2 with article2', async () => {
let body = await rq({
const { body } = await rq({
url: `/categories/${data.categories[1].id}`,
method: 'GET',
json: true,
});
expect(body.id);
@ -494,10 +480,9 @@ describe('Test oneToMany - manyToOne relation (article - category) with Content
});
test('Get cat3 with article1', async () => {
let body = await rq({
const { body } = await rq({
url: `/categories/${data.categories[2].id}`,
method: 'GET',
json: true,
});
expect(body.id);
@ -515,13 +500,12 @@ describe('Test oneToOne relation (article - reference) with Content Manager', ()
});
test('Create ref1', async () => {
let body = await rq({
const { body } = await rq({
url: '/references',
method: 'POST',
body: {
name: 'ref1',
},
json: true,
});
data.references.push(body);
@ -536,11 +520,10 @@ describe('Test oneToOne relation (article - reference) with Content Manager', ()
content: 'Content 1',
};
let body = await rq({
const { body } = await rq({
url: '/articles',
method: 'POST',
body: entry,
json: true,
});
data.articles.push(body);
@ -557,11 +540,10 @@ describe('Test oneToOne relation (article - reference) with Content Manager', ()
cleanDate(entry);
let body = await rq({
const { body } = await rq({
url: `/articles/${entry.id}`,
method: 'PUT',
body: entry,
json: true,
});
data.articles[0] = body;
@ -579,11 +561,10 @@ describe('Test oneToOne relation (article - reference) with Content Manager', ()
reference: data.references[0].id,
};
let body = await rq({
const { body } = await rq({
url: '/articles',
method: 'POST',
body: entry,
json: true,
});
data.articles.push(body);
@ -597,42 +578,38 @@ describe('Test oneToOne relation (article - reference) with Content Manager', ()
describe('Test oneWay relation (reference - tag) with Content Manager', () => {
test('Attach Tag to a Reference', async () => {
const tagToCreate = await rq({
await rq({
url: '/tags',
method: 'POST',
json: true,
body: {
name: 'tag111',
},
}).then(({ body: tagToCreate }) => {
return rq({
url: '/references',
method: 'POST',
body: {
name: 'cat111',
tag: tagToCreate,
},
}).then(({ body }) => {
expect(body.tag.id).toBe(tagToCreate.id);
});
});
const referenceToCreate = await rq({
url: '/references',
method: 'POST',
json: true,
body: {
name: 'cat111',
tag: tagToCreate,
},
});
expect(referenceToCreate.tag.id).toBe(tagToCreate.id);
});
test('Detach Tag to a Reference', async () => {
const tagToCreate = await rq({
const { body: tagToCreate } = await rq({
url: '/tags',
method: 'POST',
json: true,
body: {
name: 'tag111',
},
});
const referenceToCreate = await rq({
const { body: referenceToCreate } = await rq({
url: '/references',
method: 'POST',
json: true,
body: {
name: 'cat111',
tag: tagToCreate,
@ -641,10 +618,9 @@ describe('Test oneWay relation (reference - tag) with Content Manager', () => {
expect(referenceToCreate.tag.id).toBe(tagToCreate.id);
const referenceToUpdate = await rq({
const { body: referenceToUpdate } = await rq({
url: `/references/${referenceToCreate.id}`,
method: 'PUT',
json: true,
body: {
tag: null,
},
@ -654,45 +630,34 @@ describe('Test oneWay relation (reference - tag) with Content Manager', () => {
});
test('Delete Tag so the relation in the Reference side should be removed', async () => {
const tagToCreate = await rq({
const { body: tagToCreate } = await rq({
url: '/tags',
method: 'POST',
json: true,
body: {
name: 'tag111',
},
});
const referenceToCreate = await rq({
const { body: referenceToCreate } = await rq({
url: '/references',
method: 'POST',
json: true,
body: {
name: 'cat111',
tag: tagToCreate,
},
});
const tagToDelete = await rq({
await rq({
url: `/tags/${tagToCreate.id}`,
method: 'DELETE',
json: true,
});
const referenceToGet = await rq({
const { body: referenceToGet } = await rq({
url: `/references/${referenceToCreate.id}`,
method: 'GET',
json: true,
});
try {
if (Object.keys(referenceToGet.tag).length == 0) {
referenceToGet.tag = null;
}
} catch (err) {
// Silent
}
if (Object.keys(referenceToGet.tag).length == 0) return;
expect(referenceToGet.tag).toBe(null);
});
});
@ -704,7 +669,8 @@ describe('Delete test APIs', () => {
await rq({
url: '/content-type-builder/models/article',
method: 'DELETE',
json: true,
}).then(res => {
expect(res.statusCode).toBe(200);
});
});
@ -712,7 +678,8 @@ describe('Delete test APIs', () => {
await rq({
url: '/content-type-builder/models/tag',
method: 'DELETE',
json: true,
}).then(res => {
expect(res.statusCode).toBe(200);
});
});
@ -720,7 +687,8 @@ describe('Delete test APIs', () => {
await rq({
url: '/content-type-builder/models/category',
method: 'DELETE',
json: true,
}).then(res => {
expect(res.statusCode).toBe(200);
});
});
@ -728,7 +696,8 @@ describe('Delete test APIs', () => {
await rq({
url: '/content-type-builder/models/reference',
method: 'DELETE',
json: true,
}).then(res => {
expect(res.statusCode).toBe(200);
});
});
@ -736,7 +705,8 @@ describe('Delete test APIs', () => {
await rq({
url: '/content-type-builder/models/product',
method: 'DELETE',
json: true,
}).then(res => {
expect(res.statusCode).toBe(200);
});
});
});

View File

@ -1,15 +1,13 @@
const { auth } = require('./helpers/auth');
const rq = require('./helpers/request');
const restart = require('./helpers/restart');
const createReq = require('./helpers/request');
// const sleep = time => new Promise(resolve => setTimeout(resolve, time));
const rq = createReq();
module.exports = async () => {
await rq({
url: '/auth/local/register',
method: 'POST',
body: auth,
json: true,
}).catch(err => {
console.log(err);
throw err;

View File

@ -1,26 +1,27 @@
const rq = require('./request');
const createRq = require('./request');
const auth = {
username: 'admin',
email: 'admin@strapi.io',
password: 'pcw123'
password: 'pcw123',
};
const rq = createRq();
module.exports = {
auth,
login: () => {
return new Promise(async (resolve) => {
const body = await rq({
url: `/auth/local`,
return new Promise(async resolve => {
const res = await rq({
url: '/auth/local',
method: 'POST',
body: {
identifier: auth.email,
password: auth.password
password: auth.password,
},
json: true
});
resolve(body);
resolve(res.body);
});
}
},
};

View File

@ -1,29 +1,27 @@
let request = require('request');
const request = require('request-promise-native');
const restart = require('./restart');
request = request.defaults({
baseUrl: 'http://localhost:1337',
});
module.exports = function(options) {
const params = JSON.parse(JSON.stringify(options));
for (let key in params.formData) {
if (typeof params.formData[key] === 'object') {
params.formData[key] = JSON.stringify(params.formData[key]);
}
}
return new Promise((resolve, reject) => {
request(params, (err, res, body) => {
if (err || res.statusCode < 200 || res.statusCode >= 300) {
return reject(err || body);
}
return resolve(body);
});
const createReq = (defaults = {}) => {
const client = request.defaults({
baseUrl: 'http://localhost:1337',
json: true,
resolveWithFullResponse: true,
...defaults,
});
return async options => {
await restart(1000);
const params = JSON.parse(JSON.stringify(options));
for (let key in params.formData) {
if (typeof params.formData[key] === 'object') {
params.formData[key] = JSON.stringify(params.formData[key]);
}
}
return client(params);
};
};
module.exports.defaults = function(options) {
request = request.defaults(options);
};
module.exports = createReq;

View File

@ -1,6 +1,6 @@
const request = require('request');
module.exports = function(initTime = 3000) {
module.exports = function(initTime = 8000) {
const ping = async () => {
return new Promise((resolve, reject) => {
// set timeout to avoid ping indefinitely