cypress and instanbul

This commit is contained in:
2021-09-14 14:45:47 +02:00
parent 786fd12f34
commit f9fe8fd735
15 changed files with 618 additions and 26 deletions

View File

@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}

View File

@@ -0,0 +1,22 @@
/// <reference types="cypress" />
const { before } = require("../../support")
// @ts-check
describe("renz einfo auth", () => {
beforeEach(() => {
before()
})
afterEach(function () {
if (this.currentTest.state === "failed") {
cy.setCookie("shouldSkip", "true")
Cypress.runner.stop()
}
})
it("can visit homepage", () => {
cy.visit("/")
})
})

124
cypress/plugins/index.js Normal file
View File

@@ -0,0 +1,124 @@
/// <reference types="cypress" />
// @ts-check
const { default: axios } = require("axios")
const { MongoClient } = require("mongodb")
const fs = require("fs")
const util = require("util")
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************
// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)
function logObj(o) {
console.log(util.inspect(o, false, null, true /* enable colors */))
}
/**
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
module.exports = (on, config) => {
require("cypress-terminal-report/src/installLogsPrinter")(on, {
printLogsToConsole: "always",
includeSuccessfulHookLogs: true,
})
const registerCodeCoverageTask = require("@cypress/code-coverage/task")
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on("before:run", async (details) => {
/*
if (details.specs && details.browser) {
// details.specs and details.browser will be undefined in interactive mode
console.log(
"Running",
details.specs.length,
"specs in",
details.browser.name
)
}*/
if (config.env.CI) {
// is continous integration test run, so initialize database
console.log("CI RUN: init database")
const wmbasicDbPrefix = config.env.wmbasicDbPrefix
const projectNamespace = config.env.projectApiNamespace
// drop database from tests before
console.log(" - connecting to mongodb: " + config.env.mongodbUri)
const dbClient = new MongoClient(config.env.mongodbUri)
await dbClient.connect()
console.log(" - removing project from wmbasic db")
let db = dbClient.db(wmbasicDbPrefix)
db.collection("project").deleteMany({ namespace: projectNamespace })
const dbName = wmbasicDbPrefix + "_" + projectNamespace
console.log(" - dropping database: " + dbName)
db = dbClient.db(dbName)
await db.dropDatabase()
// login
const apiUrl = config.env.wmbasicApiUrl
const l = await axios.post(apiUrl + "/login", {
username: config.env.wmbasicUsername,
password: config.env.wmbasicPassword,
})
const wmbasicToken = l.data.token
// create project
console.log(" - creating wmbasic project: " + projectNamespace)
let project
try {
const p = await axios.post(
apiUrl + "/project",
{
configFile: config.env.projectApiConfig,
name: projectNamespace,
namespace: projectNamespace,
description: projectNamespace,
},
{
headers: {
"X-Auth-Token": wmbasicToken,
},
}
)
project = p.data
} catch (e) {
logObj(e)
throw e
}
console.log(" - checking project status: " + project.id)
try {
const p = await axios.get(apiUrl + "/project/" + project.id, {
headers: {
"X-Auth-Token": wmbasicToken,
},
})
if (!p.data.api.isOnline) {
throw p.data
}
} catch (e) {
logObj(e)
throw e
}
// TODO fill wmbasic database with test data
}
})
return registerCodeCoverageTask(on, config)
}

View File

@@ -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) => { ... })

41
cypress/support/index.js Normal file
View File

@@ -0,0 +1,41 @@
// ***********************************************************
// This example support/index.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"
import installLogsCollector from "cypress-terminal-report/src/installLogsCollector"
installLogsCollector()
import "@cypress/code-coverage/support"
// Alternatively you can use CommonJS syntax:
// require('./commands')
export const before = () => {
if (Cypress.browser.isHeaded) {
cy.clearCookie("shouldSkip")
} else {
cy.getCookie("shouldSkip").then((cookie) => {
if (
cookie &&
typeof cookie === "object" &&
cookie.value === "true"
) {
Cypress.runner.stop()
}
})
}
}