Jest - ReferenceError: imported function is not defined
Jest - ReferenceError: imported function is not defined
我已经在这里待了 2 个小时了,但无法弄清楚我做错了什么。我有以下 jest+puppeteer 测试:
import {convert} from '../__helpers__/number';
const getAmount = async (page) => {
const element = await page.waitForSelector('.my-element');
return element.evaluate((node) => convert(node.innerText));
}
非常简单明了。但它一直失败并出现以下错误:
Error: Evaluation failed: ReferenceError: _number is not defined
at __puppeteer_evaluation_script__:2:15
at ExecutionContext._evaluateInternal (node_modules/puppeteer/lib/ExecutionContext.js:122:13)
-- ASYNC --
at ExecutionContext.<anonymous> (node_modules/puppeteer/lib/helper.js:111:15)
at ElementHandle.evaluate (node_modules/puppeteer/lib/JSHandle.js:55:42)
at ElementHandle.evaluate (node_modules/puppeteer/lib/helper.js:112:23)
at _callee$ (__tests__/e2e/test.spec.js:13:27)
at tryCatch (node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:271:22)
at Generator.prototype.(anonymous function) [as next] (node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:97:21)
at asyncGeneratorStep (__tests__/e2e/test.spec.js:30:103)
at _next (__tests__/e2e/test.spec.js:32:194)
上面的行号与源代码不匹配.. 从错误来看,它似乎是 t运行spiled 代码失败的原因。所以我去了 jest-transform-cache
位置并查看了文件,行号也不匹配;它们都相差 1。t运行spiled 代码也有更新的输出并有以下行:
var _number = require('../__helpers__/number');
我尝试了所有我能想到的调试方式:
- 已清除缓存
- 已尝试重命名文件
- 重启系统
- 运行不同系统测试
- 运行没有缓存的笑话等
也试过这个:
// import {convert} from '../__helpers__/number';
const convert = (text) => magic(text); // for simplicity's sake
const getAmount = async (page) => {
const element = await page.waitForSelector('.my-element');
return element.evaluate((node) => convert(node.innerText));
}
现在失败并出现以下错误:
Evaluation failed: ReferenceError: convert is not defined
at __puppeteer_evaluation_script__:2:15
at ExecutionContext._evaluateInternal (node_modules/puppeteer/lib/ExecutionContext.js:122:13)
.
:
我想我会把它扔在这里看看是否还有其他人 运行 遇到这种怪事。它不应该以任何规范失败,而且我根本无法弄清楚。请帮我!任何 ideas/direction 将不胜感激。
谢谢!
您需要像这样将转换函数作为 elementHandle.evaluate(pageFunction[, ...args]) 的参数传入:
import {convert} from '../__helpers__/number';
const getAmount = async (page) => {
const element = await page.waitForSelector('.my-element');
return element.evaluate((node, convert) => convert(node.innerText), convert);
}
好吧,在我问这个问题一个星期后,我刚刚重新选择了测试来解决这个问题。我只是没有想到下面的代码。我想盯着代码看几个小时,无缘无故地中断,可以对你这样做。
评价完再做转换..
import {convert} from '../__helpers__/number';
const getAmount = async (page) => {
const element = await page.waitForSelector('.my-element');
const amountString = await element.evaluate((node) => node.innerText);
return convert(amountString);
}
谢谢你 ,非常感谢!
我已经在这里待了 2 个小时了,但无法弄清楚我做错了什么。我有以下 jest+puppeteer 测试:
import {convert} from '../__helpers__/number';
const getAmount = async (page) => {
const element = await page.waitForSelector('.my-element');
return element.evaluate((node) => convert(node.innerText));
}
非常简单明了。但它一直失败并出现以下错误:
Error: Evaluation failed: ReferenceError: _number is not defined
at __puppeteer_evaluation_script__:2:15
at ExecutionContext._evaluateInternal (node_modules/puppeteer/lib/ExecutionContext.js:122:13)
-- ASYNC --
at ExecutionContext.<anonymous> (node_modules/puppeteer/lib/helper.js:111:15)
at ElementHandle.evaluate (node_modules/puppeteer/lib/JSHandle.js:55:42)
at ElementHandle.evaluate (node_modules/puppeteer/lib/helper.js:112:23)
at _callee$ (__tests__/e2e/test.spec.js:13:27)
at tryCatch (node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:271:22)
at Generator.prototype.(anonymous function) [as next] (node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:97:21)
at asyncGeneratorStep (__tests__/e2e/test.spec.js:30:103)
at _next (__tests__/e2e/test.spec.js:32:194)
上面的行号与源代码不匹配.. 从错误来看,它似乎是 t运行spiled 代码失败的原因。所以我去了 jest-transform-cache
位置并查看了文件,行号也不匹配;它们都相差 1。t运行spiled 代码也有更新的输出并有以下行:
var _number = require('../__helpers__/number');
我尝试了所有我能想到的调试方式:
- 已清除缓存
- 已尝试重命名文件
- 重启系统
- 运行不同系统测试
- 运行没有缓存的笑话等
也试过这个:
// import {convert} from '../__helpers__/number';
const convert = (text) => magic(text); // for simplicity's sake
const getAmount = async (page) => {
const element = await page.waitForSelector('.my-element');
return element.evaluate((node) => convert(node.innerText));
}
现在失败并出现以下错误:
Evaluation failed: ReferenceError: convert is not defined
at __puppeteer_evaluation_script__:2:15
at ExecutionContext._evaluateInternal (node_modules/puppeteer/lib/ExecutionContext.js:122:13)
.
:
我想我会把它扔在这里看看是否还有其他人 运行 遇到这种怪事。它不应该以任何规范失败,而且我根本无法弄清楚。请帮我!任何 ideas/direction 将不胜感激。
谢谢!
您需要像这样将转换函数作为 elementHandle.evaluate(pageFunction[, ...args]) 的参数传入:
import {convert} from '../__helpers__/number';
const getAmount = async (page) => {
const element = await page.waitForSelector('.my-element');
return element.evaluate((node, convert) => convert(node.innerText), convert);
}
好吧,在我问这个问题一个星期后,我刚刚重新选择了测试来解决这个问题。我只是没有想到下面的代码。我想盯着代码看几个小时,无缘无故地中断,可以对你这样做。
评价完再做转换..
import {convert} from '../__helpers__/number';
const getAmount = async (page) => {
const element = await page.waitForSelector('.my-element');
const amountString = await element.evaluate((node) => node.innerText);
return convert(amountString);
}
谢谢你