<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Testing nuxt applications - Medium]]></title>
        <description><![CDATA[Nuxt provides you with lots of tooling and ready-made solutions, but when it comes to testing it gives you minimal setup and documentation and you’re on your own. What do you do to make your tests work? - Medium]]></description>
        <link>https://medium.com/testing-nuxt-applications?source=rss----f6fec74faecc---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Testing nuxt applications - Medium</title>
            <link>https://medium.com/testing-nuxt-applications?source=rss----f6fec74faecc---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 13:20:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/testing-nuxt-applications" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to Nuxt plugins and modules]]></title>
            <link>https://medium.com/testing-nuxt-applications/introduction-to-nuxt-plugins-and-modules-7f10887ef31b?source=rss----f6fec74faecc---4</link>
            <guid isPermaLink="false">https://medium.com/p/7f10887ef31b</guid>
            <category><![CDATA[nuxtjs]]></category>
            <dc:creator><![CDATA[Antonis Christofides]]></dc:creator>
            <pubDate>Fri, 18 Jun 2021 17:45:39 GMT</pubDate>
            <atom:updated>2021-09-15T13:23:23.370Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0rHuWGOIrSq5RKAZ" /><figcaption>Not a Nuxt module, but a module nevertheless. Photo by <a href="https://unsplash.com/@markkoenig?utm_source=medium&amp;utm_medium=referral">Mark König</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><a href="https://medium.com/testing-nuxt-applications/testing-backend-communication-84a5bf02ae10">I have already explained</a> how we can use MSW to test our Nuxt application’s communication with the backend. I made it work by importing axios and assigning wrapper.vm.$axios = axios.</p><p>However, this is actually a bad hack, since @nuxtjs/axios and axios aren’t the same. The former is a Nuxt.js module that wraps axios and offers more functionality on top of it. @nuxtjs/axios and axios happen to have a similar API which enables us to use the latter in place of the former and mock any additional methods we use (such as setToken). But what will we do in other cases where we use a Nuxt.js module that does not wrap something with a similar API? How can we use a Nuxt.js module in our tests? To answer that we need to know some internals of Nuxt.js modules.</p><p>A Nuxt.js module is often a wrapper around a Nuxt.js plugin.</p><p>A Nuxt.js plugin is often a wrapper around a Vue plugin.</p><p>Let’s go through these from the bottom up.</p><h3>Vue plugins</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ddcbdc8a7efdfcb52ac9073644ba1af/href">https://medium.com/media/9ddcbdc8a7efdfcb52ac9073644ba1af/href</a></iframe><p>After the snippet above executes, myVueInstance.$hello will have the value “world”.</p><p>A Vue plugin is an object (or module) containing an install() function (the plugin can also be a function itself instead). What Vue.use() does is register the plugin and, if it is not already registered, execute the function.</p><p>Vue plugins can be installed (with Vue.use()) either before or after creating the Vue instance.</p><h3>Nuxt plugins</h3><p>A Nuxt plugin is a JavaScript file (typically in the plugins directory) whose name is specified in the plugins property in nuxt.config.js. The file may or may not export a function.</p><h4>Nuxt plugins that do not export a function</h4><p>Here is an example of a Nuxt plugin that does not export a function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb936441eb8a7832985f2f90edb5ceec/href">https://medium.com/media/fb936441eb8a7832985f2f90edb5ceec/href</a></iframe><p>When Nuxt starts, it merely imports the file.</p><p>When a Nuxt plugin merely wraps a Vue plugin, it can simply execute Vue.use(theVuePlugin).</p><h4>Nuxt plugins that export a function</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a2a695a08048b344c8f0878d58ee905/href">https://medium.com/media/4a2a695a08048b344c8f0878d58ee905/href</a></iframe><p>When Nuxt starts, it will import the file and execute the function, passing it the Nuxt context and the inject function. In this particular case, inject(&#39;hello&#39;, &#39;world&#39;) will do something similar to the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5afafe7ae36a1e00af7174f4d888e24e/href">https://medium.com/media/5afafe7ae36a1e00af7174f4d888e24e/href</a></iframe><p>Why not simply write the above instead of exporting a function that receives a callback inject function and calls it? Because the inject function is smart. Depending on Nuxt configuration and environment (e.g. SSR or client), it might decide to add the attribute only to Vue, or only to the context, or also add it to the store, etc. By just executing inject(&#39;hello&#39;, &#39;world&#39;), we tell Nuxt to add (“inject”) the $hello attribute to wherever it needs to be added.</p><h3>Nuxt modules</h3><p>A Nuxt module is a JavaScript file that exports a function and is specified in the modules property of nuxt.config.js. Nuxt calls the function when it starts.</p><p>The difference between a Nuxt module and a Nuxt plugin is that a module runs earlier. Nuxt first loads and executes modules, then creates the Vue instance and the Nuxt context, and then executes Nuxt plugins. As a result, Nuxt modules can do more customization; for example, they can add plugins. I’m still not entirely clear on that, but for the purposes of this post it doesn’t matter, because it is given that we have a module (@nuxtjs/axios) and we need to load it for testing.</p><h3>How @nuxtjs/axios works</h3><p>@nuxtjs/axios is a Nuxt module—a .js file that exports a function. Nuxt loads the file and executes the function.</p><p>One of the things the function does is execute Nuxt’s addPlugin() method. This is the way to programmatically load a plugin without needing to add it to the plugins property of nuxt.config.js. Since this plugin does not have a name, we will call it “the Nuxt axios plugin”.</p><p>So, Nuxt starts. It initializes the @nuxtjs/axios module. The module, among other things, executes Nuxt’s addPlugin(), thus registering the Nuxt axios plugin. Later (after finishing loading all modules, creating the Vue instance and Nuxt context, and possibly doing other initialization work) Nuxt will initialize the Nuxt axios plugin. What the plugin initialization does is create an Axios instance, extend it with some additional methods (such as setToken), and then call Nuxt’s inject to add it as the $axios property of Vue (and of the Nuxt context).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f10887ef31b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/testing-nuxt-applications/introduction-to-nuxt-plugins-and-modules-7f10887ef31b">Introduction to Nuxt plugins and modules</a> was originally published in <a href="https://medium.com/testing-nuxt-applications">Testing nuxt applications</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing custom Vue events]]></title>
            <link>https://medium.com/testing-nuxt-applications/testing-custom-vue-events-f2ed10e740fb?source=rss----f6fec74faecc---4</link>
            <guid isPermaLink="false">https://medium.com/p/f2ed10e740fb</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[jest]]></category>
            <dc:creator><![CDATA[Antonis Christofides]]></dc:creator>
            <pubDate>Mon, 31 May 2021 13:16:42 GMT</pubDate>
            <atom:updated>2021-05-31T13:16:42.431Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*e4Q8xGFKYKQweh9M" /><figcaption>Photo by <a href="https://unsplash.com/@testalizeme?utm_source=medium&amp;utm_medium=referral">Testalize.me</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This isn’t a Nuxt testing issue; it’s a Vue testing issue. All the same, it’s going to affect people working in Nuxt as well.</p><p>However, whenever you have a Vue (rather than a Nuxt) testing issue, the answer is likely to be in the excellent book “Testing Vue.js applications” by Edd Yerburgh, which you should really read. It’s going to save you from lots of headaches.</p><p>I have a LoginForm component that uses a modal. LoginForm looks like this (I’ve deleted everything that isn’t relevant to the issue at hand):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd127a8ec1acb3f4c3c42364988b0275/href">https://medium.com/media/cd127a8ec1acb3f4c3c42364988b0275/href</a></iframe><p>I want to test that when the modal appears, focus goes to the username field. This is how to achieve this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/774f2199add42b5dd34f0d8bd020500f/href">https://medium.com/media/774f2199add42b5dd34f0d8bd020500f/href</a></iframe><p>This works alright.</p><p>However, there is a subtle catch here. Suppose that in the next bootstrap-vue version, the b-modal component is changed so that instead of emitting the shown custom View event, it emits a renamed event, say showmodal. Now, after installing the new bootstrap-vue version and running the test suite, this test will pass.</p><p>Unit testing purists might say that I’ve done everything correctly. I’ve tested LoginForm in isolation, and I’ve verified it receives and handles the shown event. BModal should also be tested in isolation (and that’s what the bootstrap-vue people do), and it should be verified it emits the shown event. Verifying that LoginForm and BModal work correctly together is an integration test.</p><p>However, I think writing yet another test for this detail would be overkill. Therefore, I’d like to write this test a bit differently. Rather than asking the BModal component to emit a shown event, I’d prefer to ask the BModal component to show itself. However I don’t know (yet) how to do this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2ed10e740fb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/testing-nuxt-applications/testing-custom-vue-events-f2ed10e740fb">Testing custom Vue events</a> was originally published in <a href="https://medium.com/testing-nuxt-applications">Testing nuxt applications</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing backend communication]]></title>
            <link>https://medium.com/testing-nuxt-applications/testing-backend-communication-84a5bf02ae10?source=rss----f6fec74faecc---4</link>
            <guid isPermaLink="false">https://medium.com/p/84a5bf02ae10</guid>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[nuxtjs]]></category>
            <category><![CDATA[axios]]></category>
            <dc:creator><![CDATA[Antonis Christofides]]></dc:creator>
            <pubDate>Tue, 18 May 2021 11:23:05 GMT</pubDate>
            <atom:updated>2021-05-18T11:23:05.093Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DgrRckXUZTyfgOrx" /><figcaption>The River Axios; Photo by <a href="https://unsplash.com/@dpantos?utm_source=medium&amp;utm_medium=referral">dimitris pantos</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Using MSW</h3><p>The most common answer to the problem of how to test communication with the backend is to mock fetch or axios or whatever library you use for making http requests. However, StackOverflow user <a href="https://stackoverflow.com/users/3001761/jonrsharpe">jonrsharpe</a> <a href="https://stackoverflow.com/questions/67101502/how-to-mock-axios-with-jest/67107497#67107497">proposes a better method</a>: use <a href="https://mswjs.io/">msw</a>, the Mock Service Worker. It works by essentially creating a server that intercepts requests on the network level. Here is an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8d6e2726127ddff6a4ebd1a0e34a76c/href">https://medium.com/media/e8d6e2726127ddff6a4ebd1a0e34a76c/href</a></iframe><p>The server we have created responds to login requests:</p><ul><li>If the username and password are alice and topsecret, it responds with a token</li><li>If the username and password are alice and wrongpassword, it responds with an error message</li><li>Otherwise, it throws an exception, effectively simulating a network error.</li></ul><p>That’s it, essentially. You don’t need to mock anything. Whenever your main code attempts to talk to the network, MSW will intercept the request and provide a response.</p><p>But there are a few problems.</p><h3>Initializing Axios</h3><p>As I explained in <a href="https://medium.com/testing-nuxt-applications/testing-functionality-that-involves-the-store-c0bfd69adc98">my previous post</a>, it isn’t trivial to initialize Nuxt at the beginning of testing, so certain things like the store don’t exist. Likewise, if you are using @nuxtjs/axios, this.$axios will also not exist. Therefore, when your component or any part of your code tries to make a request by using this.$axios, it will stop and give you a message like “Cannot read property ‘post’ of undefined”. I haven’t solved that problem yet. Until I do, what you can do is something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fc3a58fed7645ead2b49e83760d2dfb/href">https://medium.com/media/4fc3a58fed7645ead2b49e83760d2dfb/href</a></iframe><p>(I have left the MSW part out to concentrate on the problem at hand; but you need to combine the two parts for it to actually work.)</p><p>So we kind of “mock” axios, but not with a mock function such as jest.fn(); instead, we assign it a real instance of Axios.</p><p>Unfortunately, this won’t solve all problems. The problem is that @nuxtjs/axios is not the same as axios; the former has a little bit more functionality. If your main code uses that additional functionality, such the .setToken() method, it won’t work during the tests. What you need to do in that case is to mock this additional functionality; for example,</p><pre>wrapper.vm.$axios.setToken = jest.fn()</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84a5bf02ae10" width="1" height="1" alt=""><hr><p><a href="https://medium.com/testing-nuxt-applications/testing-backend-communication-84a5bf02ae10">Testing backend communication</a> was originally published in <a href="https://medium.com/testing-nuxt-applications">Testing nuxt applications</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing functionality that involves the store]]></title>
            <link>https://medium.com/testing-nuxt-applications/testing-functionality-that-involves-the-store-c0bfd69adc98?source=rss----f6fec74faecc---4</link>
            <guid isPermaLink="false">https://medium.com/p/c0bfd69adc98</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[vue-test-utils]]></category>
            <category><![CDATA[nuxt]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[vuex]]></category>
            <dc:creator><![CDATA[Antonis Christofides]]></dc:creator>
            <pubDate>Mon, 17 May 2021 16:54:36 GMT</pubDate>
            <atom:updated>2021-05-17T16:57:03.156Z</atom:updated>
            <content:encoded><![CDATA[<p>I wanted to test this loggedOnUser() function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81235a61698287d45c0e1a57c6883794/href">https://medium.com/media/81235a61698287d45c0e1a57c6883794/href</a></iframe><p>So at first I tried this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e9bf4ca4b863b6dd358dfdecc965e79/href">https://medium.com/media/2e9bf4ca4b863b6dd358dfdecc965e79/href</a></iframe><p>Unfortunately this fails with a loud bang. It isn’t even able to mount the component. The reason is that the component needs to use loggedOnUser() during mounting, and this.$store is undefined.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*my18GOmZt4WISkWJoLOkKg.png" /></figure><p>When Nuxt runs normally, it automatically loads machinery that causes things like this.$store to exist. This machinery isn’t loaded when we try to run the test above; you need to load it manually. Unfortunately, the documentation is silent about how to do this, and <a href="https://github.com/nuxt/nuxtjs.org/issues/1190">people have complained about this silence</a>. <a href="https://medium.com/u/cffae3791947">Brandon Aaskov</a> has actually <a href="https://medium.com/@brandonaaskov/how-to-test-nuxt-stores-with-jest-9a5d55d54b28">written an article</a> on this.</p><p>Now, purists might stop me and point out that in unit tests you are supposed to be testing the functionality of a unit (in this case the loggedOnUser() function) without any interaction with the rest of the system (in this case the store). If the unit being tested depends on another part of the system, that part (the store in this case) should be mocked. If we follow this advice, my test becomes like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d642a8ae0ed666f87b689a2e7fac965c/href">https://medium.com/media/d642a8ae0ed666f87b689a2e7fac965c/href</a></iframe><p>This one passes alright. Other tests might need you to also mock $store.commit(), but if you are comfortable with mocking this is no big deal.</p><p>Are the purists right? My opinion is no. If you have function A that calls function B, you aren’t going to mock B whenever you’re testing A. The purpose of writing tests is to develop faster and cheaper, and this means avoiding mocking wherever possible. In fact, as a Django developer, I’m delighted at the fact that Django connects to the RDBMS and creates a test database at the beginning of each test run, relieving me of the duty of having to mock the RDBMS, although the RDBMS is a textbook case of a big external system that needs to be mocked. Django only takes a few seconds to create the database, and the savings are much more significant. The vuex store is not a big external system; it’s a small internal system. I don’t believe it should be mocked on every occasion.</p><p>Brandon’s solution wouldn’t work without a fight though, and since I’m a Nuxt beginner, I settled with mocking, waiting to get more experience before retrying. I’ll be writing more of these posts as I learn; think of them as a kind of personal notes that I’m writing in public.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0bfd69adc98" width="1" height="1" alt=""><hr><p><a href="https://medium.com/testing-nuxt-applications/testing-functionality-that-involves-the-store-c0bfd69adc98">Testing functionality that involves the store</a> was originally published in <a href="https://medium.com/testing-nuxt-applications">Testing nuxt applications</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>