From 94acb9225878599bd1cbcd6319898bef210fd93f Mon Sep 17 00:00:00 2001
From: Paul Melnikow <github@paulmelnikow.com>
Date: Fri, 18 Jan 2019 23:51:07 -0500
Subject: [PATCH] Rewrite test of analytics endpoint (#2810)

This test is being weirdly flaky in #2809. The problem seems to be in the test helper code, so I rewrote this using Joi.

I imagine the change has to do with a change to the test ordering. It's a bit puzzling.

However, the new test seems fine (and the endpoint is rarely used; not critical to begin with).
---
 core/server/server.spec.js | 37 ++++++++++++++++++++++---------------
 1 file changed, 22 insertions(+), 15 deletions(-)

diff --git a/core/server/server.spec.js b/core/server/server.spec.js
index 95da4cee5a..1b1e8d9bac 100644
--- a/core/server/server.spec.js
+++ b/core/server/server.spec.js
@@ -9,6 +9,7 @@ const isSvg = require('is-svg')
 const path = require('path')
 const sinon = require('sinon')
 const portfinder = require('portfinder')
+const Joi = require('joi')
 const svg2img = require('../../gh-badges/lib/svg-to-img')
 const { createTestServer } = require('./in-process-server-test-helpers')
 
@@ -128,24 +129,30 @@ describe('The server', function() {
 
   describe('analytics endpoint', function() {
     it('should return analytics in the expected format', async function() {
+      const countSchema = Joi.array()
+        .items(
+          Joi.number()
+            .integer()
+            .min(0)
+            .required()
+        )
+        .length(36)
+        .required()
+      const analyticsSchema = Joi.object({
+        vendorMonthly: countSchema,
+        rawMonthly: countSchema,
+        vendorFlatMonthly: countSchema,
+        rawFlatMonthly: countSchema,
+        vendorFlatSquareMonthly: countSchema,
+        rawFlatSquareMonthly: countSchema,
+      }).required()
+
       const res = await fetch(`${baseUrl}$analytics/v1`)
       expect(res.ok).to.be.true
+
       const json = await res.json()
-      const expectedKeys = [
-        'vendorMonthly',
-        'rawMonthly',
-        'vendorFlatMonthly',
-        'rawFlatMonthly',
-        'vendorFlatSquareMonthly',
-        'rawFlatSquareMonthly',
-      ]
-      expect(json).to.have.all.keys(...expectedKeys)
-
-      Object.values(json).forEach(stats => {
-        expect(stats)
-          .to.be.an('array')
-          .with.length(36)
-      })
+
+      Joi.assert(json, analyticsSchema)
     })
   })
 })
-- 
GitLab