未定义 xmljs GLOBAL 的开玩笑测试
jest test with xmljs GLOBAL not defined
目前我正在使用
编写应用程序
- NodeJS v13.12.0
- 杰斯特 25.4.0
- xmljs 0.3.2
- 打字稿 3.8.3
- ts-jest 25.4.0
此应用程序应模仿 CalDAV 服务器。出于这个原因,我依赖于模块 xmljs,这是(经过我的研究)唯一给我一个直接 path
方法来查找 XML 中的属性的模块。
在节点Container中,App运行良好,没有任何错误。但是当我开始使用 Jest 进行测试时,测试失败并显示错误
ReferenceError: GLOBAL is not defined
at node_modules/xmljs/core.js:46:2
at Object.<anonymous> (node_modules/xmljs/core.js:176:3)
at node_modules/xmljs/XmlParser.js:3:11
at Object.<anonymous> (node_modules/xmljs/XmlParser.js:204:3)
我现在知道,这个错误源于 xmljs 模块试图设置 GLOBAL
变量,该变量在 NodeJS 中解析为 global
。但这不是开玩笑。
我的代码如下所示:
import XmlParser = require("xmljs");
/*
* data is the body of a PROPFIND request
*/
new XmlParser({ strict: true }).parseString(data, (err, xmlNode) => {
// omit err
xmlNode.path(["propfind", "prop"], true);
const propertiesObj: XmlNode[] = childs[0].children;
const properties: string[] = [];
Object.keys(propertiesObj).forEach(n => {
properties.push(n);
});
logger.silly("Returning properties: %O", properties);
});
任何人都可以
向我展示一个无需对我的代码进行大量修改即可使用的模块
- 它支持不使用 node-gyp 的纯 js 实现(因为它可能在 windows 服务器上使用)
告诉我如何开玩笑地做一个变通方法来欺骗在 xmljs 中设置的这个 GLOBAL 变量
感谢您的帮助
您可以在测试设置中设置 GLOBAL
的值。 GLOBAL
变量似乎是节点中 global
的弃用形式。
在您的 jest.config.js
文件中,您可以通过 setupFiles
选项添加安装文件:
module.exports = {
[...] // Other configurations
setupFiles: ['<rootDir>/define-deprecated-global.js']
};
并且在文件 define-deprecated-global
中,您可以将 GLOBAL
变量定义为:
global.GLOBAL = global;
目前我正在使用
编写应用程序- NodeJS v13.12.0
- 杰斯特 25.4.0
- xmljs 0.3.2
- 打字稿 3.8.3
- ts-jest 25.4.0
此应用程序应模仿 CalDAV 服务器。出于这个原因,我依赖于模块 xmljs,这是(经过我的研究)唯一给我一个直接 path
方法来查找 XML 中的属性的模块。
在节点Container中,App运行良好,没有任何错误。但是当我开始使用 Jest 进行测试时,测试失败并显示错误
ReferenceError: GLOBAL is not defined
at node_modules/xmljs/core.js:46:2
at Object.<anonymous> (node_modules/xmljs/core.js:176:3)
at node_modules/xmljs/XmlParser.js:3:11
at Object.<anonymous> (node_modules/xmljs/XmlParser.js:204:3)
我现在知道,这个错误源于 xmljs 模块试图设置 GLOBAL
变量,该变量在 NodeJS 中解析为 global
。但这不是开玩笑。
我的代码如下所示:
import XmlParser = require("xmljs");
/*
* data is the body of a PROPFIND request
*/
new XmlParser({ strict: true }).parseString(data, (err, xmlNode) => {
// omit err
xmlNode.path(["propfind", "prop"], true);
const propertiesObj: XmlNode[] = childs[0].children;
const properties: string[] = [];
Object.keys(propertiesObj).forEach(n => {
properties.push(n);
});
logger.silly("Returning properties: %O", properties);
});
任何人都可以
向我展示一个无需对我的代码进行大量修改即可使用的模块
- 它支持不使用 node-gyp 的纯 js 实现(因为它可能在 windows 服务器上使用)
告诉我如何开玩笑地做一个变通方法来欺骗在 xmljs 中设置的这个 GLOBAL 变量
感谢您的帮助
您可以在测试设置中设置 GLOBAL
的值。 GLOBAL
变量似乎是节点中 global
的弃用形式。
在您的 jest.config.js
文件中,您可以通过 setupFiles
选项添加安装文件:
module.exports = {
[...] // Other configurations
setupFiles: ['<rootDir>/define-deprecated-global.js']
};
并且在文件 define-deprecated-global
中,您可以将 GLOBAL
变量定义为:
global.GLOBAL = global;