wix-incubator / jest-allure2-reporter

🦉📊 Idiomatic Jest reporter for Allure Framework
https://wix-incubator.github.io/jest-allure2-reporter/
MIT License
10 stars 0 forks source link

Reimplement string formatting #29

Closed noomorph closed 6 months ago

noomorph commented 9 months ago

Currently, @Step, @Attachment, allure.createStep, etc. have incovenient formatting via %s and %d. It can be significantly improved if we switch to Handlebars.js, e.g.:

const Handlebars = require('handlebars');

// Create a new Handlebars environment and put it into Allure Runtime
const myHandlebarsInstance = Handlebars.create();

// The instance can be used for registering helpers
myHandlebarsInstance.registerHelper('percent', () => {
    // Helper implementation
});

The coolest thing about it is that if we combine it with function parsing, e.g.:

const { parseFunction } = require('parse-function')();

function wrapWithLogger(templateString, fn) {
    // Compile the Handlebars template
    const template = myHandlebarsInstance.compile(templateString);

    // Parse the provided function to get its arguments names
    const parsedFn = parseFunction.parse(fn);

    return function (...args) {
        // Create an object to map argument names to their values
        const scope = parsedFn.args.reduce((acc, arg, index) => {
            acc[arg] = args[index];
            return acc;
        }, args);

        // Use the constructed scope to populate the template and log the message
        const message = template(scope);
        console.log(message);

        // Call the original function with all its arguments
        return fn.apply(this, args);
    };
}

// Example usage
function assertSum(a, b, c) {
    return a + b === c;
}

const loggedAssertSum = log("Adding {{0}} and {{b}} = {{c percent}}", assertSum);
const result = loggedAssertSum(5, 3, 8);  // Logs: "Adding 5 and 3 = 800%"

Impact

Early adopters using:

@Step("Type text: %s")

will have to switch to this syntax:

@Step("Type text: {{0}}")

or even this one: 😮

@Step("Type text: {{value}}")
noomorph commented 9 months ago

@MetodievIvaylo, could you assess how much it will impact you regarding rewriting? The reporter is still in beta, and I'm somewhat indulging in breaking some features to improve them before the stable release. If this hurts, I'll create a fallback for you.

noomorph commented 6 months ago

Released in 2.0.0-beta.15.