From 994a1d2b41282d9c642731a55f26d226e2082b4a Mon Sep 17 00:00:00 2001 From: Jean-Yves Moyen Date: Fri, 6 Sep 2024 14:52:48 +0200 Subject: [PATCH] Workaround missing crypto in JSDOM --- unit-testing/vue/jsdom-environment.ts | 8 +++++++- unit-testing/vue/setup.ts | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/unit-testing/vue/jsdom-environment.ts b/unit-testing/vue/jsdom-environment.ts index 5e94e7b..a6d2926 100644 --- a/unit-testing/vue/jsdom-environment.ts +++ b/unit-testing/vue/jsdom-environment.ts @@ -1,9 +1,11 @@ /** - * Workaround for the missing `TextEncoder` in JSDOM. + * Workaround for the missing `TextEncoder` and `crypto` in JSDOM. * * {@link https://github.com/mswjs/mswjs.io/issues/292#issue-1977585807} */ +import crypto from "node:crypto"; + import type { EnvironmentContext, JestEnvironmentConfig, @@ -17,6 +19,10 @@ class MyJSDOMEnvironment extends JSDOMEnvironment.default { // here, you have access to regular Node globals, which you can add to the test environment this.global.TextEncoder = TextEncoder; + + // @ts-ignore + this.global.crypto = crypto; + this.global.crypto.randomUUID = crypto.randomUUID; } } diff --git a/unit-testing/vue/setup.ts b/unit-testing/vue/setup.ts index 52a4b98..5e90c5f 100644 --- a/unit-testing/vue/setup.ts +++ b/unit-testing/vue/setup.ts @@ -7,10 +7,13 @@ import * as alfa from "@siteimprove/alfa-jest"; import { Rules } from "@siteimprove/alfa-rules"; const R12 = Rules.get("R12").getUnsafe(); -import { persist } from "common/persist"; +// import { persist } from "common/persist"; alfa.Jest.createPlugin( (value: Vue.Type) => Future.from(Vue.toPage(value)), [R12], - [persist(() => "outcomes/button.spec.json")] + // For some reason, jsonld isn't included correctly in the JSDOM environment. + // Since we do not officially support Vue integration, we just skip the + // persister on this test. + [] );