diff --git a/index.js b/index.js index 333c836..917c45f 100644 --- a/index.js +++ b/index.js @@ -15,8 +15,10 @@ const skipModels = new Set(['gray']); const styles = Object.create(null); -function applyOptions(obj, options) { - options = options || {}; +function applyOptions(obj, options = {}) { + if (options.level > 3 || options.level < 0) { + throw new Error('The `level` option should be an integer from 0 to 3'); + } // Detect level if not set manually const scLevel = stdoutColor ? stdoutColor.level : 0; diff --git a/test/constructor.js b/test/constructor.js index 510f1a0..bda0137 100644 --- a/test/constructor.js +++ b/test/constructor.js @@ -20,3 +20,15 @@ test('create an isolated context where colors can be disabled (by enabled flag)' instance.enabled = true; t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m'); }); + +test('the `level` option should be a number from 0 to 3', t => { + /* eslint-disable no-new */ + t.throws(() => { + new chalk.constructor({level: 10}); + }, /should be an integer from 0 to 3/); + + t.throws(() => { + new chalk.constructor({level: -1}); + }, /should be an integer from 0 to 3/); + /* eslint-enable no-new */ +});