mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-07 05:12:31 +02:00
test: add test for load config
This commit is contained in:
parent
687bf09c96
commit
bf94bf96f8
6 changed files with 71 additions and 8 deletions
|
@ -10,5 +10,17 @@ module.exports = function loadConfig(siteDir, deleteCache = true) {
|
||||||
if (fs.existsSync(configPath)) {
|
if (fs.existsSync(configPath)) {
|
||||||
config = require(configPath); // eslint-disable-line
|
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;
|
return config;
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
"build": "node bin/munseo build website",
|
"build": "node bin/munseo build website",
|
||||||
"prettier": "prettier --config .prettierrc --write \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"",
|
"prettier": "prettier --config .prettierrc --write \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"",
|
||||||
"lint": "eslint --cache \"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": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -75,12 +75,5 @@
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=8"
|
"node": ">=8"
|
||||||
},
|
|
||||||
"jest": {
|
|
||||||
"testPathIgnorePatterns": [
|
|
||||||
"/node_modules/",
|
|
||||||
"__fixtures__"
|
|
||||||
],
|
|
||||||
"testEnvironment": "node"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
12
test/jest.config.js
Normal file
12
test/jest.config.js
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
rootDir: path.resolve(__dirname, '..'),
|
||||||
|
verbose: true,
|
||||||
|
testURL: 'http://localhost/',
|
||||||
|
testEnvironment: 'node',
|
||||||
|
moduleNameMapper: {
|
||||||
|
'^@lib/(.*)$': '<rootDir>/lib/$1'
|
||||||
|
},
|
||||||
|
testPathIgnorePatterns: ['/node_modules/', '__fixtures__']
|
||||||
|
};
|
4
test/load/__fixtures__/bad-site/siteConfig.js
Normal file
4
test/load/__fixtures__/bad-site/siteConfig.js
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
module.exports = {
|
||||||
|
title: 'Munseo',
|
||||||
|
baseUrl: '/'
|
||||||
|
};
|
7
test/load/__fixtures__/simple-site/siteConfig.js
Normal file
7
test/load/__fixtures__/simple-site/siteConfig.js
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
module.exports = {
|
||||||
|
title: 'Hello',
|
||||||
|
tagline: 'Hello World',
|
||||||
|
organizationName: 'endiliey',
|
||||||
|
projectName: 'hello',
|
||||||
|
baseUrl: '/'
|
||||||
|
};
|
35
test/load/config.test.js
Normal file
35
test/load/config.test.js
Normal file
|
@ -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"`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue