Repository: paularmstrong/build-tracker (ts)

Issue:
Silent error when server config is missing `artifacts`
Hi, thanks for the great tool!

I am in the process of setting this up for [Unsplash](https://unsplash.com/).

I've been following the docs but got stuck: the app was not showing any data, despite the fact I had uploaded builds and these were being returned by the API.

![image](https://user-images.githubusercontent.com/921609/74160416-48586c00-4c15-11ea-9565-6ab39975ec3f.png)

After some debugging I discovered the issue: there was an exception happening in the reducer, when processing the `BUILDS_SET` action. (For some reason the exception is not shown in the console?)

The exception was from this line:

https://github.com/paularmstrong/build-tracker/blob/f66274bba0003e58cd2922b759cc883222e21ac9/src/app/src/store/reducer.ts#L13

`artifactConfig` is `undefined` at runtime.

To fix this I had to add an empty `artifacts` object to my server config.

Does this requirement need to be documented, or should the app handle a missing `artifacts`?

Pull request:
[server] default artifactConfig parts individually (#104)
<!--
Thank you so much for contributing to open source and the Build Tracker project!
-->

# Problem

If an `artifacts` config is not provided, or the optional parts are missing, the application can silently fail.

# Solution

Default all keys and assign the provided config over the default.

Fixes #104 

# TODO

- [ ] 🤓 Add & update tests (always try to _increase_ test coverage)
- [ ] 🔬 Ensure CI is passing (`yarn lint:ci`, `yarn test`, `yarn tsc:ci`)
- [ ] 📖 Update relevant documentation


Patch:
diff --git a/src/server/src/server.ts b/src/server/src/server.ts
index 823d7ce..d00636d 100644
--- a/src/server/src/server.ts
+++ b/src/server/src/server.ts
@@ -39,7 +39,7 @@ export const props = (config: AppConfig, url: string): RequestHandler => (
 ): void => {
   res.locals.props = {
     url,
-    artifactConfig: config.artifacts || {},
+    artifactConfig: Object.assign({}, { budgets: {}, filters: [], groups: [] }, config.artifacts || {}),
     hideAttribution: !!config.hideAttribution,
     name: config.name || 'Build Tracker'
   };


Test patch:
diff --git a/src/server/src/__tests__/server.test.ts b/src/server/src/__tests__/server.test.ts
index 2e95d7c..eaa008c 100644
--- a/src/server/src/__tests__/server.test.ts
+++ b/src/server/src/__tests__/server.test.ts
@@ -45,7 +45,7 @@ describe('server', () => {
           });
       });
 
-      test('defaults artifactConfig to empty object', () => {
+      test('defaults artifactConfig if unset', () => {
         const app = express();
         const config = {};
         app.use(props(config, 'https://build-tracker.local'));
@@ -54,7 +54,22 @@ describe('server', () => {
           .get('/')
           .then(res => {
             expect(res.body.props).toMatchObject({
-              artifactConfig: {},
+              artifactConfig: { budgets: {}, filters: [], groups: [] },
+              url: 'https://build-tracker.local'
+            });
+          });
+      });
+
+      test('defaults individual keys of artifactConfig', () => {
+        const app = express();
+        const config = { artifacts: { filters: [/foo/] } };
+        app.use(props(config, 'https://build-tracker.local'));
+        app.get('/', localsToBody);
+        return request(app)
+          .get('/')
+          .then(res => {
+            expect(res.body.props).toMatchObject({
+              artifactConfig: { budgets: {}, filters: [/foo/], groups: [] },
               url: 'https://build-tracker.local'
             });
           });