In Node.js: Why does this test fail on the second call of main
?
test('base64Encode and back', () => {
function main(input: string) {
const base64string = base64Encode(input);
const text = base64Decode(base64string);
expect(input).toEqual(text);
}
main('demo');
main('');
});
Here are my functions:
export function base64Encode(text: string): string {
const buffer = Buffer.from(text, 'binary');
return buffer.toString('base64');
}
export function base64Decode(base64EncodedString: string): string {
const buffer = Buffer.from(base64EncodedString, 'base64');
return buffer.toString('binary');
}
From these pages, I figured I had written these functions correctly so that one would reverse the other:
- https://github.com/node-browser-compat/btoa/blob/master/index.js
- https://github.com/node-browser-compat/atob/blob/master/node-atob.js
- https://stackoverflow.com/a/47890385/470749
If I change the 'binary'
options to be 'utf8'
instead, the test passes.
But my database currently has data where this function only seems to work if I use 'binary'
.