use vitest for tests

This commit is contained in:
MapleLeaf
2021-12-20 12:28:27 -06:00
parent a9c5122615
commit 5fe083ec60
8 changed files with 354 additions and 1019 deletions

View File

@@ -16,7 +16,7 @@ jobs:
ci:
strategy:
matrix:
script-name: [test-coverage, lint, typecheck]
script-name: [test, lint, typecheck]
fail-fast: false
name: ${{ matrix.script-name }}
runs-on: ubuntu-latest

View File

@@ -8,20 +8,16 @@
"lint-fix": "pnpm lint -- --fix",
"format": "prettier --write .",
"test": "pnpm build && pnpm test-only",
"test-only": "ava",
"test-coverage": "pnpm build && c8 pnpm test-only",
"test-only": "vitest",
"coverage": "pnpm build && vitest --coverage",
"typecheck": "pnpm build && tsc --noEmit"
},
"devDependencies": {
"@itsmapleleaf/configs": "^1.1.0",
"@types/node": "*",
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
"ava": "^4.0.0-rc.1",
"@itsmapleleaf/configs": "^1.1.2",
"@typescript-eslint/eslint-plugin": "^5.8.0",
"@typescript-eslint/parser": "^5.8.0",
"c8": "^7.10.0",
"esbuild": "^0.14.2",
"esbuild-node-loader": "^0.6.3",
"eslint": "^8.4.1",
"eslint": "^8.5.0",
"eslint-config-prettier": "^8.3.0",
"eslint-import-resolver-typescript": "^2.5.0",
"eslint-plugin-import": "^2.25.3",
@@ -29,9 +25,10 @@
"eslint-plugin-react": "^7.27.1",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-unicorn": "^39.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.5.1",
"typescript": "^4.5.4"
"typescript": "^4.5.4",
"vite": "^2.7.4",
"vitest": "^0.0.102"
},
"eslintConfig": {
"extends": [
@@ -47,21 +44,5 @@
"project": "./tsconfig.json"
}
},
"prettier": "@itsmapleleaf/configs/prettier",
"ava": {
"files": [
"**/*.test.{ts,tsx}",
"!**/{node_modules,dist,coverage}/**"
],
"nodeArguments": [
"--loader=esbuild-node-loader",
"--experimental-specifier-resolution=node",
"--no-warnings",
"--enable-source-maps"
],
"extensions": {
"ts": "module",
"tsx": "module"
}
}
"prettier": "@itsmapleleaf/configs/prettier"
}

View File

@@ -1,14 +1,16 @@
import test from "ava"
import { expect, test } from "vitest"
import { createDeferred } from "./deferred.js"
test("resolve", async (t) => {
test("resolve", async () => {
const deferred = createDeferred<string>()
setTimeout(() => deferred.resolve("hi"))
t.is(await deferred, "hi")
expect(await deferred).toBe("hi")
})
test("reject", async (t) => {
test("reject", async () => {
const deferred = createDeferred()
setTimeout(() => deferred.reject(new Error("oops")))
await t.throwsAsync(() => deferred, { instanceOf: Error, message: "oops" })
const error = new Error("oops")
setTimeout(() => deferred.reject(error))
const caught = await Promise.resolve(deferred).catch((error) => error)
expect(caught).toBe(error)
})

View File

@@ -1,66 +1,13 @@
{
"name": "reacord-integration-tests",
"private": true,
"scripts": {
"lint": "eslint --ext js,ts,tsx .",
"lint-fix": "pnpm lint -- --fix",
"format": "prettier --write .",
"test": "c8 ava",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@itsmapleleaf/configs": "^1.1.0",
"@types/node": "*",
"dependencies": {
"@types/react": "*",
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
"ava": "^4.0.0-rc.1",
"c8": "^7.10.0",
"@types/node": "^17.0.1",
"discord.js": "^13.3.1",
"dotenv": "^10.0.0",
"esbuild": "^0.14.2",
"esbuild-node-loader": "^0.6.3",
"eslint": "^8.4.1",
"eslint-config-prettier": "^8.3.0",
"eslint-import-resolver-typescript": "^2.5.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-react": "^7.27.1",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-unicorn": "^39.0.0",
"nanoid": "^3.1.30",
"prettier": "^2.5.1",
"reacord": "workspace:*",
"reacord-helpers": "workspace:*",
"react": "^17.0.2",
"typescript": "^4.5.4"
},
"eslintConfig": {
"extends": [
"./node_modules/@itsmapleleaf/configs/eslint"
],
"ignorePatterns": [
"**/node_modules/**",
"**/coverage/**",
"**/.vscode/**"
],
"parserOptions": {
"project": "./tsconfig.json"
}
},
"prettier": "@itsmapleleaf/configs/prettier",
"ava": {
"files": [
"**/*.test.{ts,tsx}"
],
"nodeArguments": [
"--loader=esbuild-node-loader",
"--experimental-specifier-resolution=node",
"--no-warnings"
],
"extensions": {
"ts": "module",
"tsx": "module"
}
"react": "^17.0.2"
}
}

View File

@@ -1,7 +1,4 @@
/* eslint-disable unicorn/numeric-separators-style */
/* eslint-disable unicorn/no-null */
import type { ExecutionContext } from "ava"
import test from "ava"
import type { Message } from "discord.js"
import { Client, TextChannel } from "discord.js"
import type { ReacordRoot } from "reacord"
@@ -9,6 +6,7 @@ import { createRoot, Embed, EmbedAuthor, Text } from "reacord"
import { pick } from "reacord-helpers/pick.js"
import { raise } from "reacord-helpers/raise.js"
import React from "react"
import { afterAll, beforeAll, expect, test } from "vitest"
import { testBotToken, testChannelId } from "./test-environment.js"
const client = new Client({
@@ -18,7 +16,7 @@ const client = new Client({
let channel: TextChannel
let root: ReacordRoot
test.serial.before(async () => {
beforeAll(async () => {
await client.login(testBotToken)
const result =
@@ -31,31 +29,27 @@ test.serial.before(async () => {
}
channel = result
root = createRoot(channel)
for (const [, message] of await channel.messages.fetch()) {
await message.delete()
}
root = createRoot(channel)
})
test.after(() => {
afterAll(() => {
client.destroy()
})
// test.serial.beforeEach(async () => {
// const messages = await channel.messages.fetch()
// await Promise.all(messages.map((message) => message.delete()))
// })
test.serial("rapid updates", async (t) => {
test("rapid updates", async () => {
// rapid updates
void root.render("hi world")
void root.render("hi the")
await root.render("hi moon")
await assertMessages(t, [{ content: "hi moon" }])
await assertMessages([{ content: "hi moon" }])
})
test.serial("nested text", async (t) => {
test("nested text", async () => {
await root.render(
<Text>
<Text>hi world</Text>{" "}
@@ -64,35 +58,35 @@ test.serial("nested text", async (t) => {
</Text>
</Text>,
)
await assertMessages(t, [{ content: "hi world hi moon hi sun" }])
await assertMessages([{ content: "hi world hi moon hi sun" }])
})
test.serial("empty embed fallback", async (t) => {
test("empty embed fallback", async () => {
await root.render(<Embed />)
await assertMessages(t, [{ embeds: [{ description: "_ _" }] }])
await assertMessages([{ embeds: [{ description: "_ _" }] }])
})
test.serial("embed with only author", async (t) => {
test("embed with only author", async () => {
await root.render(
<Embed>
<EmbedAuthor>only author</EmbedAuthor>
</Embed>,
)
await assertMessages(t, [
await assertMessages([
{ embeds: [{ description: "_ _", author: { name: "only author" } }] },
])
})
test.serial("empty embed author", async (t) => {
test("empty embed author", async () => {
await root.render(
<Embed>
<EmbedAuthor />
</Embed>,
)
await assertMessages(t, [{ embeds: [{ description: "_ _" }] }])
await assertMessages([{ embeds: [{ description: "_ _" }] }])
})
test.serial("kitchen sink", async (t) => {
test("kitchen sink", async () => {
await root.render(
<>
message <Text>content</Text>
@@ -111,12 +105,12 @@ test.serial("kitchen sink", async (t) => {
</Embed>
</>,
)
await assertMessages(t, [
await assertMessages([
{
content: "message contentno space",
embeds: [
{
color: 0xfeeeef,
color: 0xfe_ee_ef,
description: "description more description",
author: {
name: "hi craw",
@@ -131,9 +125,9 @@ test.serial("kitchen sink", async (t) => {
])
})
test.serial("destroy", async (t) => {
test("destroy", async () => {
await root.destroy()
await assertMessages(t, [])
await assertMessages([])
})
type MessageData = ReturnType<typeof extractMessageData>
@@ -149,14 +143,10 @@ function extractMessageData(message: Message) {
}
}
async function assertMessages(
t: ExecutionContext<unknown>,
expected: Array<DeepPartial<MessageData>>,
) {
async function assertMessages(expected: Array<DeepPartial<MessageData>>) {
const messages = await channel.messages.fetch()
t.deepEqual(
messages.map((message) => extractMessageData(message)),
expect(messages.map((message) => extractMessageData(message))).toEqual(
expected.map((message) => ({
content: "",
...message,

View File

@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig",
"include": ["**/*", "node_modules/reacord/dist"]
}

View File

@@ -31,10 +31,10 @@
"devDependencies": {
"discord.js": "^13.3.1",
"dotenv": "^10.0.0",
"esbuild": "^0.14.2",
"esbuild": "^0.14.6",
"nanoid": "^3.1.30",
"reacord-helpers": "workspace:*",
"react": "^17.0.2",
"tsup": "^5.10.3"
"tsup": "^5.11.6"
}
}

1197
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff