Skip to content

Commit

Permalink
chore: add ava config and some basic tests (#215)
Browse files Browse the repository at this point in the history
This needs some cleanup, but I got ava + typescript to work after a bit
of wrestling.

So far I've only added some simple tests to keyring-core that don't
depend on the service, but I've also started on a mock access service so
we can test `registerSpace`, etc.

I had to add `"type": "module"` to the keyring-core package.json to get
the ava + typescript setup to work. That seems pretty safe, since we're
only including the rollup bundle in the published package, so it
shouldn't make a difference to users.

It seems like the mock services would be useful for all the packages, so
maybe we should make a shared `test-utils` package?

Co-authored-by: Travis Vachon <[email protected]>
  • Loading branch information
yusefnapora and travis authored Jan 5, 2023
1 parent 96d6c9b commit 4bd233b
Show file tree
Hide file tree
Showing 9 changed files with 727 additions and 1,424 deletions.
2 changes: 1 addition & 1 deletion .github/actions/test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ runs:
steps:
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18
- uses: pnpm/action-setup@v2
with:
version: 7
Expand Down
15 changes: 15 additions & 0 deletions ava.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default {
files: [
'test/**',
'!test/utils/'
],
extensions: {
ts: 'module'
},
nodeArguments: [
'--loader=ts-node/esm',
// the following options lets us avoid file extensions in imports
// per https://github.com/avajs/ava/blob/main/docs/recipes/typescript.md#for-packages-with-type-module
'--experimental-specifier-resolution=node'
]
}
30 changes: 0 additions & 30 deletions jest.config.js

This file was deleted.

22 changes: 0 additions & 22 deletions jest.env.js

This file was deleted.

14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,33 @@
"clean": "rm -rf node_modules package-lock.json coverage packages/*/{package-lock.json,build,node_modules}",
"compile": "pnpm run --recursive --if-present compile",
"lint": "ts-standard",
"test": "jest --config jest.config.js --passWithNoTests",
"test": "pnpm run --recursive --if-present test",
"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",
"@babel/preset-typescript": "^7.18.6",
"@babel/register": "^7.18.9",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^14.0.0",
"@rollup/plugin-replace": "^4.0.0",
"@types/jest": "^29.0.2",
"@types/jsdom": "^20.0.1",
"@types/react": "^18.0.18",
"@ucanto/client": "^4.0.3",
"@ucanto/server": "^4.0.3",
"@ucanto/transport": "^4.0.3",
"@web-std/file": "^3.0.2",
"jest": "^27.5.1",
"@web3-storage/capabilities": "^2.0.0",
"ava": "^5.1.0",
"esm": "^3.2.25",
"fake-indexeddb": "^4.0.1",
"jsdom": "^20.0.3",
"path": "^0.12.7",
"react": "^18.2.0",
"rollup": "^2.79.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/keyring-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"version": "2.0.1",
"description": "w3ui keyring core.",
"main": "src/index.ts",
"type": "module",
"publishConfig": {
"module": "build/esm/index.js",
"main": "build/cjs/index.js",
"browser": "build/umd/index.production.js",
"types": "build/types/index.d.ts"
},
"scripts": {
"compile": "../../node_modules/.bin/tsc -p tsconfig.json --noEmit --emitDeclarationOnly false"
"compile": "../../node_modules/.bin/tsc -p tsconfig.json --noEmit --emitDeclarationOnly false",
"test": "ava"
},
"files": [
"build/*",
Expand Down
29 changes: 29 additions & 0 deletions packages/keyring-core/test/agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import test from 'ava'
import { JSDOM } from 'jsdom'
import 'fake-indexeddb/auto'

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

test.before((t) => {
const dom = new JSDOM('<!DOCTYPE html>')
globalThis.document = dom.window.document
})

test('createAgent', async (t) => {
const agent = await createAgent()
t.truthy(agent)
t.true(agent.did().startsWith('did:key'))
t.is(agent.spaces.size, 0)
})

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

test('registerSpace fails if no current space is set', async (t) => {
const agent = await createAgent()
await t.throwsAsync(agent.registerSpace('[email protected]'))
})
6 changes: 5 additions & 1 deletion packages/keyring-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"outDir": "./build/types"
},
"files": ["src/index.ts"],
"include": ["src"]
"include": ["src"],
"ts-node": {
"esm": true,
"transpileOnly": true,
}
}
Loading

0 comments on commit 4bd233b

Please sign in to comment.