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

Move server tests to Jest #177

Closed
wants to merge 2 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 jobrunner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"typecheck": "tsc --noEmit",
"build": "node esbuild.config.mjs",
"lint": "eslint src/**/*.ts",
"test": "vitest run --coverage"
"test": "vitest run --coverage",
"test:integration": "TEST_INTEGRATION=true vitest run --coverage --config ./vitest.config-integration.mts"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.374.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ export default defineConfig({
enabled: true,
provider: "v8",
all: true,
include: [
"server/app/**",
"server/lib/**",
"server/components/**",
"server/middleware.ts",
"jobrunner/src/**",
"utility/**",
],
include: ["jobrunner/src/**", "utility/**"],
// Prisma client ships with broken sourcemaps, so exclude it
exclude: [
"**/prisma/client/runtime/*",
Expand Down
5 changes: 0 additions & 5 deletions jobrunner/vitest.config-integration.ts

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"lint": "yarn workspaces foreach -p --exclude bowser-root-workspace run lint",
"prettify": "yarn workspaces foreach -p --exclude bowser-root-workspace run prettify",
"test": "yarn workspaces foreach --exclude bowser-root-workspace run test",
"test:integration": "yarn workspaces foreach --exclude bowser-root-workspace run test:integration",
"typecheck": "yarn workspaces foreach -p --exclude bowser-root-workspace run tsc --noEmit",
"prepare": "husky install"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`shows > listUpcoming > returns upcoming shows 1`] = `
exports[`shows listUpcoming returns upcoming shows 1`] = `
{
"id": Any<Number>,
"name": "Test Show",
Expand Down
1 change: 0 additions & 1 deletion server/app/api/__tests__/shows.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { integrate } from "@bowser/testing";
import { appRouter } from "../_router";
import { beforeEach, describe, expect, it } from "vitest";
import { db } from "@/lib/db";
import { add, sub } from "date-fns";

Expand Down
5 changes: 2 additions & 3 deletions server/app/shows/[show_id]/actions.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { db } from "@/lib/db";
import { test, vi, beforeEach, expect } from "vitest";
import { reorderShowItems } from "./actions";
import { integrate } from "@bowser/testing";

vi.mock("server-only", () => ({}));
vi.mock("next/cache", () => ({ revalidatePath: () => {} }));
jest.mock("server-only", () => ({}));
jest.mock("next/cache", () => ({ revalidatePath: () => {} }));

const TEST_TIME = new Date("2023-07-21T16:46:35.036Z");

Expand Down
22 changes: 22 additions & 0 deletions server/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable */
const nextJest = require("next/jest");

// Providing the path to your Next.js app which will enable loading next.config.js and .env files
const createJestConfig = nextJest.default({ dir: "./" });

// Any custom config you want to pass to Jest
/** @type {import("jest").Config} */
const customJestConfig = {
testPathIgnorePatterns: [
"<rootDir>/e2e",
process.env.TEST_INTEGRATION !== "true" && ".test.integration",
].filter(Boolean),
moduleNameMapper: {
"^@/(.+)$": "<rootDir>/$1",
// HACK
"../../client": "<rootDir>/../utility/prisma/client",
},
};

// createJestConfig is exported in this way to ensure that next/jest can load the Next.js configuration, which is async
module.exports = createJestConfig(customJestConfig);
5 changes: 4 additions & 1 deletion server/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { PrismaClient } from "@bowser/prisma/client";
import invariant from "@/lib/invariant";
import { PHASE_PRODUCTION_BUILD } from "next/constants";
import makeDebug from "debug";

const debug = makeDebug("db");

// Playwright can't handle the react-server import
if (process.env.E2E_TEST !== "true") {
Expand Down Expand Up @@ -40,7 +43,7 @@ if (process.env.NODE_ENV === "development") {
// @ts-expect-error This works, trust me.
prisma.$on("query", async (e) => {
// @ts-expect-error This works, trust me.
console.log(`${e.query}\n\t${e.params}`);
debug(`${e.query}\n\t${e.params}`);
});
} else {
// Next will execute these as part of the build and fail
Expand Down
1 change: 0 additions & 1 deletion server/lib/time.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe, test, expect } from "vitest";
import { formatDurationMS } from "@/lib/time";

describe("formatDurationMS", () => {
Expand Down
9 changes: 6 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"build": "next build",
"start": "next start",
"lint": "next lint -f stylish",
"test": "dotenv -e .env.test ../node_modules/.bin/vitest run --config ./vitest.config.ts",
"test:integration": "TEST_INTEGRATION=true dotenv -e .env.test ../node_modules/.bin/vitest run --config ./vitest.config-integration.ts",
"test:integration:watch": "TEST_INTEGRATION=true dotenv -e .env.test ../node_modules/.bin/vitest watch --config ./vitest.config-integration.ts",
"test": "dotenv -e .env.test ../node_modules/.bin/jest",
"test:integration": "TEST_INTEGRATION=true dotenv -e .env.test ../node_modules/.bin/jest --runInBand --testPathPattern '.*.integration'",
"test:integration:watch": "TEST_INTEGRATION=true dotenv -e .env.test ../node_modules/.bin/jest --watch --runInBand --testPathPattern '.*.integration'",
"test:e2e": "NODE_ENV=test E2E_TEST=true yarn playwright test",
"test:e2e:debug": "NODE_ENV=test E2E_TEST=true DEBUG='pw:browser*,pw:api*' yarn playwright test",
"test:e2e:ui": "NODE_ENV=test E2E_TEST=true yarn playwright test --ui --project=chromium",
Expand Down Expand Up @@ -43,6 +43,7 @@
"client-only": "^0.0.1",
"clsx": "^2.0.0",
"date-fns": "^2.30.0",
"debug": "^4.3.4",
"eslint": "8.45.0",
"eslint-config-next": "13.5.6",
"eslint-plugin-react": "latest",
Expand All @@ -69,11 +70,13 @@
"devDependencies": {
"@playwright/test": "^1.37.0",
"@prisma/nextjs-monorepo-workaround-plugin": "^5.5.2",
"@types/jest": "^29.5.9",
"@types/shelljs": "^0.8.12",
"@vitejs/plugin-react": "^4.0.3",
"autoprefixer": "^10.4.14",
"dotenv-cli": "^7.2.1",
"dotenv-flow": "^3.2.0",
"jest": "^29.7.0",
"jsdom": "^22.1.0",
"postcss": "^8.4.27",
"prettier": "^3.0.3",
Expand Down
6 changes: 4 additions & 2 deletions utility/testing/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { describe } from "vitest";
import { describe as describeVitest } from "vitest";

const realDescribe = typeof jest === "undefined" ? describeVitest : describe;

export const integrate =
process.env.TEST_INTEGRATION === "true" ? describe : describe.skip;
process.env.TEST_INTEGRATION === "true" ? realDescribe : realDescribe.skip;
1 change: 1 addition & 0 deletions utility/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"test": "vitest"
},
"dependencies": {
"@jest/globals": "^29.7.0",
"debug": "^4.3.4",
"p-event": "^6.0.0",
"ws": "^8.13.0"
Expand Down
Loading