From 4da784d2fdf6ed04ef98d29a0e7c6d84837121c6 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 8 Oct 2018 20:31:12 +0330 Subject: [PATCH] feat: add global.consola --- .eslintignore | 2 ++ index.js | 7 ++++++ package.json | 7 +++--- test/custom-consola.test.js | 49 +++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 .eslintignore create mode 100644 index.js create mode 100644 test/custom-consola.test.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..de4d1f00 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +dist +node_modules diff --git a/index.js b/index.js new file mode 100644 index 00000000..c648d345 --- /dev/null +++ b/index.js @@ -0,0 +1,7 @@ +if (global.consola) { + module.exports = global.consola +} else { + const consola = require('./dist/consola.cjs.js') + module.exports = consola + global.consola = consola +} diff --git a/package.json b/package.json index f6ca7dbf..b28c506f 100644 --- a/package.json +++ b/package.json @@ -4,15 +4,16 @@ "description": "Elegant Console Logger", "license": "MIT", "repository": "nuxt/consola", - "main": "dist/consola.cjs.js", + "main": "index.js", "scripts": { "build": "bili", - "lint": "eslint src test examples", - "test": "jest test", + "lint": "eslint .", + "test": "yarn lint && yarn build && jest test", "prepublish": "yarn build", "release": "standard-version && yarn build && git push --follow-tags && npm publish" }, "files": [ + "index.js", "dist", "src" ], diff --git a/test/custom-consola.test.js b/test/custom-consola.test.js new file mode 100644 index 00000000..8f626fa6 --- /dev/null +++ b/test/custom-consola.test.js @@ -0,0 +1,49 @@ + +describe('custom consola', () => { + afterEach(() => { + delete global.consola + jest.resetModules() + }) + + test('require twice has same consola', () => { + const consola1 = require('consola') + jest.resetModules() + const consola2 = require('consola') + + expect(consola1 === consola2).toBe(true) + }) + + test('custom consola fails without delete cache', async () => { + require('consola') + const consola1 = 'my-consola' + global.consola = consola1 + + const consola2 = require('consola') + + expect(consola1 === consola2).toBe(false) + expect(global.consola === consola2).toBe(false) + }) + + test('require consola used global.consola by default', () => { + const consola1 = 'my-consola' + global.consola = consola1 + + const consola2 = require('consola') + expect(consola1 === consola2).toBe(true) + expect(global.consola === consola2).toBe(true) + }) + + test('custom consola works when imported but deleted', () => { + const consola0 = require('consola') + expect(consola0).not.toBe(undefined) + + const consola1 = 'my-consola2' + global.consola = consola1 + + jest.resetModules() // jest equivalent to delete require.cache + + const consola2 = require('consola') + expect(consola1 === consola2).toBe(true) + expect(global.consola === consola2).toBe(true) + }) +})