【发布时间】:2019-12-20 13:36:05
【问题描述】:
我的 Yarn package.json 是这样设置的,我在其中创建了一个名为 localPath 的全局变量。
{
"jest": {
"globals": {
"localPath": "Users/alex/Git/mytodolist"
}
}
}
然后,在我的一项规范测试中,我运行
console.log(localPath)
但得到这个错误。
ReferenceError: localPath is not defined
5 |
> 6 | console.log(localPath)
有人知道如何调用你设置的全局变量吗?我只能找到关于创建变量的文章,而不是关于如何调用它的文章。
来源:https://jestjs.io/docs/en/configuration#globals-object
编辑:感谢@slideshowp2 提供以下正确答案。结果我最终不需要使用全局变量,因为您可以在运行时动态获取执行路径。不过,这在未来肯定会有用。
beforeAll(async () => {
await page.goto('file:///'+process.cwd()+'/index.html')
})
【问题讨论】:
-
你在
jest.config.js中也尝试过吗? -
你能在你的测试中试试
global.localPath吗? -
是的,这行不通。给出同样的错误。
-
您上面的内容应该可以正常工作,您确定没有任何其他玩笑配置覆盖
package.json中的内容吗?你是如何运行测试规范的? -
以上sn-p是我
package.json的范围。我正在使用yarn jest test运行测试。
标签: javascript jestjs integration-testing