Giter VIP home page Giter VIP logo

cypress-nuxt's Introduction

Cypress-nuxt

Utilities for using Cypress with Nuxt Apps.

Current features:

  • Expose a plugin for unit testing components (automatically generates the webpack config)

Potential future features:

  • Provide commands for interacting with Nuxt
  • Setup nuxt URL automatically
  • Have other ideas? Make an issue with your idea, I'd love to hear them!

Why though?

First off, cypress is awesome. Second, there's a bit of wiring up needed when using nuxt. Although this was made way easier with Nuxt exposing the webpack config, we still need to tweak the config a tiny bit to get it to work with cypress.

There also might be other utilities needed in the future to make cypress even easier to use with nuxt, so I thought a NPM package would be good. Maybe exposing the nuxt config to cypress, or automatically setting the base URL? Have Ideas? submit some enhancement requests!

Getting started

Nuxt version

First, this requires Nuxt >2.12. This module doesn't rely on nuxt itself so you need to install it (although it's a nuxt app... if you don't have it installed what are you doing?). If you're not on > 2.12 upgrade nuxt

1. Install

npm install -D cypress-nuxt cypress-vue-unit-test

or with yarn

yarn add -D cypress-nuxt cypress-vue-unit-test

2. Add the cypress plugin

With Async/Await

You might want to check that your node.js version supports async await. If it doesn't... well first upgrade. :p but if not use promises below.

cypress/plugins/index.js

const cypressNuxt = require("cypress-nuxt");

module.exports = async (on, config) => { // make sure to include "async"!
  on("file:preprocessor", await cypressNuxt.plugin()); // make sure to include "await"!

  // other plugins...
  return config;
};

or With Promises

cypress/plugins/index.js

const cypressNuxt = require("cypress-nuxt");

module.exports = function (on, config) {
  return cypressNuxt.plugin().then(function (webpackPreProcessor) {
    on("file:preprocessor", webpackPreProcessor);

    // other plugins...
    return config;
  })
  
};

2.b @nuxt/typescript-runtime

@nuxt/typescript-runtime allows writing your nuxt.config file in typescript, this means that cypress-nuxt (and anything else reading your nuxt.config.ts needs to compile it before it can work with it.

in your cypress/plugins/index.js add a ts-node register call before getting the webpack config.

require("ts-node").register({
    compilerOptions: {
        // force to compile to what node.js expects
        target: "es5",
        module: "commonjs" // node expects commonjs format
    }
});

cypressNuxt.plugin({})
  .then((webpackConfig) => {
      console.log(webpackConfig)
  })

Or, With Async/Await

I like to pull it out to a function so it's easy to await cypress/plugins/index.js

const cypressNuxt = require("cypress-nuxt");

module.exports = async (on, config) => { // make sure to include "async"!
  on("file:preprocessor", await filePreprocessor()); // make sure to include "await"!

  // other plugins...
  return config;
};

function filePreprocessor() {
  require("ts-node").register({
    compilerOptions: {
      target: "es5",
      module: "commonjs" // node expects commonjs format
    }
  });
  // return the promise to return the webpack config
  return cypressNuxt.plugin({})
}

3. Setup Cypress-vue-unit-test

For cypress-vue-unit-test < v2 see oldTestOrganization.md

Follow the cypress-vue-unit-test documentation to get setup

4. Write a test

Javascript

~/components/Logo.spec.js

import { createWrapper } from "@vue/test-utils";
import { mountCallback } from "cypress-vue-unit-test";
import Logo from "~/components/Logo.vue";

describe("Logo", () => {
  beforeEach(mountCallback(Logo));

  it("should initialize", () => {
    cy.wrap(Cypress.vue)
      .should("not.be.undefined")
      .get(".Triangle")
      .should("have.length", 4);

    cy.wrap(createWrapper(Cypress.vue)).should(cmp => cmp.isVueInstance());
  });
});

Typescript Tests (if you have @nuxt/typescript-build enabled)

Just rename your spec file to .ts: ~/components/Logo.spec.ts

Options

the plugin function takes an options object. See the type definitions for LoadOptions for valid options.

rootDir

Set the root dir to search for the nuxt.config.[js|ts] This is useful if you don't run cypress from the directory that contains your nuxt config file.

to resolve app/client/nuxt.config.js from app/e2e/cypress/plugin.js

  return cypressNuxt.plugin({
    loadOptions: {
      rootDir: path.join(__dirname, "../../client")
    }
  })

"for"

This option tells nuxt what version of the webpack config you want. Leaving this undefined seems to work fine.

cypress-nuxt's People

Contributors

dependabot[bot] avatar nickbolles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cypress-nuxt's Issues

Feat: Watch nuxt.config

Not sure if nuxt exposes this, and if we can restart cypress when it changes, but it'd be nice. It's tripped me up a few times today.

How to detect that the server has finished initializing before running the tests

Good evening,

I started an application with Nuxt. I set up Cypress to perform e2e tests.

The problem is that to run my tests, the server needs to be launched, but since "nuxt-ts" launches the server but the process remains active, so I am not able to detect when the server has finished initializing.

Any idea how I can work around this problem?
I haven't seen a Cypress module for Nuxt, but I know that on Vue 2/3, it works fine (https://cli.vuejs.org/core-plugins/e2e-cypress.html)

Thank you

Does cypress need to be in the same package as nuxt

Hi, really cool that you made this.

I'm trying to use this in my project, we are using yarn workspaces.

Do I understand this right, that cypress-nuxt and cypress itself should share the package.json with the nuxt project, otherwise we would have to carry over all the dependencies into a separated cypress project.

Here is an (outdated) example of our file hierarchy: https://github.com/JoeSchr/graphile-starter/tree/master/%40app

@app/server => nuxt
@app/e2e => cypress

Cannot visit home page "/"

Hey, thanks for developing this package!

Can you please help me out to figure out the problem I'm having?

I did exactly the same steps but I'm getting always this error:

cy.visit() failed trying to load:
http://localhost:3000/
The response we received from your web server was:

500: Server Error

Obviously, I can open the homepage, but the test always fails. ( I spin up nuxt before running cypress open )

cypress.json:

{
    "baseUrl": "http://localhost:3000"
}

cypress/integration/test.spec.js:

describe('The Home Page', () => {
  it('should visit the home page', () => {
    cy.visit('/')
  })
})

cypress/plugins/index.js:

const cypressNuxt = require('cypress-nuxt')
module.exports = async (on, config) => {
  on('file:preprocessor', await cypressNuxt.plugin())
  return config
}

Allway getting this as the result:
Screenshot 2020-11-14 at 16 03 39

I can open the URL in a different tab and the home page opens correctly.

devDependencies:

    "cypress": "^5.6.0",
    "cypress-nuxt": "^1.3.1",

If I will start without nuxt cypress open, I will get this:
Screenshot 2020-11-14 at 16 12 23
Which means that actually, nuxt is running correctly on localhost:3000 on my previous problem, but why it's not reaching the home page ๐Ÿค”

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.