-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add assignGlobalConsola helper
- Loading branch information
Showing
8 changed files
with
178 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"env": { | ||
"test": { | ||
"presets": [ | ||
["@babel/preset-env", { | ||
"targets": { | ||
"node": "current" | ||
} | ||
}] | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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== | ||
|
@@ -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== | ||
|
@@ -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== | ||
|
@@ -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== | ||
|
@@ -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" | ||
|
@@ -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" | ||
|