|
1 | 1 | import { expect } from "@playwright/test";
|
2 | 2 | import { test } from "rollwright";
|
3 | 3 | import alias from "@rollup/plugin-alias";
|
| 4 | +import virtual from "@rollup/plugin-virtual"; |
4 | 5 |
|
5 | 6 | test.describe("mock", () => {
|
6 | 7 | test.use({ plugins: [alias({ entries: { "./math.js": "./math.mock.js" } })] });
|
@@ -29,6 +30,44 @@ test.describe("mock", () => {
|
29 | 30 | });
|
30 | 31 | });
|
31 | 32 |
|
| 33 | +test.describe("virtual mock", () => { |
| 34 | + test.use({ |
| 35 | + plugins: [ |
| 36 | + virtual({ |
| 37 | + inlinemock: ` |
| 38 | + import { stub } from "sinon"; |
| 39 | + export let sum = await (${async () => { |
| 40 | + let { stub } = await import("sinon"); |
| 41 | + return stub().returns(10); |
| 42 | + }})(); |
| 43 | + export let multiply = stub(); |
| 44 | + `, |
| 45 | + }), |
| 46 | + alias({ entries: { "./math.js": "inlinemock" } }), |
| 47 | + ], |
| 48 | + }); |
| 49 | + |
| 50 | + test("sum 1", async ({ execute }) => { |
| 51 | + let mock = await execute(async () => { |
| 52 | + let { sum } = await import("inlinemock"); |
| 53 | + return sum; |
| 54 | + }); |
| 55 | + |
| 56 | + let res = await execute(async (from) => { |
| 57 | + let element = document.createElement("div"); |
| 58 | + document.body.append(element); |
| 59 | + |
| 60 | + let { sum } = await import("./math.js"); |
| 61 | + element.append(document.createTextNode(String(sum(from, 2)))); |
| 62 | + return element; |
| 63 | + }, 1); |
| 64 | + |
| 65 | + expect(await res.asElement().innerText()).toContain(String(10)); |
| 66 | + expect(await mock.evaluate((v) => v.callCount)).toEqual(1); |
| 67 | + expect(await mock.evaluate((v) => v.calledWith(1, 2))).toEqual(true); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
32 | 71 | test.describe("real", () => {
|
33 | 72 | test("sum 1", async ({ execute }) => {
|
34 | 73 | let res = await execute(async (from) => {
|
|
0 commit comments