diff --git a/.gitpod.yml b/.gitpod.yml index 8ebef2db2..b8b9fadec 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -2,8 +2,16 @@ image: gitpod/workspace-mongodb ports: - port: 27017 onOpen: ignore + - port: 50000-50200 + onOpen: ignore + # Set to public for tests to have access across ports + # Technically, could be configured with `credentials: true` in fetch calls too + visibility: public tasks: + - before: | + export DEMO_APPS_DOMAIN=$(gp url) - init: | + cp sample.env .env npm ci mkdir /workspace/log mongod --fork --dbpath /data/db --logpath /workspace/log/mongod.log diff --git a/jest.config.js b/jest.config.js index 0ab81955e..5523f1b2c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,6 @@ const config = { - roots: ['./test/'] + roots: ['./test/'], + setupFiles: ['dotenv/config'] }; module.exports = config; diff --git a/test/jest-utils.js b/test/jest-utils.js new file mode 100644 index 000000000..75bc8c4b3 --- /dev/null +++ b/test/jest-utils.js @@ -0,0 +1,18 @@ +const baseUrl = port => { + if (process.env.DEMO_APPS_DOMAIN === 'localhost') { + const url = new URL('http://localhost'); + url.port = port; + return url; + } + + if (process.env.GITPOD_HOST) { + const url = new URL( + `https://${port}-${process.env.GITPOD_WORKSPACE_ID}.${process.env.GITPOD_WORKSPACE_CLUSTER_HOST}` + ); + return url; + } +}; + +module.exports = { + baseUrl +}; diff --git a/test/ports.test.js b/test/ports.test.js index 19ec5d1c3..146316a95 100644 --- a/test/ports.test.js +++ b/test/ports.test.js @@ -1,6 +1,5 @@ // TODO: Remove eslint-disable-next-line no-undef the minute expect can be labelled as defined const { readdirSync } = require('fs'); -const axios = require('axios'); const { join } = require('path'); const portMap = require('../port-map.json'); @@ -10,6 +9,8 @@ const names = readdirSync(join(__dirname, '../apps')).filter( name => !/(^|\/)\.[^/.]/g.test(name) && name !== 'nightlife-coordination-app' ); +const { baseUrl } = require('./jest-utils.js'); + describe('portMap', () => { it('should have unique ports', () => { const ports = Object.values(portMap); @@ -35,10 +36,11 @@ describe('Project statuses', () => { for (const name of projectNames) { const portNum = portMap[name]; + const BASE_URL = baseUrl(portNum); it(`${name} should be running on port ${portNum}`, async () => { try { - const response = await axios.get(`http://localhost:${portNum}`); + const response = await fetch(BASE_URL); // eslint-disable-next-line no-undef expect(response.status).toBe(200); @@ -49,9 +51,7 @@ describe('Project statuses', () => { }); it(`Pinging ${name} should return a status code of 200 `, async () => { try { - const response = await axios.get( - `http://localhost:${portNum}/status/ping` - ); + const response = await fetch(new URL('/status/ping', BASE_URL)); // eslint-disable-next-line no-undef expect(response.status).toBe(200); diff --git a/test/twitch-proxy.test.js b/test/twitch-proxy.test.js new file mode 100644 index 000000000..d2be46471 --- /dev/null +++ b/test/twitch-proxy.test.js @@ -0,0 +1,39 @@ +// TODO: Remove once types for Jest are recognized +/* eslint-disable no-undef */ +const portMap = require('../port-map.json'); +const twitchProxyPort = portMap['twitch-proxy']; +const { baseUrl } = require('./jest-utils.js'); + +const BASE_URL = baseUrl(twitchProxyPort); + +describe('kraken api', () => { + it('should return freecodecamp user data', async () => { + const response = await fetch( + new URL('/twitch-api/users/freecodecamp', BASE_URL) + ); + const user = await response.json(); + expect(response.status).toBe(200); + expect(user.name).toBe('freecodecamp'); + expect(user.display_name).toBe('FreeCodeCamp'); + }); + + it('should return freecodecamp channel data', async () => { + const response = await fetch( + new URL('/twitch-api/channels/freecodecamp', BASE_URL) + ); + const channel = await response.json(); + expect(response.status).toBe(200); + expect(channel.name).toBe('freecodecamp'); + expect(channel.display_name).toBe('FreeCodeCamp'); + expect(channel.url).toBe('https://www.twitch.tv/freecodecamp'); + }); + + it("should return esl_sc2's stream data", async () => { + const response = await fetch( + new URL('/twitch-api/streams/ESL_SC2', BASE_URL) + ); + const stream = (await response.json()).stream; + expect(response.status).toBe(200); + expect(stream._id).toBe(23366709968); + }); +});