Skip to content

fix(#119): allow vue-router creation through routes param from both jest and mocha #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/__tests__/vue-router-mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import '@testing-library/jest-dom'
import {render} from '@testing-library/vue'

import About from './components/Router/About.vue'

const routes = []
test('uses require("vue-router").default when require("vue-router") is undefined (useful for mocha users)', () => {
// Test for fix https://github.com/testing-library/vue-testing-library/issues/119
jest.mock('vue-router', () => {
return undefined
})

expect(() => render(About, {routes})).toThrowError(
new TypeError("Cannot read property 'default' of undefined"),
)
})
2 changes: 1 addition & 1 deletion src/vue-testing-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function render(
}

if (routes) {
const VueRouter = require('vue-router')
const VueRouter = require('vue-router') || require('vue-router').default
Copy link
Contributor Author

@agualis agualis Mar 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix-code is ugly cause the problem is that in mocha unit tests we need to bundle the code with webpack (mocha-webpack) and that does not play well with using a require here.

The problem could be also solved with an async import (to follow ES6 notation) but that would probably make the code even uglier cause it would introduce async code.

Another workaround when using mocha, would be passing an already built router through router mounting option (instead of routes) but that cannot work be done with the current implementation.

Enabling this option for mocha users would be highly appreciated. Thanks for your awesome work!

localVue.use(VueRouter)
router = new VueRouter({
routes,
Expand Down