image icon indicating copy to clipboard operation
image copied to clipboard

Expose $Img for external Testing

Open ferdynator opened this issue 4 years ago • 0 comments

We have a component that uses direct access to $img.getImage to generate image URLs. To test this component we write simple jest snapshot tests. Unfortunately I have not been able to inject $img into our component to run tests without mocks. This is our current approach:

describe('OurImageComponent', () => {
  function buildWrapper () {
    return mount(
      OurImageComponent,
      {
        propsData: { // ...
        },
        mocks: {
          $img: {
            getImage (url, options) { // some mock implementation
            },
          },
        },

( Inspired by https://github.com/nuxt/image/blob/main/test/unit/utils/mount.ts ) This works well but with mocking the actual call we generate unclean snapshot files and not really exact URLs. Is there a way to create an instance of $Img manually in a test environment or would it be an option to expose the
createImage function so one could create an instance manually?

ferdynator avatar Oct 25 '21 16:10 ferdynator