posva / vue-router-mock

๐Ÿงช Easily mock routing interactions in your Vue apps
MIT License
198 stars 7 forks source link

index.mjs causes jest to throw (regression in v1.0.4) #145

Closed cexbrayat closed 2 years ago

cexbrayat commented 2 years ago

Reproduction

A CLI app using Jest and createRouterMock in a unit test will throw when upgrading to v1.0.4 with:

/Users/cedric/Code/ninjasquad/vue-ebook/book-tests/node_modules/vue-router-mock/dist/index.mjs:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { createRouter, createMemoryHistory, START_LOCATION, routerKey, routeLocationKey, routerViewLocationKey, matchedRouteKey, RouterView, RouterLink } from 'vue-router';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { flushPromises, mount } from '@vue/test-utils';
    > 2 | import { createRouterMock, injectRouterMock } from 'vue-router-mock';

This is because the main file switched to the mjs format, which is great for Vite but not ideal for Jest.

Expected behavior

Jest tests should still work

posva commented 2 years ago

It should be a matter of configuring unbuild to output cjs. Contribution welcome!

cexbrayat commented 2 years ago

Don't you think this https://github.com/posva/vue-router-mock/commit/b7fd685c03a72f767b50fa4c5ed4e45f0385f5a3#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R5 is the source of the regression?

posva commented 2 years ago

No, I did that because there is no cjs file generated at the moment

pgarciaegido commented 2 years ago

I am working on a PR to build both .mjs and .cjs files. ๐Ÿ‘

posva commented 2 years ago

@pgarciaegido I actually just found the fix and pushed it ๐Ÿ˜…

pgarciaegido commented 2 years ago

Amazing @posva !

Just one little comment, do you think it makes sense to use the exports field to expose both files?

"exports": {
    "require": "./dist/index.cjs",
    "import": "./dist/index.mjs"
  },

Thanks a lot! :smile:

posva commented 2 years ago

Totally! Published a new verison