forked from cms/tibi-svelte-starter
cypress and instanbul
This commit is contained in:
124
cypress/plugins/index.js
Normal file
124
cypress/plugins/index.js
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user