1

Using jest-puppeteer to get the below 'page' object. That works fine.

My imported constant is coming back as undefined when in the evaluate function. I'm unsure why, given it works right outside the function in the parent scope just fine...

const CONSTANTS = require('../../../constants/Config')
const DICTIONARY = require('../../../constants/Dictionary')

describe('Check that reevaluate review section', () => {
    // Blah blah //
    var apiCall = CONSTANTS.NODE_ENDPOINT + "/api" + '/1234'  // outputs correctly

    test("reevaluate options include 'Sunset'", async () => {
    // Blah blah //
        console.log('DICTIONARY.SUNSET :>> ', DICTIONARY.SUNSET);  // outputs correctly
        const element_is_visible = await page.evaluate(() => {
            console.log('DICTIONARY.SUNSET :>> ', DICTIONARY.SUNSET);  // says "DICTIONARY is undefined"
    // Blah blah //
        });
        expect(element_is_visible).toBe(true)
    })

});

Dictionary.js

export const PENDING = "Pending"
export const PENDING_BY_QUEUE = "Pending"
export const SUNSET = "Sunset"
export const DUPLICATE_STATUS_OPTION = "Duplicate"
export const WITHDRAW_STATUS_OPTION = "Withdraw"
export const RETIRED_STATUS_OPTION = "Retired"

I suspect this problem might have something to do with the mixing of ES6 imports and nodejs imports... but I'm not sure why.

lowcrawler
  • 6,777
  • 9
  • 37
  • 79
  • 1
    It doesn't have anything to do with imports or exports, it's simply that you've not passed the variable to `evaluate`. Common Puppeteer gotcha -- that callback is serialized/deserialized and runs in browser context, a totally different process, not in Node context, so it can't see variables from Node. – ggorlen May 19 '22 at 16:05

0 Answers0