From e9c6481223323731573761a49dbbc62670eae094 Mon Sep 17 00:00:00 2001 From: Andrew Patton Date: Mon, 22 Jan 2024 13:29:20 -0800 Subject: [PATCH] Add webcrypto node.js + browser tests --- .pnp.cjs | 45 +++++++++++++------------- packages/webcrypto/package.json | 4 +++ packages/webcrypto/src/browser.test.ts | 12 +++++++ packages/webcrypto/src/node.test.ts | 19 +++++++++++ yarn.lock | 1 + 5 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 packages/webcrypto/src/browser.test.ts create mode 100644 packages/webcrypto/src/node.test.ts diff --git a/.pnp.cjs b/.pnp.cjs index 995d14a6..43e29b85 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -154,7 +154,7 @@ const RAW_RUNTIME_STATE = ["@acusti/post", "workspace:packages/post"],\ ["@types/node", "npm:20.10.5"],\ ["typescript", "patch:typescript@npm%3A5.3.3#optional!builtin::version=5.3.3&hash=e012d7"],\ - ["vitest", "virtual:d86e9109c35931fea3fd99d40fb126c9c4e0d08179ba95ce293ca3f7653be7e7a182f33676f632c72c755f7f362d9c7779a021a379fe6973fa09ca73f19902fd#npm:1.1.0"]\ + ["vitest", "virtual:54be9ef6f2875e5ea191d927659d88639c5383ec1cdb6eaa338a29bcc043e2d159814748837f6a0f75ac6e9d1074d142d7fab6be1ce7a3b4840d51ce619387d4#npm:1.1.0"]\ ],\ "linkType": "SOFT"\ }]\ @@ -167,7 +167,7 @@ const RAW_RUNTIME_STATE = ["@acusti/webcrypto", "workspace:packages/webcrypto"],\ ["@types/node", "npm:20.10.5"],\ ["typescript", "patch:typescript@npm%3A5.3.3#optional!builtin::version=5.3.3&hash=e012d7"],\ - ["vitest", "virtual:d86e9109c35931fea3fd99d40fb126c9c4e0d08179ba95ce293ca3f7653be7e7a182f33676f632c72c755f7f362d9c7779a021a379fe6973fa09ca73f19902fd#npm:1.1.0"]\ + ["vitest", "virtual:54be9ef6f2875e5ea191d927659d88639c5383ec1cdb6eaa338a29bcc043e2d159814748837f6a0f75ac6e9d1074d142d7fab6be1ce7a3b4840d51ce619387d4#npm:1.1.0"]\ ],\ "linkType": "SOFT"\ }]\ @@ -307,7 +307,7 @@ const RAW_RUNTIME_STATE = ["@acusti/post", "workspace:packages/post"],\ ["@types/node", "npm:20.10.5"],\ ["typescript", "patch:typescript@npm%3A5.3.3#optional!builtin::version=5.3.3&hash=e012d7"],\ - ["vitest", "virtual:d86e9109c35931fea3fd99d40fb126c9c4e0d08179ba95ce293ca3f7653be7e7a182f33676f632c72c755f7f362d9c7779a021a379fe6973fa09ca73f19902fd#npm:1.1.0"]\ + ["vitest", "virtual:54be9ef6f2875e5ea191d927659d88639c5383ec1cdb6eaa338a29bcc043e2d159814748837f6a0f75ac6e9d1074d142d7fab6be1ce7a3b4840d51ce619387d4#npm:1.1.0"]\ ],\ "linkType": "SOFT"\ }]\ @@ -572,8 +572,9 @@ const RAW_RUNTIME_STATE = "packageDependencies": [\ ["@acusti/webcrypto", "workspace:packages/webcrypto"],\ ["@types/node", "npm:20.10.5"],\ + ["happy-dom", "npm:12.10.3"],\ ["typescript", "patch:typescript@npm%3A5.3.3#optional!builtin::version=5.3.3&hash=e012d7"],\ - ["vitest", "virtual:d86e9109c35931fea3fd99d40fb126c9c4e0d08179ba95ce293ca3f7653be7e7a182f33676f632c72c755f7f362d9c7779a021a379fe6973fa09ca73f19902fd#npm:1.1.0"]\ + ["vitest", "virtual:afe4e4a1c85e0b022894420d9b3e04e61e9c8c5fe86707d0599325649d85a5d5c03732ca16e1484fddaba15449239efc39b69b9e5b4963f252ae7a4aee0b9a39#npm:1.1.0"]\ ],\ "linkType": "SOFT"\ }]\ @@ -18032,15 +18033,15 @@ const RAW_RUNTIME_STATE = ],\ "linkType": "SOFT"\ }],\ - ["virtual:ae52462715931ea007dc50efba07d133bbd2707991d966b03dd50bc7d26895a233be9457b0dfcacf62c9e7732b6ee7fddb7ea173aaf1a49dde4d8dadb45358ba#npm:1.1.0", {\ - "packageLocation": "./.yarn/__virtual__/vitest-virtual-2568738550/0/cache/vitest-npm-1.1.0-14b6057a75-5e4ac0231b.zip/node_modules/vitest/",\ + ["virtual:54be9ef6f2875e5ea191d927659d88639c5383ec1cdb6eaa338a29bcc043e2d159814748837f6a0f75ac6e9d1074d142d7fab6be1ce7a3b4840d51ce619387d4#npm:1.1.0", {\ + "packageLocation": "./.yarn/__virtual__/vitest-virtual-ab45a602e9/0/cache/vitest-npm-1.1.0-14b6057a75-5e4ac0231b.zip/node_modules/vitest/",\ "packageDependencies": [\ - ["vitest", "virtual:ae52462715931ea007dc50efba07d133bbd2707991d966b03dd50bc7d26895a233be9457b0dfcacf62c9e7732b6ee7fddb7ea173aaf1a49dde4d8dadb45358ba#npm:1.1.0"],\ + ["vitest", "virtual:54be9ef6f2875e5ea191d927659d88639c5383ec1cdb6eaa338a29bcc043e2d159814748837f6a0f75ac6e9d1074d142d7fab6be1ce7a3b4840d51ce619387d4#npm:1.1.0"],\ ["@edge-runtime/vm", null],\ ["@types/edge-runtime__vm", null],\ ["@types/happy-dom", null],\ ["@types/jsdom", null],\ - ["@types/node", null],\ + ["@types/node", "npm:20.10.5"],\ ["@types/vitest__browser", null],\ ["@types/vitest__ui", null],\ ["@vitest/browser", null],\ @@ -18065,7 +18066,7 @@ const RAW_RUNTIME_STATE = ["strip-literal", "npm:1.3.0"],\ ["tinybench", "npm:2.5.1"],\ ["tinypool", "npm:0.8.1"],\ - ["vite", "virtual:1399d2729d64dc03bc6ba87b35b2514fd0dea3c0240c84cd36ea0f768c0a1e237ecffe158c4515a7ecf38dad79526daab4a701410776a9dbfce2198dcacbe385#npm:5.0.10"],\ + ["vite", "virtual:afe4e4a1c85e0b022894420d9b3e04e61e9c8c5fe86707d0599325649d85a5d5c03732ca16e1484fddaba15449239efc39b69b9e5b4963f252ae7a4aee0b9a39#npm:5.0.10"],\ ["vite-node", "npm:1.1.0"],\ ["why-is-node-running", "npm:2.2.2"]\ ],\ @@ -18084,10 +18085,10 @@ const RAW_RUNTIME_STATE = ],\ "linkType": "HARD"\ }],\ - ["virtual:afdae6bed2220ffeb070ee40379649c9baf1f40f92a18dda206ea109ee08e197c8d4ded52ffaeedb5def9932505c08fa82afb8ccafbb0155d514fb4046b48c2a#npm:1.1.0", {\ - "packageLocation": "./.yarn/__virtual__/vitest-virtual-e81139061f/0/cache/vitest-npm-1.1.0-14b6057a75-5e4ac0231b.zip/node_modules/vitest/",\ + ["virtual:ae52462715931ea007dc50efba07d133bbd2707991d966b03dd50bc7d26895a233be9457b0dfcacf62c9e7732b6ee7fddb7ea173aaf1a49dde4d8dadb45358ba#npm:1.1.0", {\ + "packageLocation": "./.yarn/__virtual__/vitest-virtual-2568738550/0/cache/vitest-npm-1.1.0-14b6057a75-5e4ac0231b.zip/node_modules/vitest/",\ "packageDependencies": [\ - ["vitest", "virtual:afdae6bed2220ffeb070ee40379649c9baf1f40f92a18dda206ea109ee08e197c8d4ded52ffaeedb5def9932505c08fa82afb8ccafbb0155d514fb4046b48c2a#npm:1.1.0"],\ + ["vitest", "virtual:ae52462715931ea007dc50efba07d133bbd2707991d966b03dd50bc7d26895a233be9457b0dfcacf62c9e7732b6ee7fddb7ea173aaf1a49dde4d8dadb45358ba#npm:1.1.0"],\ ["@edge-runtime/vm", null],\ ["@types/edge-runtime__vm", null],\ ["@types/happy-dom", null],\ @@ -18107,7 +18108,7 @@ const RAW_RUNTIME_STATE = ["chai", "npm:4.3.10"],\ ["debug", "virtual:72cf4b87a119309db526d8a999aabadaaefa571c6f516a993f7ea2866552916d3e380a275156714cfdc270f48325b11cffeb3a050bfc46a315e4a93d48b1293d#npm:4.3.4"],\ ["execa", "npm:8.0.1"],\ - ["happy-dom", "npm:12.10.3"],\ + ["happy-dom", null],\ ["jsdom", null],\ ["local-pkg", "npm:0.5.0"],\ ["magic-string", "npm:0.30.5"],\ @@ -18136,15 +18137,15 @@ const RAW_RUNTIME_STATE = ],\ "linkType": "HARD"\ }],\ - ["virtual:afe4e4a1c85e0b022894420d9b3e04e61e9c8c5fe86707d0599325649d85a5d5c03732ca16e1484fddaba15449239efc39b69b9e5b4963f252ae7a4aee0b9a39#npm:1.1.0", {\ - "packageLocation": "./.yarn/__virtual__/vitest-virtual-b9a84b3b01/0/cache/vitest-npm-1.1.0-14b6057a75-5e4ac0231b.zip/node_modules/vitest/",\ + ["virtual:afdae6bed2220ffeb070ee40379649c9baf1f40f92a18dda206ea109ee08e197c8d4ded52ffaeedb5def9932505c08fa82afb8ccafbb0155d514fb4046b48c2a#npm:1.1.0", {\ + "packageLocation": "./.yarn/__virtual__/vitest-virtual-e81139061f/0/cache/vitest-npm-1.1.0-14b6057a75-5e4ac0231b.zip/node_modules/vitest/",\ "packageDependencies": [\ - ["vitest", "virtual:afe4e4a1c85e0b022894420d9b3e04e61e9c8c5fe86707d0599325649d85a5d5c03732ca16e1484fddaba15449239efc39b69b9e5b4963f252ae7a4aee0b9a39#npm:1.1.0"],\ + ["vitest", "virtual:afdae6bed2220ffeb070ee40379649c9baf1f40f92a18dda206ea109ee08e197c8d4ded52ffaeedb5def9932505c08fa82afb8ccafbb0155d514fb4046b48c2a#npm:1.1.0"],\ ["@edge-runtime/vm", null],\ ["@types/edge-runtime__vm", null],\ ["@types/happy-dom", null],\ ["@types/jsdom", null],\ - ["@types/node", "npm:20.10.5"],\ + ["@types/node", null],\ ["@types/vitest__browser", null],\ ["@types/vitest__ui", null],\ ["@vitest/browser", null],\ @@ -18169,7 +18170,7 @@ const RAW_RUNTIME_STATE = ["strip-literal", "npm:1.3.0"],\ ["tinybench", "npm:2.5.1"],\ ["tinypool", "npm:0.8.1"],\ - ["vite", "virtual:afe4e4a1c85e0b022894420d9b3e04e61e9c8c5fe86707d0599325649d85a5d5c03732ca16e1484fddaba15449239efc39b69b9e5b4963f252ae7a4aee0b9a39#npm:5.0.10"],\ + ["vite", "virtual:1399d2729d64dc03bc6ba87b35b2514fd0dea3c0240c84cd36ea0f768c0a1e237ecffe158c4515a7ecf38dad79526daab4a701410776a9dbfce2198dcacbe385#npm:5.0.10"],\ ["vite-node", "npm:1.1.0"],\ ["why-is-node-running", "npm:2.2.2"]\ ],\ @@ -18188,10 +18189,10 @@ const RAW_RUNTIME_STATE = ],\ "linkType": "HARD"\ }],\ - ["virtual:d86e9109c35931fea3fd99d40fb126c9c4e0d08179ba95ce293ca3f7653be7e7a182f33676f632c72c755f7f362d9c7779a021a379fe6973fa09ca73f19902fd#npm:1.1.0", {\ - "packageLocation": "./.yarn/__virtual__/vitest-virtual-4ccf6d8209/0/cache/vitest-npm-1.1.0-14b6057a75-5e4ac0231b.zip/node_modules/vitest/",\ + ["virtual:afe4e4a1c85e0b022894420d9b3e04e61e9c8c5fe86707d0599325649d85a5d5c03732ca16e1484fddaba15449239efc39b69b9e5b4963f252ae7a4aee0b9a39#npm:1.1.0", {\ + "packageLocation": "./.yarn/__virtual__/vitest-virtual-b9a84b3b01/0/cache/vitest-npm-1.1.0-14b6057a75-5e4ac0231b.zip/node_modules/vitest/",\ "packageDependencies": [\ - ["vitest", "virtual:d86e9109c35931fea3fd99d40fb126c9c4e0d08179ba95ce293ca3f7653be7e7a182f33676f632c72c755f7f362d9c7779a021a379fe6973fa09ca73f19902fd#npm:1.1.0"],\ + ["vitest", "virtual:afe4e4a1c85e0b022894420d9b3e04e61e9c8c5fe86707d0599325649d85a5d5c03732ca16e1484fddaba15449239efc39b69b9e5b4963f252ae7a4aee0b9a39#npm:1.1.0"],\ ["@edge-runtime/vm", null],\ ["@types/edge-runtime__vm", null],\ ["@types/happy-dom", null],\ @@ -18211,7 +18212,7 @@ const RAW_RUNTIME_STATE = ["chai", "npm:4.3.10"],\ ["debug", "virtual:72cf4b87a119309db526d8a999aabadaaefa571c6f516a993f7ea2866552916d3e380a275156714cfdc270f48325b11cffeb3a050bfc46a315e4a93d48b1293d#npm:4.3.4"],\ ["execa", "npm:8.0.1"],\ - ["happy-dom", null],\ + ["happy-dom", "npm:12.10.3"],\ ["jsdom", null],\ ["local-pkg", "npm:0.5.0"],\ ["magic-string", "npm:0.30.5"],\ diff --git a/packages/webcrypto/package.json b/packages/webcrypto/package.json index e73fc197..ef112815 100644 --- a/packages/webcrypto/package.json +++ b/packages/webcrypto/package.json @@ -29,6 +29,9 @@ "ts", "flow" ], + "scripts": { + "test": "vitest" + }, "repository": { "type": "git", "url": "https://github.com/acusti/uikit.git", @@ -45,6 +48,7 @@ }, "devDependencies": { "@types/node": "^20.10.5", + "happy-dom": "^12.10.3", "typescript": "^5.3.3", "vitest": "^1.1.0" } diff --git a/packages/webcrypto/src/browser.test.ts b/packages/webcrypto/src/browser.test.ts new file mode 100644 index 00000000..52dec4b9 --- /dev/null +++ b/packages/webcrypto/src/browser.test.ts @@ -0,0 +1,12 @@ +// @vitest-environment happy-dom +import { describe, expect, it } from 'vitest'; + +import webcrypto from './browser.js'; + +describe('@acusti/webcrypto', () => { + describe('browser.ts', () => { + it('exports node:crypto’s webcrypto object', () => { + expect(webcrypto).toBe(window.crypto); + }); + }); +}); diff --git a/packages/webcrypto/src/node.test.ts b/packages/webcrypto/src/node.test.ts new file mode 100644 index 00000000..a1f421ef --- /dev/null +++ b/packages/webcrypto/src/node.test.ts @@ -0,0 +1,19 @@ +import crypto from 'node:crypto'; + +import { describe, expect, it } from 'vitest'; + +import webcrypto from './node.js'; + +describe('@acusti/webcrypto', () => { + describe('node.ts', () => { + it('exports node:crypto’s webcrypto object', () => { + expect(webcrypto).toBe(crypto.webcrypto); + }); + + it('works in a node environment (as opposed to browser.js, which throws an error)', async () => { + await expect(() => import('./browser.js')).rejects.toThrowError( + 'not defined', + ); + }); + }); +}); diff --git a/yarn.lock b/yarn.lock index c49be02c..168cd59a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -294,6 +294,7 @@ __metadata: resolution: "@acusti/webcrypto@workspace:packages/webcrypto" dependencies: "@types/node": "npm:^20.10.5" + happy-dom: "npm:^12.10.3" typescript: "npm:^5.3.3" vitest: "npm:^1.1.0" languageName: unknown