I have this function:
import {date as DateUtils} from 'quasar' export class FormatDate { static dateTime(date: Date) : string { return dateUtils.formatDate(date, "DD.MM.YYYY HH:mm:ss") } } that I'm trying to test with Jest like:
it('Should get the correct formatted date', () => { const date = DateUtils.buildDate({year: 2021, month: 11, date: 28, hours: 12, minutes: 11}) expect(FormatDate.dateTime(date)).toEqual('28.11.2021 12:11:00') }) But, when I run the test, it gives me this error:
TypeError: Cannot read property 'date' of undefined at Object.formatDate (/node_modules/quasar/dist/quasar.cjs.prod.js:6:132667) at Function.dateTime (/src/components/FormatDate.ts:6:26) at Object.<anonymous> (/test/jest/__tests__/FormatDate.spec.ts:19:34) at Object.asyncJestTest (/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37) at /node_modules/jest-jasmine2/build/queueRunner.js:45:12 at new Promise (<anonymous>) at mapper (/node_modules/jest-jasmine2/build/queueRunner.js:28:19) at /node_modules/jest-jasmine2/build/queueRunner.js:75:41 at processTicksAndRejections (internal/process/task_queues.js:95:5) Doesn anyone know how to fix this?
21 Answer
Normally this can be solved through configurating quasar in a local vue instance:
import { Quasar } from "quasar"; import langEn from "quasar/lang/en"; const localVue = createLocalVue(); localVue.use(Quasar, {}, { lang: langEn }); A similar issue was discussed in the quasar project.
Buuuut, i'm curious if someone would have an additional solution.
Because i for myself have a similar issue, but in another context.
Through a terrible architecture (please do not ask why) our project is using the date helpers outside of the vue component contexts. As part of a simple functional helper for example. Like:
const defaultDateFormat = (date: Date): string => formatDate(date, "whatever-pattern"); Running a test on this results in the same issue described by the author.
The described solution before does not apply here.
In short:
- using a quasar date formatter outside of a vue component
- running a jest test on this helper
- fails
So, if anyone has an idea my ears are open.