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.

Beter unit test docs

See original GitHub issue

What problem does this feature solve?

A better guidance through the jungle of Unit tests in Vue…

What does the proposed API look like?

no related

[x] improvement docs

Hi,

Im deep into Angular, and doing my first Vue project for few months now. Hitting Vue unit test for the first time now and I believe the docs should be improved.

I’ll give an example, that was breaking my head…

export default {
  name: 'TimeButtons',
  props: ['value'],
  data() {
    return {
      selected: 'radio1',
      options: []
    };
  },
  computed: {
    model: {
      get() {
        return this.value;
      },
      set(newValue) {
        this.$emit('input', newValue);
      }
    }
  },
  methods: {
    createOptions() {
       // i'll add options here
      this.options = []
      ];
    }
  },
  created() {
    this.createOptions();
  }
};
</script>

Now, I want to test if createOptions is called on created

So I follow the docs and write:

    const wrapper = mount(TimeButtons, {
      stubs: [
        'bFormGroup',
        'bFormRadioGroup'
      ]
    });

    wrapper.setMethods({
       createOptions: jest.fn()
    });
    
    expect(wrapper.vm.createOptions.mock.calls.length).toBe(1);

But im getting

 Expected value to be:
      1
    Received:
      0

The problem was that my Jest.fn was added later than the mount… so the component was already created… but how should I add a jest.fn on a method before the wrapper?

I tried (but fails)

  beforeEach(() => {
    TimeButtons.methods.createOptions = jest.fn();
  });

Well… it seems there is a property methods on mount which is not mentioned in the docs 😦

https://vue-test-utils.vuejs.org/api/options.html#context

so the solutions was

  it('should call createOptions on created', () => {
    const mockMethods = {
      createOptions: jest.fn()
    };

    const wrapper = mount(TimeButtons, {
      stubs: [
        'bFormGroup',
        'bFormRadioGroup'
      ],
      methods: mockMethods
    });
    expect(mockMethods.createOptions.mock.calls.length).toBe(1);
  });
  1. These basic examples should be explained in the unit test docs.
  2. The unit test docs should mention methods on mount
  3. Overall i feel the unit test docs are lacking examples and larger explanations

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
posvacommented, Feb 21, 2019

Please search for it. A Google search will give you the link in less time it took you to post the comment. And please don’t be so demanding and negative, it’s not really helping us… If the methods property is lacking in the docs, you should be able to open a pull request adding it and we would be grateful for that help. Thank you

0reactions
posvacommented, Feb 21, 2019

Okay, that’s fair, it wasn’t negative, just inappropriately demanding. My bad

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Tests are the Best Documentation
In my opinion, they are the best form of documentation - even better than developer written wiki pages. Unit tests are a working...
Read more >
Best practices for writing unit tests - .NET - Microsoft Learn
There are numerous benefits of writing unit tests; they help with regression, provide documentation, and facilitate good design.
Read more >
Unit Tests as Documentation - YouTube
When is a test more than just a test ? When it's living documentation for the intended behaviour of your system.In this video...
Read more >
unittest — Unit testing framework — Python 3.11.1 ...
A test case is the individual unit of testing. It checks for a specific response to a particular set of inputs. unittest provides...
Read more >
How to better write unit tests - Ponicode
But it's easy to make tests worthless as documentation if we don't use good test names and descriptions. As you refactor, review or...
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