typescript Cypress SQL数据库测试-如何设置Cypress.config文件?

lvjbypge  于 2023-03-19  发布在  TypeScript
关注(0)|答案(1)|浏览(125)

我想测试使用SQL的数据库。我有SqlWorkbench,我已经添加了MySQL在我的包文件。
我在尝试运行Cypress时遇到此错误SyntaxError:意外标记“export”
cypress.config.ts中的代码。(我的本地文件在defineConfig中有数据)

import { defineConfig } from "cypress";
import mysql from 'mysql';

function queryTestDB(query,config) {
  const connection = mysql.createConnection(config.env.db)

  connection.connect()
  return new Promise((resolve, reject) => {
    connection.query(query, (error, results) => {
      if (error) {
        return reject(error)
      }
      connection.end()
      return resolve(results)
    })
  })
}

export default defineConfig({
  projectId: '...',
  env: {
    db: {
      host: '...',
      user: '...',
      password: '...',
      database: '...',
    }
  },
  e2e: {
    baseUrl: 'http://localhost...',
    setupNodeEvents(on, config) {
      on('task', {
        queryDB(query) {
          return queryTestDB(query,config)
        }
      })
    }
  },
});
k3bvogb1

k3bvogb11#

import { defineConfig } from 'cypress';
const mysql = require('mysql2'); 

function querytestDB(query: string, config) {
  const connection = mysql.createConnection(config.env.db);

  connection.connect()

  return new Promise((resolve, reject) => {
    connection.query(query, (error, results) => {
      if (error) {
        return reject(error)
      }

      connection.end()
      return resolve(results)
    });
  });
}

export default defineConfig({
  env : {
    db: {
      host: "127.0.0.1",
      user: "...",
      password: "...",
      database: "..."
    }
  },
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        queryDB: query=> {
          return querytestDB(query, config)},
      });
    },
    chromeWebSecurity: false,
  },
});

这在cypress.config.ts文件中:)

相关问题