柏树 - 可以在测试中分离测试时在测试中使用自定义任务

发布于 2025-02-10 00:06:05 字数 3869 浏览 0 评论 0原文

我尝试在测试项目之外的插件文件中添加了一个新的自定义任务。 我还将其编译并在config.json中配置了他的路径。 该文件中的所有其他插件都可以正常运行。

我在执行过程中从柏树中获得的错误是 - >

"value": "CypressError: `cy.task('queryDb')` failed with the following error:\n\nThe task 'queryDb' was not handled in the plugins file. The following tasks are registered: log\n\nFix this in your plugins file here:\n./../testilize/cypress/plugins/index.ts\n    at ...

配置文件扩展到测试项目之外的基本配置文件 - >

{
  "extends": "./../testilize/cypress.json",
  "baseUrl": "https://www.blabla.com/",
  "env": {
    "client": "https://www.blabla.com/",
    "server": "https://www.blabla.com/"
  },
  "pluginsFile": "./../testilize/cypress/plugins/index.ts",
  "supportFile": "./../testilize/cypress/support/index.js",
  "fixturesFolder": "e2e-tests/fixtures",
  "integrationFolder": "e2e-tests/test-files"
}

插件文件 - >

// cypress/plugins/index.ts
/// <reference types="cypress" />

/**
 * @type {Cypress.PluginConfig}
 */

const preprocess = require('./preprocess');
const deepmerge = require('deepmerge')
const path = require('path');
require('dotenv').config({ path: './../testilize/.env' , override: true })
import { my_connection } from '../support/db-handlers/connections';

function queryTestDb(query, config) {
    // start connection to db
    my_connection.connect();
    // exec query + disconnect to db as a Promise
    return new Promise((resolve, reject) => {
        my_connection.query(query, (error, results) => {
            if (error) reject(error);
            else {
                my_connection.end();
                // console.log(results)
                return resolve(results);
            }
        });
    });
}

module.exports = (on, config) => {
    require('cypress-log-to-output').install(on)

    on('task', {
        log (message) {
            console.log(message)
            return true
        }
    })

    const configJson = require(config.configFile)
    if (configJson.extends) {
        const baseConfigFilename = path.join(config.projectRoot, configJson.extends)
        const baseConfig = require(baseConfigFilename)
        console.log('merging %s with %s', baseConfigFilename, config.configFile)
        configJson.env.my_db_name = process.env.my_DB_NAME;
        configJson.env.my_db_host = process.env.my_DB_HOST;
        configJson.env.my_db_user = process.env.my_DB_USER;
        configJson.env.my_db_password = process.env.my_DB_PASSWORD;
        configJson.env.my_db_port = process.env.my_DB_PORT;

        return deepmerge(baseConfig, configJson);
    }

    on("file:preprocessor", preprocess);

    on('before:browser:launch', (browser , launchOptions) => {
        if (browser.name === 'chrome' && browser.isHeadless) {
            launchOptions.args.push('--disable-gpu', '--no-sandbox', '--disable-dev-shm-usage', '--window-size=1920,1080');
            return launchOptions
        }
    })

    // Usage: cy.task('queryDb', query)
    on('task', {
        'queryDb': query => {
            return queryTestDb(query, config);
        }
    });

    return configJson
}

测试文件 - &GT;

/// <reference types="./../../../testilize/node_modules/cypress" />

let allProjectIDs: any = [];


describe('Tests', () => {

  it('send graphQL request for internal api', () => {
    cy.task(
        'queryDb',
        `SELECT project_id FROM table_name LIMIT 100;`
    ).then(res => {
      console.log(res);
      allProjectIDs.push(res);
      console.log(allProjectIDs);
    });
    
  });

});

stack ::

打字稿4.6

节点14x

柏树9.6

I've tried to added a new custom task to my plugins file that located outside the tested project.
I've compiled it and configured his path in the config.json as well.
All the other plugins from this file it works ok.

The error I got from Cypress during the execution is ->

"value": "CypressError: `cy.task('queryDb')` failed with the following error:\n\nThe task 'queryDb' was not handled in the plugins file. The following tasks are registered: log\n\nFix this in your plugins file here:\n./../testilize/cypress/plugins/index.ts\n    at ...

enter image description here

The configuration file is extend to the base config file outside the tested project ->

{
  "extends": "./../testilize/cypress.json",
  "baseUrl": "https://www.blabla.com/",
  "env": {
    "client": "https://www.blabla.com/",
    "server": "https://www.blabla.com/"
  },
  "pluginsFile": "./../testilize/cypress/plugins/index.ts",
  "supportFile": "./../testilize/cypress/support/index.js",
  "fixturesFolder": "e2e-tests/fixtures",
  "integrationFolder": "e2e-tests/test-files"
}

plugins file ->

// cypress/plugins/index.ts
/// <reference types="cypress" />

/**
 * @type {Cypress.PluginConfig}
 */

const preprocess = require('./preprocess');
const deepmerge = require('deepmerge')
const path = require('path');
require('dotenv').config({ path: './../testilize/.env' , override: true })
import { my_connection } from '../support/db-handlers/connections';

function queryTestDb(query, config) {
    // start connection to db
    my_connection.connect();
    // exec query + disconnect to db as a Promise
    return new Promise((resolve, reject) => {
        my_connection.query(query, (error, results) => {
            if (error) reject(error);
            else {
                my_connection.end();
                // console.log(results)
                return resolve(results);
            }
        });
    });
}

module.exports = (on, config) => {
    require('cypress-log-to-output').install(on)

    on('task', {
        log (message) {
            console.log(message)
            return true
        }
    })

    const configJson = require(config.configFile)
    if (configJson.extends) {
        const baseConfigFilename = path.join(config.projectRoot, configJson.extends)
        const baseConfig = require(baseConfigFilename)
        console.log('merging %s with %s', baseConfigFilename, config.configFile)
        configJson.env.my_db_name = process.env.my_DB_NAME;
        configJson.env.my_db_host = process.env.my_DB_HOST;
        configJson.env.my_db_user = process.env.my_DB_USER;
        configJson.env.my_db_password = process.env.my_DB_PASSWORD;
        configJson.env.my_db_port = process.env.my_DB_PORT;

        return deepmerge(baseConfig, configJson);
    }

    on("file:preprocessor", preprocess);

    on('before:browser:launch', (browser , launchOptions) => {
        if (browser.name === 'chrome' && browser.isHeadless) {
            launchOptions.args.push('--disable-gpu', '--no-sandbox', '--disable-dev-shm-usage', '--window-size=1920,1080');
            return launchOptions
        }
    })

    // Usage: cy.task('queryDb', query)
    on('task', {
        'queryDb': query => {
            return queryTestDb(query, config);
        }
    });

    return configJson
}

Test file ->

/// <reference types="./../../../testilize/node_modules/cypress" />

let allProjectIDs: any = [];


describe('Tests', () => {

  it('send graphQL request for internal api', () => {
    cy.task(
        'queryDb',
        `SELECT project_id FROM table_name LIMIT 100;`
    ).then(res => {
      console.log(res);
      allProjectIDs.push(res);
      console.log(allProjectIDs);
    });
    
  });

});

Stack::

TypeScript 4.6

Node 14x

Cypress 9.6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2025-02-17 00:06:05

这可能是因为您在插件中有两个('task',{部分。

第一个看起来像柏树提供的默认值,请尝试将其评论。

It might be because you have two on('task', { sections in plugins.

The first one looks like the default supplied by Cypress, try commenting it out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文