Skip to content

Commit

Permalink
feat(tests): Init cypress framework
Browse files Browse the repository at this point in the history
Cypress testing base setup including
- eslint support
- configuration for e2e, component and unit tests
- ignoring relevant files
- basic e2e test home_page.cy.js
- new webpack config for component tests
- remove of duplicate line from .gitignore

Component tests should be written in the component e.g.
'MyComponent.cy.js' for 'MyComponent.vue'

Unit tests for js source files should be in a separate
'__tests__' folder and be named similar e.g.
'__tests__/utils.cy.js' for 'utils.js'

e2e tests should go to 'cypress/e2e/test-name.cy.js'
  • Loading branch information
TheGreatRefrigerator committed Dec 7, 2022
1 parent 9865f2c commit 10631c1
Show file tree
Hide file tree
Showing 12 changed files with 1,725 additions and 549 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = {
// https://github.com/standard/standard/blob/master/docs/RULES-en.md
extends: [
"eslint:recommended",
"plugin:vue/base"
"plugin:vue/base",
"plugin:cypress/recommended"
],
// add your custom rules here
rules: {
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ selenium-server.log
!/static/css/.gitkeep
!/static/fonts/.gitkeep
!/static/img/.gitkeep
!/static/img/.gitkeep

# leaflet helpers

/images

# cypress tests
/cypress/videos/
/cypress/screenshots/
3 changes: 2 additions & 1 deletion build/webpack.base.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ module.exports = {
extensions: ['.vue', '.js', '.json'],
fallback: {
timers: require.resolve('timers-browserify'),
stream: require.resolve('stream-browserify')
stream: require.resolve('stream-browserify'),
path: require.resolve('path-browserify')
}
},
module: {
Expand Down
77 changes: 77 additions & 0 deletions build/webpack.cypress.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict'
const { resolveRoot, styleLoaders} = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')
const webpack = require('webpack')


const createLintingRule = () => ({
test: /\.(js|vue)$/,
loader: 'eslint-loader',
enforce: 'pre',
include: [resolveRoot('src'), resolveRoot('test')],
exclude: [resolveRoot('tests/integration/mockups')],
options: {
formatter: require('eslint-friendly-formatter'),
emitWarning: !config.dev.showEslintErrorsInOverlay
}
})

module.exports = {
plugins: [
// fix "process is not defined" error:
new webpack.ProvidePlugin({
process: 'process/browser',
})
],
context: resolveRoot(),
entry: {
app: './src/main.js'
},
output: {
path: config.build.assetsRoot,
filename: '[name].js',
assetModuleFilename: '[base]',
publicPath: '/'
},
resolve: {
alias: {
'@': resolveRoot('src'),
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['.vue', '.js', '.json'],
fallback: {
timers: require.resolve('timers-browserify'),
stream: require.resolve('stream-browserify')
}
},
module: {
rules: [
...(config.dev.useEslint ? [createLintingRule()] : []),
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [
resolveRoot('src/'),
resolveRoot('test/'),
resolveRoot('node_modules/webpack-dev-server/client/')
],
options: {
plugins: [ '@babel/plugin-proposal-object-rest-spread' ]
}
},{
test: /\.(woff|woff2|eot|ttf|otf|png|svg|jpg|jpeg|gif|ico)$/i,
type: 'asset'
},{
test: /\.(geojson|kml|gpx|txt)$/i,
type: 'asset/source',
},
...styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
]
}
}
37 changes: 37 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { defineConfig } = require("cypress");
const config = require('./config');
const webpackPreprocessor = require('@cypress/webpack-preprocessor')

module.exports = defineConfig({
projectId: '2npvgh',
video: false,
e2e: {
specPattern: [
"cypress/e2e/*.{cy,spec}.{js,jsx,ts,tsx}",
"src/**/__tests__/*.{cy,spec}.{js,ts,jsx,tsx}"
],
baseUrl: 'http://localhost:8080',
setupNodeEvents(on, config) {
// implement node event listeners here
const options = {
// send in the options from your webpack.config.js, so it works the same
// as your app's code
webpackOptions: require('./build/webpack.base.conf'),
watchOptions: {},
}

on('file:preprocessor', webpackPreprocessor(options))
},
},
component: {
devServer: {
framework: 'vue',
bundler: 'webpack',
// optionally pass in webpack config
webpackConfig: require('./build/webpack.cypress.conf'),
},
specPattern: 'src/**/*.cy.{js,jsx,ts,tsx}',
excludeSpecPattern: "src/**/__tests__/*.{cy,spec}.{js,ts,jsx,tsx}"
},

});
12 changes: 12 additions & 0 deletions cypress/e2e/home_page.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference types="cypress" />
import defaultMapSettings from '../../src/config/default-map-settings'
import appConfig from '../../src/config/app-config'

describe('Ors map client', () => {
it('successfully loads', () => {
cy.visit('/')
let coords = defaultMapSettings.mapCenter
cy.url().should('contain', coords.lat).should('contain', coords.lng).
should('contain', appConfig.initialZoomLevel.toString())
})
})
25 changes: 25 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
12 changes: 12 additions & 0 deletions cypress/support/component-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>
</head>
<body>
<div data-cy-root></div>
</body>
</html>
27 changes: 27 additions & 0 deletions cypress/support/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ***********************************************************
// This example support/component.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')

import { mount } from 'cypress/vue2'

Cypress.Commands.add('mount', mount)

// Example use:
// cy.mount(MyComponent)
20 changes: 20 additions & 0 deletions cypress/support/e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/e2e.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
Loading

0 comments on commit 10631c1

Please sign in to comment.