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
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
11 changes: 7 additions & 4 deletions waspc/cli/src/Wasp/Cli/Command/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ import Wasp.Cli.Command.Message (cliSendMessageC)
import Wasp.Cli.Command.Require (InWaspProject (InWaspProject), require)
import Wasp.Cli.Command.Watch (watch)
import qualified Wasp.Generator
import Wasp.Generator.Common (ProjectRootDir)
import qualified Wasp.Message as Msg
import Wasp.Project.Common (dotWaspDirInWaspProjectDir, generatedCodeDirInDotWaspDir)
import Wasp.Project.Common
( WaspProjectDir,
dotWaspDirInWaspProjectDir,
generatedCodeDirInDotWaspDir,
)

test :: [String] -> Command ()
test [] = throwError $ CommandError "Not enough arguments" "Expected: wasp test client <args>"
test ("client" : args) = watchAndTest $ Wasp.Generator.testWebApp args
test ("server" : _args) = throwError $ CommandError "Invalid arguments" "Server testing not yet implemented."
test _ = throwError $ CommandError "Invalid arguments" "Expected: wasp test client <args>"

watchAndTest :: (Path' Abs (Dir ProjectRootDir) -> IO (Either String ())) -> Command ()
watchAndTest :: (Path' Abs (Dir WaspProjectDir) -> IO (Either String ())) -> Command ()
watchAndTest testRunner = do
InWaspProject waspRoot <- require
let outDir = waspRoot </> dotWaspDirInWaspProjectDir </> generatedCodeDirInDotWaspDir
Expand All @@ -39,7 +42,7 @@ watchAndTest testRunner = do
watchOrStartResult <- liftIO $ do
ongoingCompilationResultMVar <- newMVar (warnings, [])
let watchWaspProjectSource = watch waspRoot outDir ongoingCompilationResultMVar
watchWaspProjectSource `race` testRunner outDir
watchWaspProjectSource `race` testRunner waspRoot
Copy link
Contributor Author

@infomiho infomiho Jan 25, 2024

Choose a reason for hiding this comment

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

Instead of running from the .wasp/out dir, it now runs from the Wasp project root dir.


case watchOrStartResult of
Left () -> error "This should never happen, listening for file changes should never end but it did."
Expand Down
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();
})
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/react-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"allowJs": true,
"strict": false,
// Allow importing pages with the .tsx extension.
"allowImportingTsExtensions": true
"allowImportingTsExtensions": true,
},
"include": [
"src"
Expand Down
13 changes: 9 additions & 4 deletions waspc/data/Generator/templates/react-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// <reference types="vitest" />
import { mergeConfig } from "vite";
import react from "@vitejs/plugin-react";
import { defaultExclude } from "vitest/config"

{=# customViteConfig.isDefined =}
// Ignoring the TS error because we are importing a file outside of TS root dir.
Expand Down Expand Up @@ -29,12 +30,16 @@ const defaultViteConfig = {
outDir: "build",
},
test: {
globals: true,
environment: "jsdom",
setupFiles: ["./src/test/vitest/setup.ts"],
// vitest is running from the root of the project, so we need
// to specify the path to the setup file relative to the root.
setupFiles: {=& vitest.setupFilesArray =},
exclude: [
...defaultExclude,
"{= vitest.excludeWaspArtefactsPattern =}",
]
},
// resolve: {
// dedupe: ["react", "react-dom"],
// },
};

// https://vitejs.dev/config/
Expand Down
3 changes: 3 additions & 0 deletions waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
{=! Used by users, documented. =}
"./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/*.js",
"./crud/*": "./dist/crud/*.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./server/crud/*": "./dist/server/crud/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
8 changes: 7 additions & 1 deletion waspc/data/Generator/templates/sdk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
"esModuleInterop": true,
"moduleResolution": "node",
"outDir": "dist",
"allowJs": true
"allowJs": true,
"types": [
// This is needed to properly support Vitest testing with jest-dom matchers.
// Types for jest-dom are not recognized automatically and Typescript complains
// about missing types e.g. when using `toBeInTheDocument` and other matchers.
"@testing-library/jest-dom"
],
// todo(filip): Only works with common js, see https://www.typescriptlang.org/tsconfig#paths and daily-article.
// "paths": {
// "@wasp/*": [
Expand Down
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

10 changes: 9 additions & 1 deletion waspc/examples/todo-typescript/.wasp/out/sdk/wasp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
"./server/queries": "./dist/server/queries/index.js",
"./server/auth/email": "./dist/server/auth/email/index.js",
"./dbSeed/types": "./dist/dbSeed/types.js",
"./test": "./dist/test/index.js",
"./test/*": "./dist/test/*.js",
"./crud/*": "./dist/crud/*.js",
"./server/crud/*": "./dist/server/crud/*",
"./email": "./dist/email/index.js",
Expand Down Expand Up @@ -96,7 +98,13 @@
"@lucia-auth/adapter-prisma": "^4.0.0-beta.9",
"socket.io": "^4.6.1",
"socket.io-client": "^4.6.1",
"@socket.io/component-emitter": "^4.0.0"
"@socket.io/component-emitter": "^4.0.0",
"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"
}
Expand Down
Loading
Loading