question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest snapshot test failed with TypeError: Cannot read property 'extend' of undefined

See original GitHub issue

Below is the sample code.

Filename: TestComponent.svelte
<script>
  import * as dayjs from 'dayjs'
  import relativeTime from 'dayjs/plugin/relativeTime'
  dayjs.extend(relativeTime)

  dayjs('1999-01-01').fromNow()
</script>
<div>
Hello World !!!
</div>

Filename: TestComponent.test.js

import { render } from '@testing-library/svelte'
import TestComponent from './TestComponent.svelte'

describe('Render component', () => {
  it('should render component correctly', () => {
    const { component } = render(TestComponent)
    expect(component).toMatchSnapshot()
  })
})

Information dayjs : 1.10.4

** Error ** image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

8reactions
jeserkincommented, Apr 25, 2021

Yes. Just had wrong import. For some reason it worked on application level both ways, but on test level it didn’t. Once the import was changed and was same on both sides it was working as intended. At the moment, I have following imports:

import { Dayjs } from 'dayjs';
import * as dayjs from 'dayjs';
import * as en from 'dayjs/locale/en';
import * as relativeTime from 'dayjs/plugin/relativeTime';

Also I am calling now dayjs.extend(...); inside constructor. This is my case, other people requirements can be different.

0reactions
harvanchikcommented, Oct 15, 2021

@rogerprz What version of Angular are you using? What version of dayjs?

Im using React with typescript. The latest "dayjs": "^1.10.7", The file is a javascript file.

Oh, hmm. Well, I’ve done basically the same thing as you, but in Angular, and get no error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jestjs - Jest 28: createJestExpect - TypeError: Cannot read ...
Test suite failed to run TypeError: Cannot read properties of undefined (reading 'extend') at createJestExpect ...
Read more >
cannot read property 'testenvironmentoptions' of undefined
The "Cannot read property 'testEnvironmentOptions' of undefined" error occurs when a property is read or a function is called on an undefined variable...
Read more >
Expect · Jest
If you mix them up, your tests will still work, but the error messages on failing tests will look strange. expect.extend(matchers) #. You...
Read more >
API Reference
import { assert, test } from 'vitest' test.skip('skipped test', () => { // Test skipped, no error assert.equal(Math.sqrt(4), 3) }) ...
Read more >
Configuring Jest
Jest will run .mjs and .js files with nearest package.json 's type field set to module as ECMAScript Modules.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found