Skip to content

Commit

Permalink
chore: replace ava with vitest (storacha#239)
Browse files Browse the repository at this point in the history
Based on the discussion in storacha#150, I decided to try replacing ava with
vitest and found that it was super easy 😄 Much nicer out-of-the-box
experience than getting ava to play nice with typescript.

Any objections to tossing ava overboard? edit: once the build failure is
sorted out, that is 😐
  • Loading branch information
yusefnapora authored Jan 12, 2023
1 parent 0b5d6e5 commit afe756f
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 604 deletions.
15 changes: 0 additions & 15 deletions ava.config.mjs

This file was deleted.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"version": "0.0.0",
"description": "Headless, type-safe, UI components for the next generation Web3.Storage APIs.",
"author": "Alan Shaw",
"maintainers": [ "travis", "yusefnapora", "olizilla" ],
"maintainers": [
"travis",
"yusefnapora",
"olizilla"
],
"license": "Apache-2.0 OR MIT",
"scripts": {
"prepare": "pnpm run build && pnpm run compile",
Expand All @@ -16,7 +20,6 @@
"typecheck": "tsc -b"
},
"devDependencies": {
"@ava/typescript": "^3.0.1",
"@babel/plugin-transform-modules-commonjs": "^7.18.6",
"@babel/preset-env": "^7.19.0",
"@babel/preset-react": "^7.18.6",
Expand All @@ -35,10 +38,9 @@
"@ucanto/transport": "^4.0.3",
"@web-std/file": "^3.0.2",
"@web3-storage/capabilities": "^2.0.0",
"ava": "^5.1.0",
"esm": "^3.2.25",
"fake-indexeddb": "^4.0.1",
"jsdom": "^20.0.3",
"jsdom": "^21.0.0",
"path": "^0.12.7",
"react": "^18.2.0",
"rollup": "^2.79.0",
Expand All @@ -49,6 +51,8 @@
"ts-node": "^10.9.1",
"ts-standard": "^12.0.1",
"typescript": "^4.8.2",
"vitest": "^0.27.0",
"vitest-environment-w3ui": "workspace:^",
"vue": "^3.2.39"
}
}
3 changes: 2 additions & 1 deletion packages/keyring-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"scripts": {
"compile": "../../node_modules/.bin/tsc -p tsconfig.json --noEmit --emitDeclarationOnly false",
"test": "ava"
"test": "vitest run",
"test:watch": "vitest watch"
},
"files": [
"build/*",
Expand Down
23 changes: 23 additions & 0 deletions packages/keyring-core/test/agent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { test, expect } from 'vitest'
import 'fake-indexeddb/auto'

import { createAgent } from '../src/index'

test('createAgent', async () => {
const agent = await createAgent()
expect(agent).toBeTruthy()
expect(agent.did().startsWith('did:key')).toBe(true)
expect(agent.spaces.size).to.eql(0)
})

test('createSpace', async () => {
const agent = await createAgent()
const space = await agent.createSpace('test')
expect(space).toBeTruthy()
expect(space.did.startsWith('did:key:')).toBe(true)
})

test('registerSpace fails if no current space is set', async () => {
const agent = await createAgent()
await expect(agent.registerSpace('[email protected]')).rejects.toThrowError()
})
29 changes: 0 additions & 29 deletions packages/keyring-core/test/agent.ts

This file was deleted.

9 changes: 9 additions & 0 deletions packages/vitest-environment-w3ui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# vitest-environment-w3ui

A utility package that exports a custom [Vitest test environment](https://vitest.dev/guide/environment.html).

This is a very thin wrapper around the default JSDOM vitest environment that also defines the `crypto` global, using Node's builtin `crypto.webcrypto` implementation.

This is needed because the vitest JSDOM environment defines a `crypto` global with only the `getRandomValues` function defined, but one of our dependencies (`@noble/ed25519`) assumes that if the `crypto` global exists, it can safely access the WebCrypto APIs at `crypto.subtle`.

Note that this package is not published to NPM, as it's only used when testing other packages in this repo.
15 changes: 15 additions & 0 deletions packages/vitest-environment-w3ui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "vitest-environment-w3ui",
"version": "1.0.0",
"description": "A vitest environment that exposes Node's webcrypto API to the builtin JSDOM test environment",
"main": "src/index.ts",
"files": [
"src"
],
"keywords": [],
"author": "Yusef Napora",
"license": "Apache 2.0 OR MIT",
"dependencies": {
"vitest": "^0.27.0"
}
}
24 changes: 24 additions & 0 deletions packages/vitest-environment-w3ui/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import crypto from 'crypto'
import { builtinEnvironments } from 'vitest/environments'
import type { Environment } from 'vitest'

const env: Environment = {
name: 'w3ui',
async setup (global, options) {
// use the standard jsdom environment as a base
const jsdom = await builtinEnvironments.jsdom.setup(global, options)

// `crypto` is defined as a getter on the global scope in Node 19+,
// so attempting to set it with `Object.assign()` would fail. Instead,
// override the getter with a new value.
Object.defineProperty(global, 'crypto', { get: () => crypto.webcrypto })

return {
async teardown (global) {
await jsdom.teardown(global)
}
}
}
}

export default env
Loading

0 comments on commit afe756f

Please sign in to comment.