mles
mles

Reputation: 3476

Encoding / Decoding test fails in ci, but succeeds locally

I was given this function to convert strange encodings to iso-8859-1:

export const convertCharsetToIso8859 = async (
    file: Blob | File,
): Promise<Blob> => {
    type CHARSET = 'iso-8859-1' | 'UTF-8' | 'UTF-16';

    const readFileAsText = (file: Blob | File, encoding: CHARSET) =>
        new Promise<string>((resolve, reject) => {
            const reader = new FileReader();
            reader.onload = () => resolve(reader.result as string);
            reader.onerror = reject;
            reader.readAsText(file, encoding);
        });

    const encoding: CHARSET = 'iso-8859-1';
    const content = await readFileAsText(file, encoding);

    return new Blob([content], { type: `text/csv;charset=${encoding}` });
};

There is a unit test with jest to test this:

describe('convertCharsetToIso8859', () => {
    it('should handle ISO-8859-1 charset correctly', async () => {
        const encoding = 'iso-8859-1';
        const mockFileContent =
            'test content mit Umlaute: ä, ö, ü und rumänischen Zeichen: ș, ț, ă';
        const blob = new Blob([mockFileContent], {
            type: `text/csv;charset=${encoding}`,
        });
        const expectedText = new TextDecoder(encoding).decode(
            new TextEncoder().encode(mockFileContent),
        );

        const resultBlob = await convertCharsetToIso8859(blob);

        const resultText = await new Promise((resolve, reject) => {
            const reader = new FileReader();
            reader.onload = () => resolve(reader.result);
            reader.onerror = reject;
            reader.readAsText(resultBlob);
        });

        expect(resultText).toBe(expectedText);
        expect(resultBlob.type).toBe(`text/csv;charset=${encoding}`);
    });
});

This test succeeds when run locally, but when run in our GitLab CI with a public.ecr.aws/docker/library/node:20 docker container it fails:

  ● convertCharsetToIso8859 › should handle ISO-8859-1 charset correctly
    expect(received).toBe(expected) // Object.is equality
    Expected: "test content mit Umlaute: ä, ö, ü und rumänischen Zeichen: È, È, Ä"
    Received: "test content mit Umlaute: ä, ö, ü und rumänischen Zeichen: ș, ț, ă"
      23 |         });
      24 |
    > 25 |         expect(resultText).toBe(expectedText);
         |                            ^
      26 |         expect(resultBlob.type).toBe(`text/csv;charset=${encoding}`);
      27 |     });
      28 | });
      at Object.toBe (src/tools/utils/helpers.test.ts:25:28)

I'm out of guesses what could be the reason. I've tried:

What could be the reason the tests fails in a ci environment, but succeeds locally?

Upvotes: 1

Views: 54

Answers (1)

mles
mles

Reputation: 3476

The issue seems to be the node version. With the latest node:20 container with Node 20.18.3 the tests fail. When hardcoded to 20.18.2, the tests succeed.

enter image description here

Upvotes: 0

Related Questions