Jeff Williams 4badaef5fd better config loading (#1550)
+ Use `cosmiconfig` instead of rolling our own code (which gives us YAML support)
+ Look for the config in these locations, and in this order:
    + A `jsdoc` property in `package.json`
    + `.jsdocrc` (can be JSON or YAML; comments not allowed for JSON)
    + `.jsdocrc.json` (comments allowed)
    + `.jsdocrc.yaml`
    + `.jsdocrc.yml`
    + `.jsdocrc.js`
    + `jsdoc.config.js`
2019-01-25 19:33:18 -08:00

106 lines
2.9 KiB
JavaScript

describe('@jsdoc/config', () => {
const mockFs = require('mock-fs');
const config = require('../../index');
const defaults = require('../../defaults');
afterEach(() => mockFs.restore());
it('exists', () => {
expect(config).toBeObject();
});
it('has a loadSync method', () => {
expect(config.loadSync).toBeFunction();
});
describe('loadSync', () => {
it('returns an object with `config` and `filepath` properties', () => {
mockFs({
'conf.json': '{}'
});
const conf = config.loadSync('conf.json');
expect(conf.config).toBeObject();
expect(conf.filepath).toEndWith('conf.json');
});
it('loads settings from the specified filepath if there is one', () => {
mockFs({
'conf.json': '{"foo":"bar"}'
});
const conf = config.loadSync('conf.json');
expect(conf.config.foo).toBe('bar');
});
it('finds the config file when no filepath is specified', () => {
mockFs({
'package.json': '{"jsdoc":{"foo":"bar"}}'
});
const conf = config.loadSync();
expect(conf.config.foo).toBe('bar');
});
it('parses JSON config files that have an extension and contain comments', () => {
mockFs({
'.jsdocrc.json': '// comment\n{"foo":"bar"}'
});
const conf = config.loadSync();
expect(conf.config.foo).toBe('bar');
});
it('parses JSON files that start with a BOM', () => {
mockFs({
'.jsdocrc.json': '\uFEFF{"foo":"bar"}'
});
const conf = config.loadSync();
expect(conf.config.foo).toBe('bar');
});
it('parses YAML files that start with a BOM', () => {
mockFs({
'.jsdocrc.yaml': '\uFEFF{"foo":"bar"}'
});
const conf = config.loadSync();
expect(conf.config.foo).toBe('bar');
});
it('provides the default config if the user config is an empty object', () => {
mockFs({
'.jsdocrc.json': '{}'
});
const conf = config.loadSync();
expect(conf.config).toEqual(defaults);
});
it('provides the default config if there is no user config', () => {
const conf = config.loadSync();
expect(conf.config).toEqual(defaults);
});
it('merges nested defaults with nested user settings as expected', () => {
mockFs({
'.jsdocrc.json': '{"tags":{"foo":"bar"}}'
});
const conf = config.loadSync();
expect(conf.config.tags.allowUnknownTags).toBe(defaults.tags.allowUnknownTags);
expect(conf.config.tags.foo).toBe('bar');
});
});
});