Skip to content

Commit

Permalink
feat: scope inheritance support (#23)
Browse files Browse the repository at this point in the history
also:
- add ability to access consola using global object
- add jest tests
  • Loading branch information
pimlie authored and pi0 committed Oct 4, 2018
1 parent 0adc6ba commit 0070c54
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"module": "./src/index.js",
"scripts": {
"build": "bili",
"test": "true",
"lint": "eslint --ext .js,.mjs src test examples",
"test": "jest test/",
"prepublish": "yarn build",
"release": "standard-version && yarn build && git push --follow-tags && npm publish"
},
Expand Down Expand Up @@ -46,6 +47,7 @@
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-vue": "^4.7.1",
"esm": "^3.0.84",
"jest": "^23.6.0",
"standard-version": "^4.4.0",
"winston": "^3.1.0"
}
Expand Down
4 changes: 3 additions & 1 deletion src/consola.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default class Consola {

const logObj = Object.assign({
date: new Date()
}, defaults)
}, defaults, {
scope: this.scope
})

const argsStr = Array.from(args).map(String).join(' ')

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ if (!consola) {
}

Object.assign(consola, { Consola }, Reporters)

global.consola = consola
}

export default consola
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 0070c54

Please sign in to comment.