|
| 1 | +import { spawnSync } from "child_process"; |
| 2 | + |
| 3 | +const MODULE_NAME = "@huggingface/transformers"; |
| 4 | + |
| 5 | +const CODE_BODY = ` |
| 6 | +const model_id = "hf-internal-testing/tiny-random-LlamaForCausalLM"; |
| 7 | +const generator = await pipeline("text-generation", model_id, { dtype: "fp32" }); |
| 8 | +const result = await generator("hello", { max_new_tokens: 3, return_full_text: false }); |
| 9 | +process.stdout.write(result[0].generated_text); |
| 10 | +`; |
| 11 | + |
| 12 | +const TARGET_OUTPUT = "erdingsAndroid Load"; |
| 13 | + |
| 14 | +const wrap_async_iife = (code) => `(async function() { ${code} })();`; |
| 15 | + |
| 16 | +const check = (code, module = false) => { |
| 17 | + const args = ["-e", code]; |
| 18 | + if (module) args.push("--input-type=module"); |
| 19 | + const { status, stdout, stderr } = spawnSync("node", args); |
| 20 | + expect(stderr.toString()).toBe(""); // No warnings or errors are printed |
| 21 | + expect(stdout.toString()).toBe(TARGET_OUTPUT); // The output should match |
| 22 | + expect(status).toBe(0); // The process should exit cleanly |
| 23 | +}; |
| 24 | + |
| 25 | +describe("Testing the bundle", () => { |
| 26 | + it("ECMAScript Module (ESM)", () => { |
| 27 | + check(`import { pipeline } from "${MODULE_NAME}";${CODE_BODY}`, true); |
| 28 | + }); |
| 29 | + |
| 30 | + it("CommonJS (CJS) with require", () => { |
| 31 | + check(`const { pipeline } = require("${MODULE_NAME}");${wrap_async_iife(CODE_BODY)}`); |
| 32 | + }); |
| 33 | + |
| 34 | + it("CommonJS (CJS) with dynamic import", () => { |
| 35 | + check(`${wrap_async_iife(`const { pipeline } = await import("${MODULE_NAME}");${CODE_BODY}`)}`); |
| 36 | + }); |
| 37 | +}); |
0 commit comments