diff --git a/src/reporters/index.js b/src/reporters/index.js index 2f994c39d..a3d6f8d06 100644 --- a/src/reporters/index.js +++ b/src/reporters/index.js @@ -72,14 +72,14 @@ function getReporterDefinition(rawReporter, parser) { return { type: null, path: null }; } - const { type, path } = parser(rawReporter); + const { type, path, append } = parser(rawReporter); if (!type) { const strRawReporter = typeof rawReporter !== "string" ? JSON.stringify(rawReporter) : rawReporter; throw new Error(`Failed to find required "type" field in reporter definition: "${strRawReporter}"`); } - return { type, path }; + return { type, path, append }; } function validateReporter(Reporter) { diff --git a/src/reporters/informers/file.js b/src/reporters/informers/file.js index 82ab87b8b..126b6309c 100644 --- a/src/reporters/informers/file.js +++ b/src/reporters/informers/file.js @@ -8,7 +8,7 @@ module.exports = class FileInformer extends BaseInformer { constructor(opts) { super(opts); - this._fileStream = fs.createWriteStream(opts.path); + this._fileStream = fs.createWriteStream(opts.path, { flags: opts.append === true ? "a" : "w" }); this._reporterType = opts.type; logger.log(`Information with test results for report: "${opts.type}" will be saved to a file: "${opts.path}"`); diff --git a/test/src/reporters/index.js b/test/src/reporters/index.js index 62dda848d..e6e7275eb 100644 --- a/test/src/reporters/index.js +++ b/test/src/reporters/index.js @@ -93,7 +93,7 @@ describe('"initReporters" method', () => { it("should create reporter with correct args", async () => { sinon.spy(Reporter, "create"); - const jsonReporter = JSON.stringify({ type: "reporter", path: "./foo/bar.txt" }); + const jsonReporter = JSON.stringify({ type: "reporter", path: "./foo/bar.txt", append: true }); await initReporters([jsonReporter], runner); @@ -121,7 +121,7 @@ describe('"initReporters" method', () => { it("should create reporter with correct args", async () => { sinon.spy(Reporter, "create"); - const objReporter = { type: "reporter", path: "./foo/bar.txt" }; + const objReporter = { type: "reporter", path: "./foo/bar.txt", append: true }; await initReporters([objReporter], runner); diff --git a/test/src/reporters/informers/file.js b/test/src/reporters/informers/file.js index b5beeae1b..d0bcd7e55 100644 --- a/test/src/reporters/informers/file.js +++ b/test/src/reporters/informers/file.js @@ -22,10 +22,16 @@ describe("reporter/informers/file", () => { afterEach(() => sandbox.restore()); - it("should create write stream to passed file path", () => { + it("should overwrite the file by default", () => { FileInformer.create({ path: "./some-path/file.txt" }); - assert.calledOnceWith(fs.createWriteStream, "./some-path/file.txt"); + assert.calledOnceWith(fs.createWriteStream, "./some-path/file.txt", { flags: "w" }); + }); + + it("should append to the file when requested", () => { + FileInformer.create({ path: "./some-path/file.txt", append: true }); + + assert.calledOnceWith(fs.createWriteStream, "./some-path/file.txt", { flags: "a" }); }); it("should inform user that test results will be saved to a file", () => {