Cypress测试中使用ibm_db触发bindings.js TypeError错误的求助
问题:Cypress中使用ibm_db访问DB2数据库报错
环境信息
- Node版本:18.19.1
问题背景
现有Cypress项目内的测试均可正常运行,新增通过ibm_db访问DB2数据库的测试后,将测试文件复制到/test/目录并重命名为DB.test.js,可通过Mocha正常执行(首次执行常超时,第二次成功),但在Cypress中运行时直接报错。
项目结构
dzs-e2e-tests/ ├── cypress/ │ ├── e2e/ │ │ ├── DB.cy.ts │ │ └── 其他测试文件 │ ├── fixtures/ │ ├── integrations/ │ └── support/ │ ├── commands.ts │ ├── db2Utils.js │ └── e2e.ts ├── node_modules/ ├── test/ │ └── db2Util.test.js ├── cypress.config.ts ├── package.json └── package-lock.json
DB.cy.ts代码
const { getTestData } = require('../support/db2Utils.js'); describe('Test the getTestData method', () => { it('Data should be returned', (done) => { getTestData((err: Error | null, data: any) => { if (err) { done(err); // fail test in case of errors } else { if (data && data.length > 0) { done(); // If data has been returned, close test as successful } else { done(new Error('No data returned.')); // test failed because no data was returned } } }); }).timeout(4000); });
/support/db2Utils.js代码
const ibmdb = require('ibm_db'); function getTestData(callback) { const connString = process.env.DB_CONN_STRING; if (connString === undefined) { throw new Error('DB_CONN_STRING is missing. Needed to run test.'); } ibmdb.open(connString, function (err, conn) { if (err) { console.error("Error when trying to open connection:", err); callback(err, null); return; } conn.query("SELECT konto FROM ENTW.XY where END_KREDIT_NEHMER = 11753215", function (err, data) { if (err) { console.error("Error at query:", err); callback(err, null); } else { callback(null, data); } conn.close(function () { console.log("Connection closed."); }); }); }); } module.exports = { getTestData };
错误及堆栈信息
1) An uncaught error was detected outside of a test: TypeError: The following error originated from your test code, not from Cypress. > Cannot read properties of undefined (reading 'indexOf') When Cypress detects uncaught errors originating from your test code it will automatically fail the current test. Cypress could not associate this error to any specific test. We dynamically generated a new test to display this failure. at Function.getFileName (node_modules/bindings/bindings.js:178:0) at bindings (node_modules/bindings/bindings.js:82:0) at Object.eval (node_modules/ibm_db/lib/odbc.js:57:0) at Object.eval (https://myrepo/__cypress/tests?p=cypress/e2e/DB.cy.js:8991:4) at 38../climacros (https://myrepo/__cypress/tests?p=cypress/e2e/DB.cy.js:8991:17) at o (node_modules/browser-pack/_prelude.js:1:0) at eval (node_modules/browser-pack/_prelude.js:1:0) at Object.eval (cypress/support/db2Utils.js:1:14) at Object.eval (https://myrepo/__cypress/tests?p=cypress/e2e/DB.cy.js:55:4) at 2._process (https://myrepo/__cypress/tests?p=cypress/e2e/DB.cy.js:55:17)
/support/e2e.ts代码
import './commands' import 'cypress-plugin-tab'
package.json配置
{ "scripts": { "test:e2e": "cypress run --e2e", "test:db2": "mocha", "test:db2:debug": "mocha --inspect" }, "engines": { "npm": ">=8.0.0 <11.0.0", "node": ">=16.0.0 <21.0.0" }, "stepDefinitions": [ "cypress/e2e/[filepath]/**/*.{js,ts}", "cypress/e2e/[filepath].{js,ts}", "cypress/support/step_definitions/**/*.{js,ts}" ], "devDependencies": { "@cypress/webpack-preprocessor": "^6.0.2", "@types/ibm_db": "^3.2.0", "@types/node": "^22.7.5", "cypress": "13.6.2", "cypress-cucumber-preprocessor": "^4.3.1", "cypress-plugin-tab": "^1.0.5", "mocha": "^10.6.0", "terser-webpack-plugin": "^5.3.11", "ts-loader": "^9.5.1", "ts-node": "^10.9.2", "typescript": "^5.6.3" }, "dependencies": { "ibm_db": "^3.2.4" } }
cypress.config.ts配置
import {defineConfig} from 'cypress' const cucumber = require('cypress-cucumber-preprocessor').default; export default defineConfig({ e2e: { setupNodeEvents(on, config) { const cucumberOptions = { typescript: require.resolve('typescript'), } on('file:preprocessor', cucumber(cucumberOptions)) return config; }, video: true, specPattern: ['cypress/integration/*.feature', 'cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}'], supportFile: 'cypress/support/e2e.ts', excludeSpecPattern: [ 'cypress/e2e/**/T002*.cy.ts' ], baseUrl: 'https://my_app.net', viewportWidth: 1920, viewportHeight: 1080 } })
已尝试的解决方法
- 在
cypress.config.ts中替换为webpack-preprocessor,并配置TerserPlugin:
{ ... const webpackOptions = webpackPreprocessor.defaultOptions; webpackOptions.optimization = { minimize: true, minimizer: [ new TerserPlugin({ terserOptions: { compress: { reduce_vars: false, }, }, }), ], }; on('file:preprocessor', webpackPreprocessor(webpackOptions)); ... }
- 参考过bindings库的相关issue,但问题未解决。
疑问
请问问题根源是什么?该如何修复?
内容的提问来源于stack exchange,提问作者BranAndSceolan
相关产品推荐
相关产品推荐

