javascript - 在 node.js 中需要 mocha 测试文件导致 TypeError: describe is not a function
问题描述
我有一个包含某些测试的测试文件:
describe("tests", () => {
before(async () => {
//....
});
afterEach(async () => {
// ...
});
});
我想在我的 node.js 代码中要求该文件:
const test = require(resolve('server.test'));
console.log(test);
但由于以下错误,它不起作用:
TypeError: describe is not a function
我试图从测试文件中导出一些东西,如下所示:
// in the test file
module.exports.name = 11;
describe("tests", () => {
// ....
});
// in node.js
const name = require(resolve('server.test'));
const t = JSON.stringify(name);
console.log(t);
我仍然遇到同样的错误
解决方案
我不确定您为什么要require
在常规代码文件中测试文件,但如果您真的想要,您可以向全局命名空间对象describe
添加一个函数,以便在需要您的测试文件时它存在:
if (global.describe === undefined) { // if a global describe doesn't exist...
global.describe = () => {}; // ...then define one
}
const test = require(resolve('server.test'));
console.log(test);
推荐阅读
- git - PowerShell 一行命令显示本地 git 存储库的名称
- c++ - 如何静态链接 allegro5 (Linux, G++)?
- scala - 解压时如何在 Scala 中压缩多个文件而不创建文件夹
- c - 为什么这个联盟以与预期不同的方式组织数据?
- python - 在 pyinstaller 中添加 pip 库以为我的脚本创建 exe 文件
- python - Python:列出驱动器、子目录中的文件并按扩展名排序
- git - I already have 1 code commit account set up, how do I set up a second code commit account? (I'm getting a clone error on the second account)
- python - 无法打开在 mpi4py 中创建的 zip 档案
- google-apps-marketplace - 无法将屏幕截图上传到 G Suite Marketplace
- firebase - 使用 Geofire 查询时出现 getter 错误