-
Notifications
You must be signed in to change notification settings - Fork 16
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
Jest testing: TypeError: $dayjs is not a function #296
Comments
@mmeester |
any ideas about this? |
Hi @mmeester, meanwhile you can create a mock of $dayjs into $nuxt.context for your test, like this: import dayjs from 'dayjs';
import { mount } from "@vue/test-utils";
import PublishDate from "@/components/PublishDate.vue";
// Waiting on response for this ticket to see how we can solve this (https://github.com/nuxt-community/dayjs-module/issues/296)
describe("Badge", () => {
test('Expect date to show "February 2021"', () => {
const wrapper = mount(PublishDate, {
propsData: {
date: "2021-02-06T17:22+02:00"
},
mocks: {
$nuxt: {
context: {
$dayjs: dayjs
},
},
},
});
const PublishDateEl = wrapper.find("time");
expect(PublishDateEl.element.textContent).toBe("February 2021");
});
}); I think you can add a global mock for $dayjs in your jest.config.js#setupFiles // jest-setup-file.js
import dayjs from 'dayjs';
import { config } from '@vue/test-utils';
config.mocks = {
$nuxt: {
$context: {
$dayjs: dayjs
}
}
}``` |
Any progress on this? We are still encountering this issue, @christian-bravo7's solution no longer works with Nuxt's recommended approach for unit testing components |
I'm trying to use dayjs in one of our tests but keep running into the following TypeError:
TypeError: $dayjs is not a function
It seems like dayjs isn't part of the context:
PublishDate.vue
PublisDate.spec.js
The text was updated successfully, but these errors were encountered: