Skip to content

Commit

Permalink
feat: add assignGlobalConsola helper
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie committed Oct 11, 2018
1 parent 418d84a commit 1af28f7
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 9 deletions.
13 changes: 13 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"env": {
"test": {
"presets": [
["@babel/preset-env", {
"targets": {
"node": "current"
}
}]
]
}
}
}
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
testEnvironment: 'node',
coverageDirectory: './coverage/',
collectCoverageFrom: [
'src/**'
],
transformIgnorePatterns: ['<rootDir>/node_modules/'],
moduleFileExtensions: ['js', 'mjs', 'json'],
expand: true,
forceExit: false
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
"std-env": "^2.0.2"
},
"devDependencies": {
"@babel/core": "^7.1.2",
"@babel/preset-env": "^7.1.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"bili": "^3.1.2",
"eslint": "^5.6.1",
"eslint-config-standard": "^12.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { default as Consola } from './consola'
export { default as Types } from './types'
export { isLogObj } from './utils'
export { isLogObj, assignGlobalConsola } from './utils'

export * from './reporters'
30 changes: 30 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,33 @@ export function align (alignment, str, len, space = ' ') {
default: return str
}
}

export function assignGlobalReference (newInstance, referenceKey) {
if (!newInstance.constructor || (global[referenceKey] && !global[referenceKey].constructor)) {
throw new Error('Assigning to global reference is only supported for class instances')
} else if (newInstance.constructor && !global[referenceKey]) {
global[referenceKey] = newInstance
} else if (!(
newInstance instanceof global[referenceKey].constructor ||
global[referenceKey] instanceof newInstance.constructor
)) {
throw new Error(`Not a ${global[referenceKey].constructor.name} instance`)
}

const oldInstance = Object.create(global[referenceKey])

for (let prop in global[referenceKey]) {
oldInstance[prop] = global[referenceKey][prop]
delete global[referenceKey][prop]
}

for (let prop in newInstance) {
global[referenceKey][prop] = newInstance[prop]
}

return oldInstance
}

export function assignGlobalConsola (newConsola) {
return assignGlobalReference(newConsola, 'consola')
}
86 changes: 86 additions & 0 deletions test/assign-reference.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { assignGlobalConsola } from '../src'

describe('assignGlobalConsola', () => {
test('global reference intact', () => {
class TestClass {
constructor (param) {
this.param = param
}
}

const consola1 = new TestClass('my-consola1')
global.consola = consola1
const json1 = JSON.stringify(consola1)

const consola2 = new TestClass('my-consola2')
const consola3 = assignGlobalConsola(consola2)

expect(consola3).not.toBe(consola1)
expect(global.consola).toBe(consola1)
expect(global.consola).not.toBe(consola2)
expect(global.consola).not.toBe(consola3)
expect(JSON.stringify(consola1)).toEqual(JSON.stringify(consola2))
expect(JSON.stringify(consola3)).toEqual(json1)
})

test('fn binds are intact', () => {
class TestClass {
constructor (param) {
this.param = param
this.fn = this.createFn(param)
}

createFn (param) {
function fn () {
return param
}
return fn.bind(this)
}
}

const param1 = 'my-consola1'
const consola1 = new TestClass(param1)
global.consola = consola1

expect(consola1.fn()).toBe(param1)

const param2 = 'my-consola2'
const consola2 = new TestClass(param2)
const consola3 = assignGlobalConsola(consola2)

expect(consola2.fn()).toBe(param2)
expect(consola3.fn()).toBe(param1)
expect(global.consola.fn()).toBe(param2)
})

test('cannot assign different constructor', () => {
class TestClass {}
class TestClass2 {}
global.consola = new TestClass()

expect(() => {
assignGlobalConsola(new TestClass2())
}).toThrow(/Not a TestClass instance/)
})

test('can assign inherited constructor to base', () => {
class TestClass {}
class TestClass2 extends TestClass {}
global.consola = new TestClass()

expect(() => {
assignGlobalConsola(new TestClass2())
}).not.toThrow()
})

test('can assign base constructor to inherited', () => {
class TestClass {}
class TestClass2 extends TestClass {}
class TestClass3 extends TestClass2 {}
global.consola = new TestClass3()

expect(() => {
assignGlobalConsola(new TestClass())
}).not.toThrow()
})
})
7 changes: 3 additions & 4 deletions test/custom-consola.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

describe('custom consola', () => {
describe('require', () => {
afterEach(() => {
delete global.consola
jest.resetModules()
jest.resetModules() // jest equivalent to delete require.cache
})

test('require twice has same consola', () => {
Expand Down Expand Up @@ -40,7 +39,7 @@ describe('custom consola', () => {
const consola1 = 'my-consola2'
global.consola = consola1

jest.resetModules() // jest equivalent to delete require.cache
jest.resetModules()

const consola2 = require('consola')
expect(consola1 === consola2).toBe(true)
Expand Down
33 changes: 29 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
dependencies:
"@babel/highlight" "^7.0.0"

"@babel/core@^7.0.0-beta.39":
"@babel/core@^7.0.0-beta.39", "@babel/core@^7.1.2":
version "7.1.2"
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.1.2.tgz#f8d2a9ceb6832887329a7b60f9d035791400ba4e"
integrity sha512-IFeSSnjXdhDaoysIlev//UzHZbdEmm7D0EIH2qtse9xK7mXEZQpYjs2P00XlP1qYsYvid79p+Zgg6tz1mp6iVw==
Expand Down Expand Up @@ -221,7 +221,7 @@
esutils "^2.0.2"
js-tokens "^4.0.0"

"@babel/parser@^7.1.0", "@babel/parser@^7.1.2":
"@babel/parser@^7.0.0", "@babel/parser@^7.1.0", "@babel/parser@^7.1.2":
version "7.1.2"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.1.2.tgz#85c5c47af6d244fab77bce6b9bd830e38c978409"
integrity sha512-x5HFsW+E/nQalGMw7hu+fvPqnBeBaIr0lWJ2SG0PPL2j+Pm9lYvCrsZJGIgauPIENx0v10INIyFjmSNUD/gSqQ==
Expand Down Expand Up @@ -561,7 +561,7 @@
"@babel/helper-regex" "^7.0.0"
regexpu-core "^4.1.3"

"@babel/preset-env@^7.0.0-beta.39":
"@babel/preset-env@^7.0.0-beta.39", "@babel/preset-env@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@babel/preset-env/-/preset-env-7.1.0.tgz#e67ea5b0441cfeab1d6f41e9b5c79798800e8d11"
integrity sha512-ZLVSynfAoDHB/34A17/JCZbyrzbQj59QC1Anyueb4Bwjh373nVPq5/HMph0z+tCmcDjXDe+DlKQq9ywQuvWrQg==
Expand Down Expand Up @@ -617,7 +617,7 @@
"@babel/parser" "^7.1.2"
"@babel/types" "^7.1.2"

"@babel/traverse@^7.1.0":
"@babel/traverse@^7.0.0", "@babel/traverse@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.1.0.tgz#503ec6669387efd182c3888c4eec07bcc45d91b2"
integrity sha512-bwgln0FsMoxm3pLOgrrnGaXk18sSM9JNf1/nHC/FksmNGFbYnPWY4GYCfLxyP1KRmfsxqkRpfoa6xr6VuuSxdw==
Expand Down Expand Up @@ -1008,6 +1008,23 @@ babel-core@^6.0.0, babel-core@^6.26.0:
slash "^1.0.0"
source-map "^0.5.7"

babel-core@^7.0.0-bridge.0:
version "7.0.0-bridge.0"
resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-7.0.0-bridge.0.tgz#95a492ddd90f9b4e9a4a1da14eb335b87b634ece"
integrity sha512-poPX9mZH/5CSanm50Q+1toVci6pv5KSRv/5TWCwtzQS5XEwn40BcCrgIeMFWP9CKKIniKXNxoIOnOq4VVlGXhg==

babel-eslint@^10.0.1:
version "10.0.1"
resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-10.0.1.tgz#919681dc099614cd7d31d45c8908695092a1faed"
integrity sha512-z7OT1iNV+TjOwHNLLyJk+HN+YVWX+CLE6fPD2SymJZOZQBs+QIexFjhm4keGTm8MW9xr4EC9Q0PbaLB24V5GoQ==
dependencies:
"@babel/code-frame" "^7.0.0"
"@babel/parser" "^7.0.0"
"@babel/traverse" "^7.0.0"
"@babel/types" "^7.0.0"
eslint-scope "3.7.1"
eslint-visitor-keys "^1.0.0"

babel-generator@^6.18.0, babel-generator@^6.26.0:
version "6.26.1"
resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.26.1.tgz#1844408d3b8f0d35a404ea7ac180f087a601bd90"
Expand Down Expand Up @@ -2431,6 +2448,14 @@ eslint-plugin-vue@^4.7.1:
dependencies:
vue-eslint-parser "^2.0.3"

[email protected]:
version "3.7.1"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-3.7.1.tgz#3d63c3edfda02e06e01a452ad88caacc7cdcb6e8"
integrity sha1-PWPD7f2gLgbgGkUq2IyqzHzctug=
dependencies:
esrecurse "^4.1.0"
estraverse "^4.1.1"

eslint-scope@^3.7.1:
version "3.7.3"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-3.7.3.tgz#bb507200d3d17f60247636160b4826284b108535"
Expand Down

0 comments on commit 1af28f7

Please sign in to comment.