Skip to content
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

How to write a test for Vue3 SFC components ? #61

Closed
2 tasks done
SuslegStyle opened this issue Jan 24, 2022 · 2 comments
Closed
2 tasks done

How to write a test for Vue3 SFC components ? #61

SuslegStyle opened this issue Jan 24, 2022 · 2 comments

Comments

@SuslegStyle
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Hi There.

I'd like to write unit tests for my Vue3 SFC components in my fastify-vite app.
I tried to use:

  • "@babel/preset-env": "^7.16.11",
  • "@vue/test-utils": "^2.0.0-rc.18",
  • "@vue/vue3-jest": "^27.0.0-alpha.4",
  • "babel-core": "^7.0.0-bridge.0",
  • "babel-jest": "^27.4.6",
  • "jest": "^27.4.7",

My jest.config.json looks like

module.exports = {
    moduleFileExtensions: ['js', 'json', 'vue'],
    testEnvironment: 'jsdom',
    transform: {
        '^.+\\.js$': 'babel-jest',
        '^.+\\.vue$': '@vue/vue3-jest'
    }
};

And my copyright.spec.js file looks

import {shallowMount} from '@vue/test-utils';
import Copyright from '../../../pageComponents/base/copyright.vue';

describe('Page components / Base / Copyright', () => {
    it('should have correct text', () => {
        const copyrightText = `My copyright text`;
        const wrapper = shallowMount(Copyright);

        expect(wrapper.text()).toContain(copyrightText);
    });
});

When I run tests, I see an error

TypeError: (0 , _vue.createElementVNode) is not a function

Could you please recommend me, how to fix it ? Maybe you have a working example, how to write tests for Vue3 components in fastify-vite apps ?

Thank you.

@wobsoriano
Copy link

wobsoriano commented May 25, 2022

I'd probably do something like this when testing SSR with Vue

import { createSSRApp, h } from 'vue'
import { renderToString } from '@vue/server-renderer'

it('should have correct text', async () => {
  const app = createSSRApp({
    setup() {
      return () => h(Copyright)
    }
  })
  
  const text = await renderToString(app)

  expect(text).toContain(`My copyright text`)
})

@galvez galvez closed this as completed Dec 5, 2023
@wobsoriano
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants