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

Migrating vitest to SDK #1672

Merged
merged 10 commits into from
Jan 27, 2024
Merged
Changes from 1 commit
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
Next Next commit
WIP
infomiho committed Jan 24, 2024
commit c07dba8e61949f5bc1e3c409c7344aee035d73c3
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import matchers from '@testing-library/jest-dom/matchers'
import { expect } from 'vitest'
import { afterEach } from 'vitest'
import { cleanup } from '@testing-library/react'
Copy link
Contributor Author

@infomiho infomiho Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the vitest and all the accompanying libraries to the latest versions. I did this because there was an error with using jsx files from deps (in our case our SDK) which would mean we would need to bundle our SDK for vitest to work again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running with the latest versions seems to work fine 👍

import '@testing-library/jest-dom/vitest'

expect.extend(matchers)
// runs a clean after each test case (e.g. clearing jsdom)
afterEach(() => {
cleanup();
})
3 changes: 2 additions & 1 deletion waspc/data/Generator/templates/react-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -6,7 +6,8 @@
"allowJs": true,
"strict": false,
// Allow importing pages with the .tsx extension.
"allowImportingTsExtensions": true
"allowImportingTsExtensions": true,
"types": ["@testing-library/jest-dom"]
},
"include": [
"src"
7 changes: 6 additions & 1 deletion waspc/data/Generator/templates/react-app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -29,8 +29,13 @@ const defaultViteConfig = {
outDir: "build",
},
test: {
globals: true,
environment: "jsdom",
setupFiles: ["./src/test/vitest/setup.ts"],
setupFiles: ["./.wasp/out/web-app/test/vitest/setup.ts"],
// This is a hack to make Vite's test runner work with user files living
// outside of the web app root dir.
root: "../../..",
exclude: [".wasp/**/*", "**/node_modules/**"]
},
// resolve: {
// dedupe: ["react", "react-dom"],
6 changes: 5 additions & 1 deletion waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
@@ -97,7 +97,11 @@
{=! Used by our code, uncodumented (but accessible) for users. =}
"./server/queries": "./dist/server/queries/index.js",
{=! Used by users, documented. =}
"./dbSeed/types": "./dist/dbSeed/types.js"
"./dbSeed/types": "./dist/dbSeed/types.js",
{=! Used by users, documented. =}
"./test": "./dist/test/index.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./test/*": "./dist/test/*"
},
"license": "ISC",
"include": [
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import { BrowserRouter as Router } from 'react-router-dom'
import { render, RenderResult, cleanup } from '@testing-library/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { beforeAll, afterEach, afterAll } from 'vitest'
import { Query } from '../../queries'
import { Query } from 'wasp/rpc'
import config from 'wasp/core/config'
import { HttpMethod, Route } from 'wasp/types'

8 changes: 8 additions & 0 deletions waspc/data/Generator/templates/sdk/test/vitest/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { afterEach } from 'vitest'
infomiho marked this conversation as resolved.
Show resolved Hide resolved
import { cleanup } from '@testing-library/react'
import '@testing-library/jest-dom/vitest'

// runs a clean after each test case (e.g. clearing jsdom)
afterEach(() => {
cleanup();
})
3 changes: 2 additions & 1 deletion waspc/data/Generator/templates/sdk/tsconfig.json
Original file line number Diff line number Diff line change
@@ -20,7 +20,8 @@
"esModuleInterop": true,
"moduleResolution": "node",
"outDir": "dist",
"allowJs": true
"allowJs": true,
"types": ["@testing-library/jest-dom"],
Copy link
Contributor Author

@infomiho infomiho Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were type errors related to the unit test matchers e.g. toBeInTheDocument was reported as undefined. Since we are copying the ext-src to sdk we also need these types here.

// todo(filip): Only works with common js, see https://www.typescriptlang.org/tsconfig#paths and daily-article.
// "paths": {
// "@wasp/*": [
2 changes: 1 addition & 1 deletion waspc/examples/todo-typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/.wasp/
/.env.server
/.env.client
/node_modules/
node_modules/
Copy link
Contributor Author

@infomiho infomiho Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vitest creates some cache files in the src dir and it does it in node_modules/.vitest

12 changes: 10 additions & 2 deletions waspc/examples/todo-typescript/.wasp/out/sdk/wasp/package.json
Original file line number Diff line number Diff line change
@@ -49,7 +49,9 @@
"./server/utils": "./dist/server/utils.js",
"./server/actions": "./dist/server/actions/index.js",
"./server/queries": "./dist/server/queries/index.js",
"./dbSeed/types": "./dist/dbSeed/types.js"
"./dbSeed/types": "./dist/dbSeed/types.js",
"./test": "./dist/test/index.js",
"./test/*": "./dist/test/*"
},
"license": "ISC",
"include": [
@@ -71,7 +73,13 @@
"@types/express-serve-static-core": "^4.17.13",
"@stitches/react": "^1.2.8",
"lucia": "^3.0.0-beta.14",
"@lucia-auth/adapter-prisma": "^4.0.0-beta.9"
"@lucia-auth/adapter-prisma": "^4.0.0-beta.9",
"vitest": "^1.2.1",
"@vitest/ui": "^1.2.1",
"jsdom": "^21.1.1",
"@testing-library/react": "^14.1.2",
"@testing-library/jest-dom": "^6.3.0",
"msw": "^1.1.0"
},
"devDependencies": {"@tsconfig/node18": "latest"
}
Loading