When debugging with jest values are displayed as undefined while they have values
I'm wondering why some values are displayed as `undefined` when suspending at a breakpoint while they seem to have values. It seems to happen when accessing global variables from another module:
export let list: string[] = [];
export const setList = (val: string[]) => {
list = val;
};
with this test:
import { list, setList } from './testdummy';
describe('modify global var from other module', () => {
it('should modify the global variable', () => {
setList(['hello, world']);
expect(list).toHaveLength(1); // success
expect(list[0]).toBe('hello, world'); // success
});
});
Everything works fine, but when I set a breakpoint and peek at the value, it's undefined:
Any idea what that might cause that behavior? Thank you!
Please sign in to leave a comment.
Undefined variables (
list
in your case) while evaluating code during debugging is caused by the weird way imported objects are transpiled + missing name mappings in sourcemaps: if the variable is renamed while transpiling/obfuscating, and no appropriate name mapping is provided, the debugger won't be able to match variable in source code with the one in VM.There are some known issues when evaluating ES6 imports in modules transpiled by babel/built by webpack. Please see https://github.com/webpack/webpack/issues/3957, https://github.com/babel/babel/issues/1468 for details. There is unfortunately no way to fix the issue on the IDE end.
I see. I was afraid of such. Thanks for your quick response, though!