Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Tips

    Becoming a Technical Lead: Working on Your Leadership Skills

    Remote Job

    Ultimate Onboarding Checklist for Web Developers (Bonus: Onboarding Checklist for Freelancers)

    Interview

    Best Resources for Preparing for Your Technical Interview: Books and Online Platforms

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Sunday, September 28
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / Vue / Writing end-to-end tests for Nuxt apps using jsdom and AVA
    JavaScript

    Writing end-to-end tests for Nuxt apps using jsdom and AVA

    EmmanuelBy EmmanuelJanuary 21, 2021Updated:January 22, 2021No Comments8 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Writing end-to-end tests for Nuxt apps using jsdom and AVA
    Writing end-to-end tests for Nuxt Apps using jsdom and AVA
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Writing end-to-end tests for Nuxt Apps using jsdom and AVA
    Writing end-to-end tests for Nuxt Apps using jsdom and AVA

    Introduction

    Testing is a very important aspect of web application development. It can help reveal bugs before they appear and also instill confidence in your web application, especially during its launch. In this article, we will explore jsdom and AVA testing frameworks while writing tests for the Nuxt application.

    Prerequisite

    • Working knowledge of Vue.js
    • Prior experience working with Nuxt.js
    • Node.js and npm

    Introducing end-to-end test

    End-to-end testing is a testing technique where the different parts of the application are often tested together hence, it is often labeled as integration tests. such tests are written to mimic a user’s movement throughout the application.

    Introducing jsdom

    The server side has raw HTML with no access to the DOM hence there isn’t much you can do as regards DOM manipulation. With jsdom, the DOM can be accessed inside Node.js.

    According to jsdom documentation: jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards, for use with Node.js.

    The latest versions of jsdom require Node.js v10 or newer. (Versions of jsdom below v16 still work with previous Node.js versions, but are unsupported.)

    jsdom is pre-configured by nuxtjs and in order to use it, we need to install jsdom package then convert the HTML to DOM fragment using nuxt.renderAndGetWindow and write our test.

    Introducing AVA

    AVA is a test runner for Node.js with a simple test syntax. It is minimal and fast, runs tests concurrently, Enforces writing atomic tests, and has typescript support.

    We will build a demo nuxt app so that we can proceed with writing its corresponding test suit. Our demo app will allow users to log in and access their dashboard.

    In this tutorial, we would use create-nuxt-app to scaffold our nuxtjs project so let’s get started. Open your terminal and run this command:

    npx create-nuxt-app nuxt-ava

    If you are using yarn run this command:

    yarn create nuxt-app nuxt-ava

    Next, you will have a prompt with a list of options that configures your application for development.

    Your configuration should be similar to this:

    Nuxt project configuration photo
    Nuxt project configuration photo

    Once that is done, we’ll run the following command in our terminal:

    $ cd nuxt-ava
    $ npm install
    $ npm run dev

    Your app should now be running on http://localhost:3000 as follows:

    Nuxt initial scaffold photo
    Nuxt initial scaffold photo

    In the pages directory, create a login.vue file then add the following code snippet to it:

    //pages/login.vue
    <template>
      <div>
        <h1>Login</h1>
        <form @submit.prevent="login">
          <p>
            Username:
            <input
              v-model="username"
              class="username"
              type="text"
              name="username"
            />
          </p>
          <p>
            Password:
            <input
              v-model="password"
              class="password"
              type="password"
              name="password"
            />
          </p>
          <button class="button" type="submit">Login</button>
        </form>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          username: "",
          password: "",
        };
      },
      methods: {
        login() {
          if (this.username && this.password) {
            this.$router.push("/dashboard");
            this.formUsername = "";
            this.formPassword = "";
          } else {
            console.log("Invalid form entry")
          }
        },
      },
    };
    </script>
    
    <style>
    </style>

    Here, we create a basic form with its input field values binds to the template through the v-model attribute. Also, we create a login method which redirects users to the dashboard if all the input values are provided otherwise, an error message is logged to the console.

    Read More:  Libero Enim Sedfaucibus Turpis Magna Fermentum Justoeget

    In the pages directory, create a dashboard.vue file then add the following code snippet to it:

    <template>
      <div>
        <h1>Dashboard</h1>
        <p class="green">{{ message }}</p>
        </div>
    </template>
    
    <script>
    export default {
      async asyncData (context) {
        return {
          message: 'Welcome to your dashboard!'
        }
      }
    }
    </script>
    
    <style>
    .green {
      color: green;
    }
    </style>

    The snippet above dynamically renders the content on the template. the asyncData hook ensures that Nuxt renders message in the template before navigating to the next page or display the error page).

    Update pages/index.vue with the following code snippet:

    <template>
      <div>
        <h1>Welcome to youChores</h1>
        <p>We are here to serve you</p>
      </div>
    </template>
    
    <script>
    export default {
      //
    }
    </script>
    
    <style>
    
    </style>

    Setting up the test environment

    We will use the following steps to setup our test environment:

    step1

    Run the command below to install ava and jsdom as dev dependencies:

    $ npm install ava --save-dev
    $ npm install jsdom --save-dev

    step2

    Update package.json file with the following AVA configuration:

    // package.json
    {
     "scripts": {
     "test": "ava --verbose",
     "test:watch": "ava --watch"
     },
     "ava": {
     "require": [
     "./setup.js",
     "@babel/polyfill"
     ],
     "files": [
     "test/**/*"
     ]
     }
    }

    step3

    Create a setup.js file in the root directory, then add the following code:

    // setup.js
    require('@babel/register')({
     babelrc: false,
     presets: ['@babel/preset-env']
    })

    step 4

    Since we will likely use the es6 features in our test, we need to install the Babel package in the app:

    $ npm install @babel/polyfill
    $ npm install @babel/core --save-dev
    $ npm install @babel/preset-env --save-dev
    $ npm install @babel/register --save-dev

    Building the test suite

    Now, we are done with the configurations and we are ready to write our test. We will test our Nuxt app with the following user flow:

    • User access the home page
    • User access the login page
    • User enters his login details
    • User is taken to his dashboard

    In the tests directory, create tests.js and add the following test suite:

    // test/tests.js
    import test from 'ava'
    
    import { Nuxt, Builder } from 'nuxt'
    import { resolve } from 'path'
    
    // We keep a reference to Nuxt so we can close
    // the server at the end of the test
    let nuxt = null
    
    // Init Nuxt.js and start listening on localhost:5000
    test.before('Init Nuxt.js', async t => {
      const rootDir = resolve(__dirname, '..')
      let config = {}
      try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {}
      config.rootDir = rootDir // project folder
      config.dev = false // production build
      config.mode = 'universal' // Isomorphic application
      nuxt = new Nuxt(config)
      await new Builder(nuxt).build()
      nuxt.listen(5000, 'localhost')
    })

    The above code snippet initializes nuxt with the required configurations for our tests, setup a test server, and starts listening on localhost:5000.

    User access the home page

    Add the code snippet to test/tests.js below the previous code snippet.

    test.serial('Route / exits and renders correct HTML', async (t) => {
      let context = {}
      const { html } = await nuxt.renderRoute('/', context)
      t.true(html.includes('<h1>Welcome to youChores</h1>'))
      t.true(html.includes('<p>We are here to serve you</p>'))
      
    })

    The test suit above checks for the existence of the home route and also check if its content is properly rendered.

    Read More:  Strategies for Keeping Projects on Track and Meeting Deadlines

    N/B: The test.serial method ensures that the test cases execute orderly from top to bottom.

    User access the login page

    Add the code snippet to test/tests.js below the previous code snippet.

    test.serial('Route /login exits and renders correct HTML', async (t) => {
    
      const window = await nuxt.renderAndGetWindow('http://localhost:5000/login')
      const username = window.document.querySelector('.username')
      const password = window.document.querySelector('.password')
      const button = window.document.querySelector('.button')
    
      t.not(username, null)
      t.not(password, null)
      t.not(button, null)
    })

    The test suit above checks for the existence of the /login route and also check if its content is properly rendered. The test suit above performs DOM manipulation on the server-side with jsdom.

    Confirm the default values of the input fields

    Add the code snippet to test/tests.js below the previous code snippet.

    test.serial('Route /login exits and set correct default data', async (t) => {
      const window = await nuxt.renderAndGetWindow('http://localhost:5000/login')
      const username = window.document.querySelector('.username')
      const password = window.document.querySelector('.password')
    
      t.is(username.value, '')
      t.is(password.value, '')
      
    })

    The test suit above checks for the existence of the /login route and also check if its default values are set properly.

    A user enters his login details

    Add the code snippet to test/tests.js below the previous code snippet.

    test.serial('Route /login exits and should update input values then click the login button', async (t) => {
      const window = await nuxt.renderAndGetWindow('http://localhost:5000/login')
      const username = window.document.querySelector('.username')
      const password = window.document.querySelector('.password')
      const button = window.document.querySelector('.button')
      
      t.pass(username.value, 'admin')
      t.pass(password.value, 'password')
      button.click()
    })

    The test suit above checks for the existence of the /login route and also ensures the user has provided values for the input fields then clicks the login button.

    User is taken to his dashboard

    Add the code snippet to test/tests.js below the previous code snippet.

    test.serial("Route /dashboard exits and renders correct HTML", async (t) => {
      let context = {};
      const { html } = await nuxt.renderRoute("/dashboard", context);
      t.true(html.includes("<h1>Dashboard</h1>"));
      t.true(html.includes('<p class="green">Welcome to your dashboard!</p>'));
    });

    The test suit above checks for the existence of the /dashboard route and also ensure that its content is properly rendered.

    Running the test suite

    Open your terminal and run the test suite with the following command:

    npm run test

    If all your test suite passes, you should get the following result:

    Successful test suite photo
    Successful test suite photo

    Conclusion

    Finally, we have an idea of what an end-to-end test is and how to write end-to-end tests for Nuxt apps using jsdom and AVA. I hope you have learned a great deal from this tutorial. Do reach out in the comment section below if you have any questions or suggestions. Here is the Github repo for the demo Nuxt application.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Emmanuel

      Related Posts

      Streamlining Resource Allocation for Enhanced Project Success

      December 18, 2024

      Conducting Effective Post-Project Evaluations: A Guide

      December 16, 2024

      Strategies for Keeping Projects on Track and Meeting Deadlines

      December 10, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      Beginners August 29, 2019

      Developer Guide to GDPR

      It’s been over a year since the GDPR came into full effect, thus panic associated with the changes has already subsided; nevertheless, the GDPR compliance still raises a handful of issues, which we’ll subsequently cover in this article.

      Роли внутри проектов

      March 18, 2016

      Women in Tech Series: Pioneers of ‘Coding for Everyone’ Movement

      February 7, 2019

      Interview with Alexander

      September 13, 2017

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Optimizing Database Interactions in Python: SQLAlchemy Best Practices

      Programming March 27, 2023

      Why Startups Fail? Part 1

      Startups October 5, 2016

      Best Practices for Writing Clean and Maintainable Code

      Programming December 9, 2024

      Уроки React. Урок 11. Pt.1.

      Programming October 31, 2016

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      Interview

      Interview with Leonid

      JavaScript

      Basic Principles and Rules of Our Team

      Events

      Last Chance to Get Your Running Remote Early-Bird Ticket!

      Most Popular

      Handling Side Effects in Redux: Redux-Saga

      JavaScript

      How Deep Work Can Change Your Freelance Life

      Remote Job

      JavaScript Design Patterns in Action

      JavaScript
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.