diff --git a/lib/load/config.js b/lib/load/config.js index 557edef3e2..3303e4f438 100644 --- a/lib/load/config.js +++ b/lib/load/config.js @@ -10,5 +10,17 @@ module.exports = function loadConfig(siteDir, deleteCache = true) { if (fs.existsSync(configPath)) { config = require(configPath); // eslint-disable-line } + + const requiredFields = [ + 'title', + 'tagline', + 'organizationName', + 'projectName', + 'baseUrl' + ]; + const missingFields = requiredFields.filter(field => !config[field]); + if (missingFields && missingFields.length > 0) { + throw new Error(missingFields.join(', ') + ' are missing in siteConfig.js'); + } return config; }; diff --git a/package.json b/package.json index ab056b14b9..3a04e668f0 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "build": "node bin/munseo build website", "prettier": "prettier --config .prettierrc --write \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"", "lint": "eslint --cache \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"", - "test": "jest" + "test": "jest --config test/jest.config.js" }, "repository": { "type": "git", @@ -75,12 +75,5 @@ }, "engines": { "node": ">=8" - }, - "jest": { - "testPathIgnorePatterns": [ - "/node_modules/", - "__fixtures__" - ], - "testEnvironment": "node" } } diff --git a/test/jest.config.js b/test/jest.config.js new file mode 100644 index 0000000000..cb505b774e --- /dev/null +++ b/test/jest.config.js @@ -0,0 +1,12 @@ +const path = require('path'); + +module.exports = { + rootDir: path.resolve(__dirname, '..'), + verbose: true, + testURL: 'http://localhost/', + testEnvironment: 'node', + moduleNameMapper: { + '^@lib/(.*)$': '/lib/$1' + }, + testPathIgnorePatterns: ['/node_modules/', '__fixtures__'] +}; diff --git a/test/load/__fixtures__/bad-site/siteConfig.js b/test/load/__fixtures__/bad-site/siteConfig.js new file mode 100644 index 0000000000..58d75f1d57 --- /dev/null +++ b/test/load/__fixtures__/bad-site/siteConfig.js @@ -0,0 +1,4 @@ +module.exports = { + title: 'Munseo', + baseUrl: '/' +}; diff --git a/test/load/__fixtures__/simple-site/siteConfig.js b/test/load/__fixtures__/simple-site/siteConfig.js new file mode 100644 index 0000000000..0e52974bd7 --- /dev/null +++ b/test/load/__fixtures__/simple-site/siteConfig.js @@ -0,0 +1,7 @@ +module.exports = { + title: 'Hello', + tagline: 'Hello World', + organizationName: 'endiliey', + projectName: 'hello', + baseUrl: '/' +}; diff --git a/test/load/config.test.js b/test/load/config.test.js new file mode 100644 index 0000000000..381ebbe0aa --- /dev/null +++ b/test/load/config.test.js @@ -0,0 +1,35 @@ +import path from 'path'; +import loadConfig from '@lib/load/config.js'; + +describe('loadConfig', () => { + test('website with valid siteConfig', () => { + const siteDir = path.join(__dirname, '__fixtures__', 'simple-site'); + const config = loadConfig(siteDir); + expect(config).toEqual({ + baseUrl: '/', + organizationName: 'endiliey', + projectName: 'hello', + tagline: 'Hello World', + title: 'Hello' + }); + expect(config).not.toEqual({}); + }); + + test('website with incomplete siteConfig', () => { + const siteDir = path.join(__dirname, '__fixtures__', 'bad-site'); + expect(() => { + loadConfig(siteDir); + }).toThrowErrorMatchingInlineSnapshot( + `"tagline, organizationName, projectName are missing in siteConfig.js"` + ); + }); + + test('website with no siteConfig', () => { + const siteDir = path.join(__dirname, '__fixtures__', 'nonExisting'); + expect(() => { + loadConfig(siteDir); + }).toThrowErrorMatchingInlineSnapshot( + `"title, tagline, organizationName, projectName, baseUrl are missing in siteConfig.js"` + ); + }); +});