Skip to content

[Bug] Text not appended to a file in a mock-fs filesystem when writing to a WritableStream created with the a flag #411

Closed
@Arc8ne

Description

@Arc8ne

New text does not get appended to a file in a mock-fs filesystem when the write() function of a writable stream created using the following code is called:

let fileWriteStream = fs.createWriteStream(
    "file",
    {
        flags: "a"
    }
);

The following snippet of code (originally part of a Jest test script) can be used to reproduce this bug:

const fs = require("fs");

const { test, expect } = require("@jest/globals");

const mock = require("mock-fs");

function cleanUp()
{
    mock.restore();
}

test(
    "Append to a file in a 'mock-fs' filesystem",
    async () =>
    {
        mock(
            {
                "test.log": "[Block 1]"
            }
        );

        let testLogFileWriteStream = fs.createWriteStream(
            "test.log",
            {
                flags: "a"
            }
        );

        testLogFileWriteStream.write("[Block 2]");

        let textInTestLogFile = await fs.promises.readFile(
            "test.log",
            {
                encoding: "utf-8"
            }
        );

        try
        {
            expect(textInTestLogFile).toEqual("[Block 1][Block 2]"); // Here, the 'test.log' file contains its initial text (i.e. '[Block 1]') but does not contain the text appended to it (i.e. '[Block 2]') causing this assertion to fail.
        }
        finally
        {
            cleanUp();
        }
    }
);

Is there a way to resolve this bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions