Skip to content

Commit

Permalink
feat: add global.consola
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 8, 2018
1 parent 6180985 commit 4da784d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down
49 changes: 49 additions & 0 deletions test/custom-consola.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit 4da784d

Please sign in to comment.