Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set up testing #3

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ preview:
bunx --bun concurrently --kill-others "rye run sanic server --port 8070 --host 0.0.0.0" "bunx --bun vite preview --port 8071 --host 0.0.0.0"

test:
bun test #TODO: proper test setup
bun test
rye test

lint:
bunx --bun prettier --check .
Expand Down
24 changes: 24 additions & 0 deletions bun-svelte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { plugin } from "bun"

await plugin({
name: "svelte loader",
async setup(build) {
const { compile } = await import("svelte/compiler")

// when a .svelte file is imported...
build.onLoad({ filter: /\.svelte$/ }, async ({ path }) => {
// read and compile it with the Svelte compiler
const file = await Bun.file(path).text()
const contents = compile(file, {
filename: path,
generate: "client",
}).js.code

// and return the compiled source code as "js"
return {
contents,
loader: "js",
}
})
},
})
Binary file modified bun.lockb
Binary file not shown.
40 changes: 40 additions & 0 deletions bun_setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { GlobalRegistrator } from "@happy-dom/global-registrator"
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte"
import { plugin } from "bun"
import { afterEach, expect } from "bun:test"
import { cleanup } from "@testing-library/svelte"
import * as matchers from "@testing-library/jest-dom/matchers"

expect.extend(matchers)

afterEach(() => {
cleanup()
})

const oldConsole = console
GlobalRegistrator.register()
window.console = oldConsole

await plugin({
name: "svelte loader",
async setup(builder) {
const { compile, preprocess } = await import("svelte/compiler")
const { readFileSync } = await import("fs")

builder.onLoad({ filter: /\.svelte$/ }, async ({ path }) => {
return {
// Use the preprocessor of your choice.
contents: compile(
await preprocess(readFileSync(path, "utf8"), vitePreprocess()).then(
(processed) => processed.code,
),
{
filename: path,
generate: "client",
},
).js.code,
loader: "js",
}
})
},
})
2 changes: 2 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
preload = ["./bun_setup.ts"]
3 changes: 3 additions & 0 deletions happydom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { GlobalRegistrator } from "@happy-dom/global-registrator"

GlobalRegistrator.register()
7 changes: 7 additions & 0 deletions matchers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { AsymmetricMatchers, Matchers } from "bun:test"
import type { TestingLibraryMatchers } from "@testing-library/jest-dom/matchers"

declare module "bun:test" {
interface Matchers<T> extends TestingLibraryMatchers<typeof expect.stringContaining, T> {}
interface AsymmetricMatchers extends TestingLibraryMatchers {}
}
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
"private": true,
"license": "ISC",
"devDependencies": {
"@happy-dom/global-registrator": "^15.7.4",
"@sveltejs/adapter-auto": "^3.2.5",
"@sveltejs/kit": "^2.7.1",
"@sveltejs/vite-plugin-svelte": "^4.0.0-next.6",
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.6.2",
"@testing-library/svelte": "^5.2.4",
"@types/bun": "^1.1.11",
"@types/eslint": "^9.6.1",
"concurrently": "^9.0.1",
"eslint": "^9.12.0",
"eslint": "^9.13.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.45.1",
"globals": "^15.11.0",
Expand All @@ -19,9 +24,8 @@
"svelte-check": "^4.0.5",
"tslib": "^2.8.0",
"typescript": "^5.6.3",
"typescript-eslint": "^8.9.0",
"vite": "^5.4.9",
"vitest": "^2.1.3"
"typescript-eslint": "^8.10.0",
"vite": "^5.4.9"
},
"type": "module"
}
11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ build-backend = "hatchling.build"

[tool.rye]
managed = true
dev-dependencies = []
dev-dependencies = [
"pytest>=8.3.3",
]

[tool.ruff]
format.indent-style = "tab"

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
"src",
]

[tool.hatch.metadata]
allow-direct-references = true

Expand Down
7 changes: 7 additions & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ html5tagger==1.3.0
# via tracerite
httptools==0.6.2
# via sanic
iniconfig==2.0.0
# via pytest
multidict==6.1.0
# via sanic
mypy==1.12.0
# via aigis
mypy-extensions==1.0.0
# via mypy
packaging==24.1
# via pytest
pluggy==1.5.0
# via pytest
pytest==8.3.3
sanic==24.6.0
# via aigis
sanic-routing==23.12.0
Expand Down
5 changes: 5 additions & 0 deletions src/aigis/test_1plus2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pytest


def test_1plus2():
assert 1 + 2 == 3
12 changes: 11 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { describe, it, expect } from "vitest"
import { describe, it, expect } from "bun:test"
import page from "./routes/+page.svelte"
import { screen, render } from "@testing-library/svelte"

describe("sum test", () => {
it("adds 1 + 2 to equal 3", () => {
expect(1 + 2).toBe(3)
})
})

describe("root page test", () => {
it("has the correct header", async () => {
render(page)
const header = await screen.findByRole("heading")
expect(header).toBeDefined()
})
})
9 changes: 9 additions & 0 deletions testing-library.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { afterEach, expect } from "bun:test"
import { cleanup } from "@testing-library/svelte"
import * as matchers from "@testing-library/jest-dom/matchers"

expect.extend(matchers)

afterEach(() => {
cleanup()
})
5 changes: 1 addition & 4 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { sveltekit } from "@sveltejs/kit/vite"
import { defineConfig } from "vitest/config"
import { defineConfig } from "vite"

export default defineConfig({
plugins: [sveltekit()],
test: {
include: ["src/**/*.{test,spec}.{js,ts}"],
},
server: {
watch: {
ignored: ["**/__pycache__/**", "**/.*_cache/**", "**/.venv/**", "**/.direnv/**"],
Expand Down