diff --git a/examples/vanilla/mathmock.spec.js b/examples/vanilla/mathmock.spec.js index f906cee..ffdf6fa 100644 --- a/examples/vanilla/mathmock.spec.js +++ b/examples/vanilla/mathmock.spec.js @@ -1,6 +1,7 @@ import { expect } from "@playwright/test"; import { test } from "rollwright"; import alias from "@rollup/plugin-alias"; +import virtual from "@rollup/plugin-virtual"; test.describe("mock", () => { test.use({ plugins: [alias({ entries: { "./math.js": "./math.mock.js" } })] }); @@ -29,6 +30,44 @@ test.describe("mock", () => { }); }); +test.describe("virtual mock", () => { + test.use({ + plugins: [ + virtual({ + inlinemock: ` + import { stub } from "sinon"; + export let sum = await (${async () => { + let { stub } = await import("sinon"); + return stub().returns(10); + }})(); + export let multiply = stub(); + `, + }), + alias({ entries: { "./math.js": "inlinemock" } }), + ], + }); + + test("sum 1", async ({ execute }) => { + let mock = await execute(async () => { + let { sum } = await import("inlinemock"); + return sum; + }); + + let res = await execute(async (from) => { + let element = document.createElement("div"); + document.body.append(element); + + let { sum } = await import("./math.js"); + element.append(document.createTextNode(String(sum(from, 2)))); + return element; + }, 1); + + expect(await res.asElement().innerText()).toContain(String(10)); + expect(await mock.evaluate((v) => v.callCount)).toEqual(1); + expect(await mock.evaluate((v) => v.calledWith(1, 2))).toEqual(true); + }); +}); + test.describe("real", () => { test("sum 1", async ({ execute }) => { let res = await execute(async (from) => { diff --git a/examples/vanilla/package.json b/examples/vanilla/package.json index 50da5e1..fd8c198 100644 --- a/examples/vanilla/package.json +++ b/examples/vanilla/package.json @@ -8,6 +8,7 @@ "dependencies": { "@playwright/test": "^1.42.1", "@rollup/plugin-alias": "^5.1.0", + "@rollup/plugin-virtual": "^3.0.2", "rollup-plugin-worker-url": "file:../../packages/rollup-plugin-worker-url", "rollwright": "file:../../packages/rollwright", "sinon": "^17.0.1"