Skip to content

Commit

Permalink
freeze defaultLoaders so it cannot be mutated causing unexpected results
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisblossom committed Oct 22, 2019
1 parent 26b65bb commit 7a0f0e6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ function cosmiconfigSync(moduleName: string, options: OptionsSync = {}) {
} as const;
}

const defaultLoaders = {
// do not allow mutation of default loaders. Make sure it is set inside options
const defaultLoaders = Object.freeze({
'.js': loaders.loadJs,
'.json': loaders.loadJson,
'.yaml': loaders.loadYaml,
'.yml': loaders.loadYaml,
noExt: loaders.loadYaml,
} as const;
} as const);

function normalizeOptions(
moduleName: string,
Expand Down
9 changes: 8 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TempDir } from './util';
import {
cosmiconfig as cosmiconfigModule,
cosmiconfigSync as cosmiconfigSyncModule,
defaultLoaders,
LoaderSync,
} from '../src';

Expand Down Expand Up @@ -195,7 +196,7 @@ describe('cosmiconfig', () => {

const expectedError =
'loader for extension ".things" is not a function (type provided: "number"), so searchPlaces item ".foorc.things" is invalid';
test('async', async () => {
test('async', () => {
expect(() =>
// @ts-ignore
cosmiconfig('foo', explorerOptions),
Expand All @@ -209,4 +210,10 @@ describe('cosmiconfig', () => {
).toThrow(expectedError);
});
});

describe('cannot mutate default loaders', () => {
const expectedError = "Cannot delete property '.js' of #<Object>";
// @ts-ignore
expect(() => delete defaultLoaders['.js']).toThrow(expectedError);
});
});

0 comments on commit 7a0f0e6

Please sign in to comment.