I'm trying to write a custom reporter that will output the intern test results to a file, but I've reached a roadblock.

I managed to get hold of the Node file system API via Intern's dojo loader, but neither writeFile or writeFileSync do anything (the difference being that writeFileSync locks up the process).

A minimal version of my reporter code:

define(["node_modules/intern/node_modules/dojo/node!fs"], function (fs) {
    var buffer = "";
    return {
        "/test/pass": function(test) {
            buffer += test.id + " passed\n";
        },
        "/test/fail": function(test) {
            buffer += test.id + " failed\n";
        },
        "/runner/end": function () {
            fs.writeFileSync("result.txt", buffer, function (error) {
                if (error) {
                    throw error;
                }
                console.log("File saved");
            });
        }
    };
});

If I replace the write call with a console log, I get the expected data, so everything else appears to be working fine.

Edit: Modified code that works:

define(["node_modules/intern/node_modules/dojo/node!fs"], function (fs) {
    var buffer = "";
    return {
        "/test/pass": function(test) {
            buffer += test.id + " passed\n";
        },
        "/test/fail": function(test) {
            buffer += test.id + " failed\n";
        },
        "/runner/end": function () {
            fs.writeFileSync("result.txt", buffer);
            console.log("File saved");
        }
    };
});
有帮助吗?

解决方案

Yes, it is possible to write files from reporters—the lcov reporter does exactly this. However, until Intern 1.2 is released, asynchronous I/O operations have the potential to not complete properly because the process is forcibly terminated at the end of the test run.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top