unjs / consola

🐨 Elegant Console Logger for Node.js and Browser
Other
6k stars 175 forks source link

Upgrading to consola v3 breaks testing environment #195

Open gbenson-ir opened 1 year ago

gbenson-ir commented 1 year ago

Environment

Node v18.15.0 NPM v8.19.2 Consola v3.1.0

Reproduction

  1. Clone https://github.com/gbenson-ir/consola-v3-bug-reproduction
  2. Enter the directory
  3. Run npm i
  4. Run npm run test

Describe the bug

Since upgrading to v3 of consola, I am unable to run my unit tests. My tests fail and I receive the error "Cannot find module 'consola'".

This happens whether or not I use the new named exports.

Upgrading consola is the only change I have made to my project; before the upgrade I did not have the issue, following the upgrade I do. Prior to the upgrade I was using consola v2.15.3.

Additional context

I'm using Jest to run my unit tests. In my actual project I'm using TypeScript, but in the repo linked above it's a simple javascript project that also uses Jest (and babel-jest to transform the JS) and the issue is still present.

Logs

Cannot find module 'consola' from 'src/logger.test.js'

  2 | import { logger } from './logger';
  3 |
> 4 | jest.mock('consola', () => ({
    |      ^
  5 |   consola: {
  6 |       log: jest.fn()
  7 |   }

  at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:427:11)
  at Object.mock (src/logger.test.js:4:6)
everettblakley commented 1 year ago

I am also experiencing this issue and cannot upgrade because of this

jschroeter commented 1 year ago

Same issue here. Any idea?

jschroeter commented 1 year ago

I worked around it by mocking Consola in the tests, maybe it helps someone:

jest.config.js

module.exports = {
  moduleNameMapper: {
    consola: "<rootDir>/test/unit/test-utils/mockConsola.js"
  }
};

mockConsola.js

// mocking because of incompatibility between consola v3 & jest, see https://github.com/unjs/consola/issues/195

const LogTypes = [
  "silent",
  "fatal",
  "error",
  "warn",
  "log",
  "info",
  "success",
  "fail",
  "ready",
  "start",
  "box",
  "debug",
  "trace",
  "verbose",
];

class Consola {
  constructor() {
    const types = LogTypes;

    LogTypes.forEach((type) => {
      this[type] = function () {
        if (["error", "warn"].includes(type)) console[type](...arguments);
      };
    });
  }

  mockTypes(mockFn) {
    LogTypes.forEach((type) => {
      this[type] = mockFn(type);
    });
  }

  create() {
    return new Consola();
  }
}

const createConsola = function () {
  return new Consola();
};

const consola = createConsola();

export { consola, createConsola };

Basically it mutes all consola logs except of warn and error which are printed via native console.

jschroeter commented 1 year ago

Maybe related: I'm also having issues using consola v3 with Nuxt2 + Nuxt Bridge. Dev mode works fine, building as well. But when I run nuxi start, I get:

 │  You are running Nuxt production build in preview mode.
 │  Node.js:           v18.14.2
 │  Nitro Preset:      node-server
 │  Working directory: .output

Listening on http://0.0.0.0:3000
[nuxt] [request error] [unhandled] [500] Object(...) is not a function
  at ./server/chunks/app/server.mjs:41322:67  
  at callWithNuxt (./server/chunks/app/server.mjs:10259:25)  
  at ./server/chunks/app/server.mjs:10264:31  
  at createApp (./server/chunks/app/server.mjs:41692:16)  
  at async __webpack_exports__.default (./server/chunks/app/server.mjs:41820:12)  
  at async Object.renderToString (./server/chunks/handlers/renderer.mjs:216:19)  
  at async ./server/chunks/handlers/renderer.mjs:657:21  
  at async ./server/chunks/handlers/renderer.mjs:18:22  
  at async Object.handler (./server/chunks/nitro/node-server.mjs:2006:19)  
  at async Server.toNodeHandle (./server/chunks/nitro/node-server.mjs:2195:7)

which points to the line where I call createConsola().

mvalenko-f9 commented 2 days ago

It's because package.json["exports.default"] was missing an entry for require for jest to correctly resolve it:

"require": "./dist/browser.cjs"

Now it's in repo but not released yet. @pi0 is it possible to release the fix?