Skip to content

Commit

Permalink
Workaround missing crypto in JSDOM
Browse files Browse the repository at this point in the history
  • Loading branch information
Jym77 committed Sep 6, 2024
1 parent fc4dc97 commit 994a1d2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion unit-testing/vue/jsdom-environment.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
}
}

Expand Down
7 changes: 5 additions & 2 deletions unit-testing/vue/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
[]
);

0 comments on commit 994a1d2

Please sign in to comment.