Skip to content

Conversation

@suchmaske
Copy link
Contributor

I have struggled the last days to properly test vue components using ava. To make life easier for others, I added some additional recipes for testing vuejs using ava.

For example:

  • testing the DOM of the component after a change in the data model
  • testing data model changes when using async requests and promises
  • testing DOM updates when promises are involved
Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@suchmaske nice! I've pushed a commit that fixes some formatting, and I have a question around nextTick() usage.

@blake-newman @OmgImAlexis @knpwrs if you're around, could you give this a look?

vm.message = 'my new message';
// this fails here: t.is('my new message', vm.$el.textContent)

Vue.nextTick(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is asynchronous? That'll mean the assertion is run when the test is ended. We don't currently warn about that, but it means the assertion is ignored. You'd have to get a promise out of this or use test.cb() with t.end().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the Vue docs:

New in 2.1.0+: returns a Promise if no callback is provided and Promise is supported in the execution environment. - https://vuejs.org/v2/api/#Vue-nextTick

So you can just use an async function for the test and await it here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with using async and await syntax.

@sindresorhus sindresorhus changed the title Add more vuejs recipes Add more Vue.js recipes Jul 23, 2017
vm.message = 'my new message';
// this fails here: t.is('my new message', vm.$el.textContent)

Vue.nextTick(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the Vue docs:

New in 2.1.0+: returns a Promise if no callback is provided and Promise is supported in the execution environment. - https://vuejs.org/v2/api/#Vue-nextTick

So you can just use an async function for the test and await it here.

// just testing the data model can be achieved by using await
t.is(await vm.message, 'my message');

await vm.message.then((data) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .then() here is moot. You're already await'ing it. Just get value back the async/await way.

hooks(['vue', 'js']).plugin('babel').push();
```

When you are using ES6 modules, use the dist-file of Vue. Not doing that may cause a not completely accessible (and thus not testable) Vue model.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ES6 => ES2015

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not true. Using the hooks compiles the es6 to es2015. You can use the source components.

```js
import browserEnv from 'browser-env';
import hooks from 'require-extension-hooks';
import Vue from 'vue/dist/vue';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this type of import not require webpack? I didn't think this worked out of the box with babel, etc. yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure of the point of this example, same as above but es imports.

@sindresorhus can you confirm @OmgImAlexis is also correct.

hooks(['vue', 'js']).plugin('babel').push();
```

When you are using ES6 modules, use the dist-file of Vue. Not doing that may cause a not completely accessible (and thus not testable) Vue model.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not true. Using the hooks compiles the es6 to es2015. You can use the source components.

```js
import browserEnv from 'browser-env';
import hooks from 'require-extension-hooks';
import Vue from 'vue/dist/vue';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure of the point of this example, same as above but es imports.

@sindresorhus can you confirm @OmgImAlexis is also correct.

vm.message = 'my new message';
// this fails here: t.is('my new message', vm.$el.textContent)

Vue.nextTick(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with using async and await syntax.


// just testing the data model can be achieved by using await
t.is(await vm.message, 'my message');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would vm.message be a promise? No need to await this

@knpwrs
Copy link

knpwrs commented Sep 5, 2017

I think it's worth pointing out that Vue has started work on official testing tools. They have an example with AVA available as well.

@blake-newman
Copy link
Contributor

@knpwrs Indeed. Will create a PR to link to that example repository

@novemberborn
Copy link
Member

@blake-newman @knpwrs is this PR still relevant?

@suchmaske will you have time to revive this PR?

@suchmaske
Copy link
Contributor Author

@novemberborn I recently have changed my workflow to vue-test-utils. I will take the feedback and try to rework the PR in the next days.

@knpwrs
Copy link

knpwrs commented Oct 26, 2017

I don't personally use Vue enough anymore to offer any official recommendations.

@novemberborn
Copy link
Member

That's great, thank you @suchmaske.

@blake-newman
Copy link
Contributor

@novemberborn i don't think the PR is relevant. Also there is many examples, via vue-test-utils that enhance what is on offering here.

Sorry for the late reply

@sindresorhus
Copy link
Member

Closing this for lack of activity. Happy to reopen if anyone wants to finish it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

6 participants