vincent15000's avatar

Testing VueJS

Hello,

For a Laravel / VueJS application, what do you suggest for testing ?

I have seen some suggestions in the VueJS official documentation :

  • vuejs/test-utils

  • vitest

  • nightwatch

test-utils seems to be appropriated to test components, vitest too but impossible to find anything in the documentation, nightwatch seems to have a pretty interesting documentation to begin with.

Have you any experience with these packages ?

What do you suggest me ?

Thanks for your answer.

V

0 likes
4 replies
LaryAI's avatar
Level 58

When it comes to testing a Laravel/VueJS application, there are several tools and libraries you can use, each serving different purposes. Here's a breakdown of the tools you mentioned and some recommendations:

1. vue-test-utils

vue-test-utils is the official unit testing utility library for Vue.js. It provides methods to mount and interact with Vue components, making it ideal for unit testing individual components.

Example:

import { mount } from '@vue/test-utils'
import MyComponent from '@/components/MyComponent.vue'

test('renders a message', () => {
  const wrapper = mount(MyComponent, {
    props: {
      msg: 'Hello World'
    }
  })
  expect(wrapper.text()).toContain('Hello World')
})

2. Vitest

vitest is a Vite-native unit testing framework. It's fast and integrates well with Vite, making it a good choice if you're using Vite for your Vue project.

Example:

import { mount } from '@vue/test-utils'
import { describe, it, expect } from 'vitest'
import MyComponent from '@/components/MyComponent.vue'

describe('MyComponent', () => {
  it('renders a message', () => {
    const wrapper = mount(MyComponent, {
      props: {
        msg: 'Hello World'
      }
    })
    expect(wrapper.text()).toContain('Hello World')
  })
})

3. Nightwatch

nightwatch is an end-to-end (E2E) testing framework. It allows you to write tests that simulate user interactions with your application, making it suitable for testing the entire application flow.

Example:

module.exports = {
  'Demo test Google': function (browser) {
    browser
      .url('http://www.google.com')
      .waitForElementVisible('body', 1000)
      .setValue('input[type=text]', 'nightwatch')
      .waitForElementVisible('input[name=btnK]', 1000)
      .click('input[name=btnK]')
      .pause(1000)
      .assert.containsText('#main', 'Nightwatch.js')
      .end();
  }
};

Recommendations

  • Unit Testing: Use vue-test-utils along with a testing framework like vitest or jest. This combination is powerful for testing individual Vue components.
  • End-to-End Testing: Use nightwatch or cypress for E2E testing. These tools are designed to test the entire application flow and user interactions.

Example Setup

Here's a basic setup for unit testing with vue-test-utils and vitest:

  1. Install dependencies:

    npm install @vue/test-utils vitest --save-dev
    
  2. Configure vitest in your package.json:

    {
      "scripts": {
        "test": "vitest"
      }
    }
    
  3. Create a test file:

    // tests/MyComponent.spec.js
    import { mount } from '@vue/test-utils'
    import { describe, it, expect } from 'vitest'
    import MyComponent from '@/components/MyComponent.vue'
    
    describe('MyComponent', () => {
      it('renders a message', () => {
        const wrapper = mount(MyComponent, {
          props: {
            msg: 'Hello World'
          }
        })
        expect(wrapper.text()).toContain('Hello World')
      })
    })
    
  4. Run your tests:

    npm run test
    

By using these tools, you can ensure that your Vue components and application as a whole are well-tested and reliable.

gych's avatar

I mostly use Laravel + Vue with Inertia and then I use Laravel Dusk for tests.

But if I look at the suggestions you mentioned, nightwatch looks kind of similar to Laravel Dusk so personally I would use that.

1 like
vincent15000's avatar

@gych I know Laravel Dusk too, I didn't think about using it to test VueJS ... but perhaps it's possible ?

Hmmmm ... no ... not possible ... the router isn't the same between back and front ... so I need to use another package to test VueJS.

I will try Nightwatch.

1 like
gych's avatar
gych
Best Answer
Level 29

@vincent15000 Yes indeed I also think that Laravel Dusk will only work if your front and backend is connected with Inertia but if separated I also think nightwatch will be the best option since the syntax looks kind of similar to Laravle Dusk and therefor it will be a lower learning curve when you already know Laravel Dusk.

Goodluck ! :)

1 like

Please or to participate in this conversation.