From 88f4c8bf6b5df545b57556e9b7173913c5ada1cf Mon Sep 17 00:00:00 2001 From: Richard Petersen <richard.petersen@open-xchange.com> Date: Tue, 22 Nov 2022 10:47:10 +0100 Subject: [PATCH] Improve tests --- integration/caching_test.js | 2 +- integration/config_test.js | 4 +-- integration/update-version_test.js | 4 +-- spec/app_root_test.js | 2 +- spec/file-depencies_test.js | 4 +-- spec/file_caching_test.js | 4 +-- spec/headers_test.js | 8 ++--- spec/meta_test.js | 7 +++-- spec/redirect_test.js | 2 +- spec/redis_test.js | 5 ++-- spec/server_test.js | 47 +++++++++--------------------- spec/util.js | 3 +- 12 files changed, 38 insertions(+), 54 deletions(-) diff --git a/integration/caching_test.js b/integration/caching_test.js index f54041f..232f3f9 100644 --- a/integration/caching_test.js +++ b/integration/caching_test.js @@ -9,7 +9,7 @@ describe('File caching service', function () { let app beforeEach(async function () { - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) mockFetch({ 'http://ui-server': { '/manifest.json': generateSimpleViteManifest({ diff --git a/integration/config_test.js b/integration/config_test.js index cde423c..e6b8826 100644 --- a/integration/config_test.js +++ b/integration/config_test.js @@ -11,7 +11,7 @@ describe('Configuration', function () { beforeEach(async function () { // need to set the redis-prefix. Otherwise, the bull workers will interfere process.env.REDIS_PREFIX = Math.random().toString() - mockConfig(config = { urls: ['http://ui-server/'] }) + mockConfig(config = { baseUrls: ['http://ui-server/'] }) mockFetch({ 'http://ui-server': { '/manifest.json': generateSimpleViteManifest({ @@ -42,7 +42,7 @@ describe('Configuration', function () { const response = await request(app.server).get('/meta') expect(response.body).to.have.length(2) - config.urls = [] + config.baseUrls = [] const { pubClient } = await import('../src/redis.js') pubClient.publish(getRedisKey({ name: 'updateLatestVersion' }), '1234') await new Promise(resolve => setTimeout(resolve, 200)) diff --git a/integration/update-version_test.js b/integration/update-version_test.js index af2244c..15d3fc7 100644 --- a/integration/update-version_test.js +++ b/integration/update-version_test.js @@ -10,7 +10,7 @@ describe('Updates the version', function () { beforeEach(async function () { // need to set the redis-prefix. Otherwise, the bull workers will interfere process.env.REDIS_PREFIX = Math.random().toString() - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) mockFetch({ 'http://ui-server': { '/manifest.json': generateSimpleViteManifest({ @@ -102,7 +102,7 @@ describe('Updates the version', function () { td.reset() // need to set the redis-prefix. Otherwise, the bull workers will interfere process.env.REDIS_PREFIX = Math.random().toString() - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) mockFetch({ 'http://ui-server': { '/manifest.json': generateSimpleViteManifest({ diff --git a/spec/app_root_test.js b/spec/app_root_test.js index f80fa82..029da1e 100644 --- a/spec/app_root_test.js +++ b/spec/app_root_test.js @@ -9,7 +9,7 @@ describe('With different app root', function () { beforeEach(async function () { let count = 0 - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) mockRedis() mockFetch({ 'http://ui-server': { diff --git a/spec/file-depencies_test.js b/spec/file-depencies_test.js index 395197d..45e699e 100644 --- a/spec/file-depencies_test.js +++ b/spec/file-depencies_test.js @@ -9,7 +9,7 @@ describe('JS files with dependencies contain events', function () { let mockFetchConfig beforeEach(async function () { - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) mockRedis() mockFetch(mockFetchConfig = { 'http://ui-server': { @@ -45,7 +45,7 @@ describe('JS files with dependencies contain events', function () { expect(response.text).to.equal('console.log("this is index.html.js")\n/*injected by ui-middleware*/document.dispatchEvent(new CustomEvent("load-css",{detail:{css:["main.css"]}}))') }) - it('javascript files from different versions have corrent dependencies', async function () { + it('javascript files from different versions have correct dependencies', async function () { const r1 = await request(app.server).get('/index.html.js') expect(r1.headers.dependencies).to.equal('main.css') diff --git a/spec/file_caching_test.js b/spec/file_caching_test.js index d3e8023..3803ddb 100644 --- a/spec/file_caching_test.js +++ b/spec/file_caching_test.js @@ -17,7 +17,7 @@ describe('File caching service', function () { beforeEach(async function () { let count = 0 - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) redis = mockRedis() mockFetch({ 'http://ui-server': { @@ -202,7 +202,7 @@ describe('File caching service', function () { it('requests known file only from one server', async function () { let spy1, spy2 - mockConfig({ urls: ['http://ui-server1/', 'http://ui-server2/'] }) + mockConfig({ baseUrls: ['http://ui-server1/', 'http://ui-server2/'] }) // we have example.js in both files. the first one will be overwritten and therefore not be called mockFetch({ 'http://ui-server1': { diff --git a/spec/headers_test.js b/spec/headers_test.js index c97721b..faca45b 100644 --- a/spec/headers_test.js +++ b/spec/headers_test.js @@ -8,7 +8,7 @@ describe('Responses contain custom headers', function () { let app before(async function () { - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) mockFetch({ 'http://ui-server': { '/manifest.json': generateSimpleViteManifest({ @@ -64,7 +64,7 @@ describe('Responses contain custom headers', function () { td.reset() await new RedisMock().flushdb() - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) mockRedis() mockFetch({ 'http://ui-server': { @@ -90,7 +90,7 @@ describe('Responses contain custom headers', function () { td.reset() await new RedisMock().flushdb() - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) mockRedis() mockFetch({ 'http://ui-server': { @@ -117,7 +117,7 @@ describe('Responses contain custom headers', function () { td.reset() await new RedisMock().flushdb() - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) mockRedis() mockFetch({ 'http://ui-server': { diff --git a/spec/meta_test.js b/spec/meta_test.js index c5a0dc2..b9ca8a1 100644 --- a/spec/meta_test.js +++ b/spec/meta_test.js @@ -10,7 +10,7 @@ describe('Responses contain custom headers', function () { let app beforeEach(async function () { - mockConfig(config = { urls: ['http://ui-server/'] }) + mockConfig(config = { baseUrls: ['http://ui-server/'] }) mockRedis() mockFetch(fetchConfig = { 'http://ui-server': { @@ -61,7 +61,7 @@ describe('Responses contain custom headers', function () { const response = await request(app.server).get('/meta') expect(response.body).to.have.length(2) - config.urls = [] + config.baseUrls = [] await import('../src/version.js').then(async ({ updateVersionProcessor }) => { // need to process two times to actually trigger the update await updateVersionProcessor() @@ -91,7 +91,8 @@ describe('Responses contain custom headers', function () { describe('without redis disabled', function () { beforeEach(async function () { td.reset() - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) + mockRedis() mockFetch(fetchConfig = { 'http://ui-server': { '/manifest.json': generateSimpleViteManifest({ diff --git a/spec/redirect_test.js b/spec/redirect_test.js index 8e8ca1c..0baf750 100644 --- a/spec/redirect_test.js +++ b/spec/redirect_test.js @@ -8,7 +8,7 @@ describe('Redirects', function () { let app before(async function () { - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) mockRedis() mockFetch({ 'http://ui-server': { diff --git a/spec/redis_test.js b/spec/redis_test.js index d8c30c2..cc51b84 100644 --- a/spec/redis_test.js +++ b/spec/redis_test.js @@ -1,5 +1,5 @@ import request from 'supertest' -import { generateSimpleViteManifest, mockApp, mockConfig, mockFetch } from './util.js' +import { generateSimpleViteManifest, mockApp, mockConfig, mockFetch, mockRedis } from './util.js' import { expect } from 'chai' import * as td from 'testdouble' import sinon from 'sinon' @@ -11,7 +11,8 @@ describe('Redis', function () { let spy beforeEach(async function () { - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) + mockRedis({}, false) mockFetch({ 'http://ui-server': { '/manifest.json': generateSimpleViteManifest({}), diff --git a/spec/server_test.js b/spec/server_test.js index b67e78b..6c725c9 100644 --- a/spec/server_test.js +++ b/spec/server_test.js @@ -9,7 +9,7 @@ describe('UI Middleware', function () { let fetchConfig beforeEach(async function () { - mockConfig({ urls: ['http://ui-server/'] }) + mockConfig({ baseUrls: ['http://ui-server/'] }) mockRedis() mockFetch(fetchConfig = { 'http://ui-server': { @@ -23,7 +23,6 @@ describe('UI Middleware', function () { afterEach(async function () { td.reset() await new RedisMock().flushdb() - process.env.CACHE_TTL = '30000' }) it('fetches manifest data', async function () { @@ -33,51 +32,33 @@ describe('UI Middleware', function () { expect(response.body).to.deep.equal([{ namespace: 'test', path: 'example' }]) }) - describe('when configuration changes', function () { - let prevConfig - beforeEach(async function () { - prevConfig = fetchConfig['http://ui-server'] - }) - - afterEach(function () { - fetchConfig['http://ui-server'] = prevConfig - }) - - it('caches manifest data', async function () { - const response = await request(app.server).get('/manifests').parse(brotliParser) - expect(response.statusCode).to.equal(200) - expect(response.body).to.deep.equal([{ namespace: 'test', path: 'example' }]) + it('caches manifest data when configuration changes', async function () { + const response = await request(app.server).get('/manifests').parse(brotliParser) + expect(response.statusCode).to.equal(200) + expect(response.body).to.deep.equal([{ namespace: 'test', path: 'example' }]) - fetchConfig['http://ui-server'] = { - '/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }), - '/example.js': '' - } + fetchConfig['http://ui-server'] = { + '/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }), + '/example.js': '' + } - await new Promise(resolve => setTimeout(resolve, 150)) + await new Promise(resolve => setTimeout(resolve, 150)) - const response2 = await request(app.server).get('/manifests').parse(brotliParser) - expect(response2.statusCode).to.equal(200) - expect(response2.body).to.deep.equal([{ namespace: 'test', path: 'example' }]) - }) + const response2 = await request(app.server).get('/manifests').parse(brotliParser) + expect(response2.statusCode).to.equal(200) + expect(response2.body).to.deep.equal([{ namespace: 'test', path: 'example' }]) }) describe('multiple configurations', function () { - let prevApp beforeEach(async function () { - mockConfig({ urls: ['http://ui-server/', 'http://ui-server2/'] }) + mockConfig({ baseUrls: ['http://ui-server/', 'http://ui-server2/'] }) fetchConfig['http://ui-server2'] = { '/manifest.json': generateSimpleViteManifest({ 'example2.js': 'thing' }), '/example2.js': '' } - prevApp = app app = await mockApp() }) - afterEach(function () { - delete fetchConfig['http://ui-server2'] - app = prevApp - }) - it('can load multiple configurations', async function () { await request(app.server) .get('/manifests') diff --git a/spec/util.js b/spec/util.js index dbfe0e8..23c361c 100644 --- a/spec/util.js +++ b/spec/util.js @@ -2,6 +2,7 @@ import * as td from 'testdouble' import { register } from 'prom-client' import RedisMock from 'ioredis-mock' import zlib from 'node:zlib' +import yaml from 'js-yaml' export function generateSimpleViteManifest (mapping) { const viteManifest = {} @@ -18,7 +19,7 @@ export function generateSimpleViteManifest (mapping) { export function mockConfig (obj = {}) { td.replaceEsm('fs/promises', {}, { readFile () { - return `baseUrls:\n${obj.urls.map(u => ` - ${u}`).join('\n')}` + return yaml.dump(obj) } }) } -- GitLab