<commit_before>"use strict";

const path = require("path");

require("css.escape");
require("handlebars/runtime");
const {JSDOM} = require("jsdom");
const _ = require("lodash");

const handlebars = require("./handlebars");
const stub_i18n = require("./i18n");
const namespace = require("./namespace");
const test = require("./test");
const blueslip = require("./zblueslip");
const zjquery = require("./zjquery");
const zpage_params = require("./zpage_params");

global.DOMParser = new JSDOM().window.DOMParser;

require("@babel/register")({
    extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],
    only: [
        new RegExp("^" + _.escapeRegExp(path.resolve(__dirname, "../../static/js") + path.sep)),
        new RegExp(
            "^" + _.escapeRegExp(path.resolve(__dirname, "../../static/shared/js") + path.sep),
        ),
    ],
    plugins: [
        "babel-plugin-rewire-ts",
        ["@babel/plugin-transform-modules-commonjs", {lazy: () => true}],
    ],
});

// Create a helper function to avoid sneaky delays in tests.
function immediate(f) {
    return () => f();
}

// Find the files we need to run.
const files = process.argv.slice(2);
if (files.length === 0) {
    throw new Error("No tests found");
}

// Set up our namespace helpers.
const window = new Proxy(global, {
    set: (obj, prop, value) => {
        namespace.set_global(prop, value);
        return true;
    },
});

// Set up Handlebars
handlebars.hook_require();

const noop = function () {};

function short_tb(tb) {
    const lines = tb.split("\n");

    const i = lines.findIndex((line) => line.includes("run_one_module"));

    if (i === -1) {
        return tb;
    }

    return lines.splice(0, i + 1).join("\n") + "\n(...)\n";
}

require("../../static/js/templates"); // register Zulip extensions

function run_one_module(file) {
    zjquery.clear_initialize_function();
    zjquery.clear_all_elements();
    console.info("running test " + path.basename(file, ".js"));
    test.set_current_file_name(file);
    require(file);
    namespace.complain_about_unused_mocks();
}

test.set_verbose(files.length === 1);

try {
    for (const file of files) {
        namespace.start();
        namespace.set_global("window", window);
        namespace.set_global("to_$", () => window);
        namespace.set_global("location", {
            hash: "#",<commit_msg>node_tests: Refresh templates.js import for every test.<commit_after>"use strict";

const path = require("path");

require("css.escape");
require("handlebars/runtime");
const {JSDOM} = require("jsdom");
const _ = require("lodash");

const handlebars = require("./handlebars");
const stub_i18n = require("./i18n");
const namespace = require("./namespace");
const test = require("./test");
const blueslip = require("./zblueslip");
const zjquery = require("./zjquery");
const zpage_params = require("./zpage_params");

global.DOMParser = new JSDOM().window.DOMParser;

require("@babel/register")({
    extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],
    only: [
        new RegExp("^" + _.escapeRegExp(path.resolve(__dirname, "../../static/js") + path.sep)),
        new RegExp(
            "^" + _.escapeRegExp(path.resolve(__dirname, "../../static/shared/js") + path.sep),
        ),
    ],
    plugins: [
        "babel-plugin-rewire-ts",
        ["@babel/plugin-transform-modules-commonjs", {lazy: () => true}],
    ],
});

// Create a helper function to avoid sneaky delays in tests.
function immediate(f) {
    return () => f();
}

// Find the files we need to run.
const files = process.argv.slice(2);
if (files.length === 0) {
    throw new Error("No tests found");
}

// Set up our namespace helpers.
const window = new Proxy(global, {
    set: (obj, prop, value) => {
        namespace.set_global(prop, value);
        return true;
    },
});

// Set up Handlebars
handlebars.hook_require();

const noop = function () {};

function short_tb(tb) {
    const lines = tb.split("\n");

    const i = lines.findIndex((line) => line.includes("run_one_module"));

    if (i === -1) {
        return tb;
    }

    return lines.splice(0, i + 1).join("\n") + "\n(...)\n";
}

function run_one_module(file) {
    zjquery.clear_initialize_function();
    zjquery.clear_all_elements();
    console.info("running test " + path.basename(file, ".js"));
    test.set_current_file_name(file);
    require(file);
    namespace.complain_about_unused_mocks();
}

test.set_verbose(files.length === 1);

try {
    for (const file of files) {
        // register Zulip extensions before each test file; this is
        // necessary because of how we mock i18n for every module
        // except for the i18n test suite.
        require("../../static/js/templates");

        namespace.start();
        namespace.set_global("window", window);
        namespace.set_global("to_$", () => window);
        namespace.set_global("location", {
            hash: "#",
{'fraction_non_alphanumeric': 0.18602281988590058, 'fraction_numerical': 0.005297473512632437, 'mean_word_length': 3.1252100840336134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4987949', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1554, 'n_words': 358}