const foo = undefined; const bar = 'bar'; const foobar = foo?.replace(`foo${bar}`, ''); expect(foobar).toBe(undefined);