diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 6ae0b0dfcc..0000000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,28 +0,0 @@ -version: 2 -jobs: - build: - docker: - # specify the version you desire here - - image: circleci/node:9.11.1 - - working_directory: ~/repo - - steps: - - checkout - - # Download and cache dependencies - - restore_cache: - keys: - - v1-dependencies-{{ checksum "package.json" }} - # fallback to using the latest cache if no exact match is found - - v1-dependencies- - - - run: npm install - - - save_cache: - paths: - - node_modules - key: v1-dependencies-{{ checksum "package.json" }} - - # run tests! - - run: npm test:unit diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000000..6e24875c75 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,33 @@ +name: Node.js CI + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [10.x, 12.x] + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - name: Cache node modules + uses: actions/cache@v1 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- + - run: npm install + - run: nohup npm run server & + env: + NODE_ENV: test + - run: npm run test:int # In-memory db + api tests + env: + CI: true diff --git a/src/express/testingEndpoints.js b/src/express/testingEndpoints.js new file mode 100644 index 0000000000..b4b2e3ba27 --- /dev/null +++ b/src/express/testingEndpoints.js @@ -0,0 +1,9 @@ +function registerTestingEndpoints() { + this.router.post('/killkillkill', (req, res) => { + console.log('Gracefully killing server...'); + res.json({ message: 'Shutting down server.' }); + process.exit(0); + }); +} + +module.exports = registerTestingEndpoints; diff --git a/src/index.js b/src/index.js index e0df1828ff..e3aae8f927 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,7 @@ const registerUser = require('./auth/register'); const registerUpload = require('./uploads/register'); const registerCollections = require('./collections/register'); const registerGlobals = require('./globals/register'); +const testingEndpoints = require('./express/testingEndpoints'); const GraphQL = require('./graphql'); const sanitizeConfig = require('./utilities/sanitizeConfig'); @@ -22,6 +23,7 @@ class Payload { this.registerUpload = registerUpload.bind(this); this.registerCollections = registerCollections.bind(this); this.registerGlobals = registerGlobals.bind(this); + this.testingEndpoints = testingEndpoints.bind(this); // Setup & initialization connectMongoose(this.config.mongoURL); @@ -60,6 +62,10 @@ class Payload { ); } + if (process.env.NODE_ENV === 'test') { + this.testingEndpoints(); + } + this.router.get(this.config.routes.graphQLPlayground, graphQLPlayground({ endpoint: `${this.config.routes.api}${this.config.routes.graphQL}`, settings: { diff --git a/src/tests/integration/api.spec.js b/src/tests/integration/api.spec.js index 03c8aad93a..729a069426 100644 --- a/src/tests/integration/api.spec.js +++ b/src/tests/integration/api.spec.js @@ -6,13 +6,18 @@ const axios = require('axios'); const faker = require('faker'); describe('API', () => { + const url = 'http://localhost:3000'; let token = null; let email = 'test@test.com'; const password = 'test123'; + afterAll(async () => { + await axios.post(`${url}/api/killkillkill`); + }); + describe('first register', () => { it('should allow first user to register', async () => { - const createResponse = await axios.post('http://localhost:3000/api/first-register', { + const createResponse = await axios.post(`${url}/api/first-register`, { email, password, }, { headers: { 'Content-Type': 'application/json' } }); @@ -25,7 +30,7 @@ describe('API', () => { describe('login', () => { it('should allow login', async () => { - const loginResponse = await axios.post('http://localhost:3000/api/login', { + const loginResponse = await axios.post(`${url}/api/login`, { email, password, }, { headers: { 'Content-Type': 'application/json' } }); @@ -38,7 +43,7 @@ describe('API', () => { describe('register', () => { it('should allow create user', async () => { email = `${faker.name.firstName()}@test.com`; - const createResponse = await axios.post('http://localhost:3000/api/users/register', { + const createResponse = await axios.post(`${url}/api/users/register`, { email: `${email}`, password, }, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } }); @@ -52,7 +57,7 @@ describe('API', () => { describe('Collections - Post', () => { it('should allow create post', async () => { - const createResponse = await axios.post('http://localhost:3000/api/posts', { + const createResponse = await axios.post(`${url}/api/posts`, { title: faker.name.firstName(), description: faker.lorem.lines(20), priority: 1, @@ -64,7 +69,7 @@ describe('API', () => { it('should allow create post - localized', async () => { const spanishDesc = faker.lorem.lines(20); const englishDesc = faker.lorem.lines(20); - const englishCreateResponse = await axios.post('http://localhost:3000/api/posts', { + const englishCreateResponse = await axios.post(`${url}/api/posts`, { title: `English-${faker.name.firstName()}`, description: englishDesc, priority: 1, @@ -72,7 +77,7 @@ describe('API', () => { expect(englishCreateResponse.status).toBe(201); const { id } = englishCreateResponse.data.doc; - const spanishCreateResponse = await axios.put(`http://localhost:3000/api/posts/${id}?locale=es`, { + const spanishCreateResponse = await axios.put(`${url}/api/posts/${id}?locale=es`, { title: `Spanish-${faker.name.firstName()}`, description: spanishDesc, priority: 1, @@ -80,19 +85,19 @@ describe('API', () => { expect(spanishCreateResponse.status).toBe(200); expect(spanishCreateResponse.data.doc.description).toBe(spanishDesc); - const englishQueryResponse = await axios.get(`http://localhost:3000/api/posts/${id}`); + const englishQueryResponse = await axios.get(`${url}/api/posts/${id}`); expect(englishQueryResponse.status).toBe(200); expect(englishQueryResponse.data.description).toBe(englishDesc); - const englishQueryResponseWithLocale = await axios.get(`http://localhost:3000/api/posts/${id}?locale=en`); + const englishQueryResponseWithLocale = await axios.get(`${url}/api/posts/${id}?locale=en`); expect(englishQueryResponseWithLocale.status).toBe(200); expect(englishQueryResponseWithLocale.data.description).toBe(englishDesc); - const spanishQueryResponse = await axios.get(`http://localhost:3000/api/posts/${id}/?locale=es`); + const spanishQueryResponse = await axios.get(`${url}/api/posts/${id}/?locale=es`); expect(spanishQueryResponse.status).toBe(200); expect(spanishQueryResponse.data.description).toBe(spanishDesc); - const allQueryResponse = await axios.get(`http://localhost:3000/api/posts/${id}/?locale=all`); + const allQueryResponse = await axios.get(`${url}/api/posts/${id}/?locale=all`); expect(allQueryResponse.status).toBe(200); expect(allQueryResponse.data.description.es).toBe(spanishDesc); expect(allQueryResponse.data.description.en).toBe(englishDesc);