Search code examples
javascriptnode.jsjestjspuppeteerreferenceerror

Jest - ReferenceError: imported function is not defined


I have been at this for 2 hours now, but couldn't figure out what I am doing wrong. I have following jest+puppeteer test:

import {convert} from '../__helpers__/number';

const getAmount = async (page) => {
  const element = await page.waitForSelector('.my-element');

  return element.evaluate((node) => convert(node.innerText));
}

which is very simple and straight forward. But it keeps failing with following error:

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)

The above line number don't match with the source code .. and from the error it looked like it is failing from the transpiled code. So I went to the jest-transform-cache location and looked at the file, and the line numbers don't match that either; they are all off by 1. The transpiled code also had the updated output and had following line:

var _number = require('../__helpers__/number');

I tried debugging in all the ways I could think of:

  • cleared cache
  • tried renaming the file
  • restarted the system
  • ran the test in different system
  • running jest with no cache etc

Also tried this:

// 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));
}

Now it fails with following error:

Evaluation failed: ReferenceError: convert is not defined

      at __puppeteer_evaluation_script__:2:15
      at ExecutionContext._evaluateInternal (node_modules/puppeteer/lib/ExecutionContext.js:122:13)
.
:

Thought I would throw it here to see if anyone else run into this weirdness. It shouldn't fail by any norms, and I can't figure it out at all. Please help me! Any ideas/direction would be greatly appreciated.

Thank you!


Solution

  • Alright, after a week since I asked this question, I just picked the test back up to solve this. I just didn't think of the below code. I guess staring at the code for hours, that breaks for no reason, can do that to you.

    Just do the conversion after the evaluation ..

    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);
    }
    

    And thank you @plat123456789 for your answer, much appreciated!