-
-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch from jest to vitest in new app templates (#3985)
* Switch from jest to vitest in new app templates * Finish vitest setup * Handle vitest.config.js vs vitest.config.ts * Add proper vitest config to js templates * Add changeset * Update READMEs in new app templates * Fix tests after vitest upgrade * Update spyOn references in tests
- Loading branch information
Showing
37 changed files
with
1,542 additions
and
359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@blitzjs/rpc": patch | ||
"@blitzjs/generator": patch | ||
--- | ||
|
||
Switch from jest to vitest in new app templates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
apps/toolkit-app/src/auth/mutations/forgotPassword.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { vi, describe, it, beforeEach } from "vitest" | ||
import db from "db" | ||
import { hash256 } from "@blitzjs/auth" | ||
import forgotPassword from "./forgotPassword" | ||
import previewEmail from "preview-email" | ||
import { Ctx } from "@blitzjs/next" | ||
|
||
beforeEach(async () => { | ||
await db.$reset() | ||
}) | ||
|
||
const generatedToken = "plain-token" | ||
vi.mock("@blitzjs/auth", async () => { | ||
const auth = await vi.importActual<Record<string, unknown>>("@blitzjs/auth")! | ||
return { | ||
...auth, | ||
generateToken: () => generatedToken, | ||
} | ||
}) | ||
|
||
vi.mock("preview-email", () => ({ default: vi.fn() })) | ||
|
||
describe("forgotPassword mutation", () => { | ||
it("does not throw error if user doesn't exist", async () => { | ||
await expect(forgotPassword({ email: "[email protected]" }, {} as Ctx)).resolves.not.toThrow() | ||
}) | ||
|
||
it("works correctly", async () => { | ||
// Create test user | ||
const user = await db.user.create({ | ||
data: { | ||
email: "[email protected]", | ||
tokens: { | ||
// Create old token to ensure it's deleted | ||
create: { | ||
type: "RESET_PASSWORD", | ||
hashedToken: "token", | ||
expiresAt: new Date(), | ||
sentTo: "[email protected]", | ||
}, | ||
}, | ||
}, | ||
include: { tokens: true }, | ||
}) | ||
|
||
// Invoke the mutation | ||
await forgotPassword({ email: user.email }, {} as Ctx) | ||
|
||
const tokens = await db.token.findMany({ where: { userId: user.id } }) | ||
const token = tokens[0] | ||
if (!user.tokens[0]) throw new Error("Missing user token") | ||
if (!token) throw new Error("Missing token") | ||
|
||
// delete's existing tokens | ||
expect(tokens.length).toBe(1) | ||
|
||
expect(token.id).not.toBe(user.tokens[0].id) | ||
expect(token.type).toBe("RESET_PASSWORD") | ||
expect(token.sentTo).toBe(user.email) | ||
expect(token.hashedToken).toBe(hash256(generatedToken)) | ||
expect(token.expiresAt > new Date()).toBe(true) | ||
expect(previewEmail).toBeCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { vi, describe, it, beforeEach, expect } from "vitest" | ||
import resetPassword from "./resetPassword" | ||
import db from "db" | ||
import { SecurePassword, hash256 } from "@blitzjs/auth" | ||
|
||
beforeEach(async () => { | ||
await db.$reset() | ||
}) | ||
|
||
const mockCtx: any = { | ||
session: { | ||
$create: vi.fn(), | ||
}, | ||
} | ||
|
||
describe("resetPassword mutation", () => { | ||
it("works correctly", async () => { | ||
expect(true).toBe(true) | ||
|
||
// Create test user | ||
const goodToken = "randomPasswordResetToken" | ||
const expiredToken = "expiredRandomPasswordResetToken" | ||
const future = new Date() | ||
future.setHours(future.getHours() + 4) | ||
const past = new Date() | ||
past.setHours(past.getHours() - 4) | ||
|
||
const user = await db.user.create({ | ||
data: { | ||
email: "[email protected]", | ||
tokens: { | ||
// Create old token to ensure it's deleted | ||
create: [ | ||
{ | ||
type: "RESET_PASSWORD", | ||
hashedToken: hash256(expiredToken), | ||
expiresAt: past, | ||
sentTo: "[email protected]", | ||
}, | ||
{ | ||
type: "RESET_PASSWORD", | ||
hashedToken: hash256(goodToken), | ||
expiresAt: future, | ||
sentTo: "[email protected]", | ||
}, | ||
], | ||
}, | ||
}, | ||
include: { tokens: true }, | ||
}) | ||
|
||
const newPassword = "newPassword" | ||
|
||
// Non-existent token | ||
await expect( | ||
resetPassword({ token: "no-token", password: "", passwordConfirmation: "" }, mockCtx) | ||
).rejects.toThrowError() | ||
|
||
// Expired token | ||
await expect( | ||
resetPassword( | ||
{ token: expiredToken, password: newPassword, passwordConfirmation: newPassword }, | ||
mockCtx | ||
) | ||
).rejects.toThrowError() | ||
|
||
// Good token | ||
await resetPassword( | ||
{ token: goodToken, password: newPassword, passwordConfirmation: newPassword }, | ||
mockCtx | ||
) | ||
|
||
// Delete's the token | ||
const numberOfTokens = await db.token.count({ where: { userId: user.id } }) | ||
expect(numberOfTokens).toBe(0) | ||
|
||
// Updates user's password | ||
const updatedUser = await db.user.findFirst({ where: { id: user.id } }) | ||
expect(await SecurePassword.verify(updatedUser!.hashedPassword, newPassword)).toBe( | ||
SecurePassword.VALID | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,32 @@ | ||
import { useCurrentUser } from "src/users/hooks/useCurrentUser" | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
|
||
import { expect, vi, test } from "vitest" | ||
import { render } from "test/utils" | ||
|
||
import Home from "../src/pages/index" | ||
|
||
jest.mock("src/users/hooks/useCurrentUser") | ||
const mockUseCurrentUser = useCurrentUser as jest.MockedFunction<typeof useCurrentUser> | ||
vi.mock("public/logo.png", () => ({ | ||
default: { src: "/logo.png" }, | ||
})) | ||
|
||
describe("renders blitz documentation link", () => { | ||
it("test", () => { | ||
// This is an example of how to ensure a specific item is in the document | ||
// But it's disabled by default (by test.skip) so the test doesn't fail | ||
// when you remove the the default content from the page | ||
test.skip("renders blitz documentation link", () => { | ||
// This is an example of how to ensure a specific item is in the document | ||
// But it's disabled by default (by test.skip) so the test doesn't fail | ||
// when you remove the the default content from the page | ||
|
||
// This is an example on how to mock api hooks when testing | ||
mockUseCurrentUser.mockReturnValue({ | ||
// This is an example on how to mock api hooks when testing | ||
vi.mock("src/users/hooks/useCurrentUser", () => ({ | ||
useCurrentUser: () => ({ | ||
id: 1, | ||
name: "User", | ||
email: "[email protected]", | ||
role: "user", | ||
}) | ||
}), | ||
})) | ||
|
||
const { getByText } = render(<Home />) | ||
const linkElement = getByText(/Documentation/i) | ||
expect(linkElement).toBeInTheDocument() | ||
}) | ||
const { getByText } = render(<Home />) | ||
const linkElement = getByText(/Documentation/i) | ||
expect(linkElement).toBeInTheDocument() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// This is the jest 'setupFilesAfterEnv' setup file | ||
// It's a good place to set globals, add global before/after hooks, etc | ||
import "@testing-library/jest-dom" | ||
|
||
export {} // so TS doesn't complain | ||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { defineConfig } from "vitest/config" | ||
|
||
import react from "@vitejs/plugin-react" | ||
import tsconfigPaths from "vite-tsconfig-paths" | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [react(), tsconfigPaths()], | ||
test: { | ||
dir: "./", | ||
globals: true, | ||
setupFiles: "./test/setup.ts", | ||
coverage: { | ||
reporter: ["text", "json", "html"], | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.