最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Cypress - Best way to Seed Database - Stack Overflow

programmeradmin2浏览0评论

Currently I have a Command called "seedDB". This Command currently clears down the DB and then runs a number of SQL files in to re-seed the DB.

Cypress.Commands.add('seedDB', (customerFlag) => {

    // Set Up Variables
    deleteDataSQLFile = 'cypress/database/delete_data.sql';
    sqlFileLocation = 'cypress/database/reset/'

    // Delete all Data from DB
    cy.readFile(deleteDataSQLFile)
        .then(($file) => {
            cy.task("runSQLDB", {
                dbConfig: Cypress.config('dbConfig'),
                sqlQuery: $file
            })
            console.log(deleteDataSQLFile);
    })

    // Run all SQL files within Diretory
    cy.task('countFiles', sqlFileLocation).then((fileArray) => { 
        fileArray.forEach(fileArray => {
            cy.readFile(sqlFileLocation + fileArray).then(($file) => {
                cy.task("runSQLDB", {
                    dbConfig: Cypress.config('dbConfig'),
                    sqlQuery: $file            
                })
                console.log($file);
            })                
        });
    })
})

I am running this on a "Before" on the main test.cy.js. I seem to have two issues.

  • there is quite a few SQL files needed to reseed the DB and when cypress is running it can timeout and fails. This means running in Jenkins does not work
  • would like to disconnect it from the main test files and run before cypress has loaded the test file but not sure how go about this

Can anyone help with these two points or suggest a better way

Currently I have a Command called "seedDB". This Command currently clears down the DB and then runs a number of SQL files in to re-seed the DB.

Cypress.Commands.add('seedDB', (customerFlag) => {

    // Set Up Variables
    deleteDataSQLFile = 'cypress/database/delete_data.sql';
    sqlFileLocation = 'cypress/database/reset/'

    // Delete all Data from DB
    cy.readFile(deleteDataSQLFile)
        .then(($file) => {
            cy.task("runSQLDB", {
                dbConfig: Cypress.config('dbConfig'),
                sqlQuery: $file
            })
            console.log(deleteDataSQLFile);
    })

    // Run all SQL files within Diretory
    cy.task('countFiles', sqlFileLocation).then((fileArray) => { 
        fileArray.forEach(fileArray => {
            cy.readFile(sqlFileLocation + fileArray).then(($file) => {
                cy.task("runSQLDB", {
                    dbConfig: Cypress.config('dbConfig'),
                    sqlQuery: $file            
                })
                console.log($file);
            })                
        });
    })
})

I am running this on a "Before" on the main test.cy.js. I seem to have two issues.

  • there is quite a few SQL files needed to reseed the DB and when cypress is running it can timeout and fails. This means running in Jenkins does not work
  • would like to disconnect it from the main test files and run before cypress has loaded the test file but not sure how go about this

Can anyone help with these two points or suggest a better way

Share Improve this question asked Feb 7 at 16:04 James DudleyJames Dudley 9574 gold badges15 silver badges36 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Increasing task timeouts

To avoid the timeout problem, the cy.task() command has a timeout option

cy.task("runSQLDB", {
  dbConfig: Cypress.config('dbConfig'),
  sqlQuery: $file
},
{ timeout: 100_000 }
)

Running before cypress has loaded the test file

Take a look at the Before Run API.

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  // setupNodeEvents can be defined in either
  // the e2e or component configuration
  e2e: {
    setupNodeEvents(on, config) {
      on('before:run', (details) => {
        /* ... */
      })
    },
  },
})

It's quite early in the process, it should run to finish before any timeouts are active.

Depending on the logic of the tasks you need to port some of the before() hook code into Cypress setupNodeEvents().


const fs = require('fs')
const deleteDataSQLFile = fs.readFileSync('./cypress/database/delete_data.sql');
const sqlFileLocation = fs.readFileSync('.cypress/database/reset/');

setupNodeEvents(on, config) {
  const dbConfig = config('dbConfig')

发布评论

评论列表(0)

  1. 暂无评论