import { assignGlobalConsola } from "../src"

describe("assignGlobalConsola", () => {
  test("global reference intact", () => {
    const s = Symbol("test")
    class TestClass {
      constructor (param) {
        this.param = param
      }

      get symbol () {
        return this[s]
      }

      set symbol (newVal) {
        this[s] = newVal
      }
    }

    const symbol1 = "my-symbol1"
    const consola1 = new TestClass("my-consola1")
    consola1.symbol = symbol1
    global.consola = consola1
    const json1 = JSON.stringify(consola1)

    const symbol2 = "my-symbol2"
    const consola2 = new TestClass("my-consola2")
    consola2.symbol = symbol2
    const consola3 = assignGlobalConsola(consola2)

    expect(consola3).not.toBe(consola1)
    expect(consola3.symbol).toBe(symbol1)

    expect(global.consola).toBe(consola1)
    expect(global.consola).not.toBe(consola2)
    expect(global.consola).not.toBe(consola3)

    expect(global.consola.symbol).toBe(consola1.symbol)
    expect(global.consola.symbol).toBe(consola2.symbol)
    expect(global.consola.symbol).not.toBe(consola3.symbol)

    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()
  })
})

Related articles

consola pause

#!/usr/bin/env node -r esm import { consola } from "./utils" const c1 = consola.withTag("foo") const c2 = consola.withTag("bar") consola.log("before pause") c2.pause() c1.log("C1 is ready") c2.log("C2 is ready") setTimeout(() => { consola.resume(

consola basic

#!/usr/bin/env node -r esm import { BasicReporter } from "../src" import { reporterDemo } from "./utils" reporterDemo(new BasicReporter({}))

consola tsconfig

{ "compilerOptions": { "baseUrl": ".", "moduleResolution": "node", "noEmit": true, // "strict": true, "paths": { "consola": [ "../.." ] } } }

consola index.legacy

<!DOCTYPE html> <html> <head> <title>Consola</title> </head> <body> Open developer tools to see the magic! <script src="../dist/consola.browser.js"></script> <script> consola.level = 5 consola.wrapAll() for (let

consola index

import { Consola, FancyReporter } from "../../src" import { randomSentence } from "./sentence" export function reporterDemo (reporter) { const consola = new Consola({ level: 5, reporters: [ reporter ] }) for (const type of Objec

consola index

<!DOCTYPE html> <html> <head> <title>Consola</title> </head> <body> Open developer tools to see the magic! <script type="module"> import consola from "../src/browser.js" consola.level = 5 consola.wrapAll() for

consola special

#!/usr/bin/env node -r esm import { consola } from "./utils" consola.error({ message: "Foobar" }) consola.log({ AAA: "BBB" }) // consola.log(consola) consola.log("%d", 12) consola.error({ type: "CSSError", message: "Use scss" }) consola.error(

consola fancy

#!/usr/bin/env node -r esm import { FancyReporter } from "../src" import { reporterDemo } from "./utils" reporterDemo(new FancyReporter({}))

consola mock

#!/usr/bin/env node -r esm import { consola } from "./utils" function mockFn (type) { if (type === "info") { return () => this.log("INFO INFO INFO") } } consola.info("before") consola.mockTypes(mockFn) const tagged = consola.withTag("newTag"

consola require.test

describe("require", () => { afterEach(() => { delete global.consola jest.resetModules() // jest equivalent to delete require.cache }) test("require twice has same consola", () => { const consola1 = require("consola") jest.resetModu