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

Feat japa testing #8

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
13 changes: 13 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@
"version": "detect"
}
}
},
// Test override
{
"files": ["**/*.spec.ts?(x)"],
"rules": {
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": true,
"optionalDependencies": false
}
]
}
}
],
"parserOptions": {
Expand Down
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@
// see: https://github.com/prettier/prettier-vscode/issues/3040#issuecomment-1793748475
"prettier.prettierPath": "./node_modules/prettier/index.cjs",

"japa.tests.npmScript": "test:unit -- --log-prefix=none",
"japa.tests.filePattern": "**/*.spec.{ts,tsx}",

"material-icon-theme.folders.associations": {
"components/primitives": "Delta",
".turbo": "Archive"
},
"material-icon-theme.files.associations": {
"*.css.ts": "Css",
"japa.unit.ts": "Test-ts",
"tsconfig.react.json": "Tsconfig"
},

Expand Down
18 changes: 18 additions & 0 deletions apps/node-sample/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://json.schemastore.org/eslintrc",
"extends": ["../../.eslintrc"],
"overrides": [
{
"files": ["japa.unit.ts"],
"rules": {
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": true,
"optionalDependencies": false
}
]
}
}
]
}
1 change: 1 addition & 0 deletions apps/node-sample/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/coverage
11 changes: 11 additions & 0 deletions apps/node-sample/japa.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect } from "@japa/expect";
import { configure, processCLIArgs, run } from "@japa/runner";

processCLIArgs(process.argv.splice(2));
configure({
files: ["src/**/*.spec.ts?(x)"],
importer: (filePath) => import(filePath.toString()),
plugins: [expect()],
});

await run();
8 changes: 6 additions & 2 deletions apps/node-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@
"lint:fix:format": "prettier --write --list-different ./",
"lint:fix:semantic": "eslint ./ --fix",
"test": "pnpm test:unit",
"test:unit": "echo \"Warning: no test specified\" && exit 0"
"test:unit": "c8 --all --include src tsx japa.unit.ts"
},
"dependencies": {
"@pnpm-monorepo/sample-lib": "workspace:*"
},
"devDependencies": {
"eslint": "^8.52.0",
"@japa/expect": "3.0.0",
"@japa/runner": "3.0.5",
"c8": "^8.0.1",
"concurrently": "^8.2.2",
"eslint": "^8.52.0",
"prettier": "^3.0.3",
"rimraf": "^5.0.5",
"tsx": "^3.14.0",
"typescript": "^5.2.2"
}
}
7 changes: 7 additions & 0 deletions apps/node-sample/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from "@japa/runner";

import { sampled } from "./index.js";

test("return imported package name", ({ expect }) => {
expect(sampled).toStrictEqual({ pkg: "@pnpm-monorepo/sample-lib" });
});
2 changes: 1 addition & 1 deletion apps/node-sample/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { sample } from "@pnpm-monorepo/sample-lib";

sample() satisfies { pkg: "sample" };
export const sampled = sample() satisfies { pkg: "@pnpm-monorepo/sample-lib" };
1 change: 1 addition & 0 deletions apps/node-sample/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["**/*.spec.ts", "**/*.spec.tsx"],
"compilerOptions": {
"noEmit": false,
"outDir": "build",
Expand Down
2 changes: 1 addition & 1 deletion apps/vite-sample/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"extends": ["../../.eslintrc"],
"overrides": [
{
"files": ["**/vite.config.ts"],
"files": ["japa.unit.ts", "vite.config.ts"],
"rules": {
"import/no-extraneous-dependencies": [
"error",
Expand Down
1 change: 1 addition & 0 deletions apps/vite-sample/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/coverage
16 changes: 16 additions & 0 deletions apps/vite-sample/japa.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from "@japa/expect";
import { configure, processCLIArgs, run } from "@japa/runner";

processCLIArgs(process.argv.splice(2));
configure({
files: ["src/**/*.spec.ts?(x)"],
importer: (filePath) => import(filePath.toString()),
plugins: [
expect(),
function jsDomPlugin() {
import("global-jsdom/register");
},
],
});

await run();
14 changes: 11 additions & 3 deletions apps/vite-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"lint:fix:format": "prettier --write --list-different ./",
"lint:fix:semantic": "eslint ./ --fix",
"test": "pnpm test:unit",
"test:unit": "echo \"Warning: no test specified\" && exit 0"
"test:unit": "c8 --all --include src vite-node japa.unit.ts"
},
"dependencies": {
"@pnpm-monorepo/react-sample-lib": "workspace:*",
Expand All @@ -28,14 +28,22 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@japa/expect": "3.0.0",
"@japa/runner": "3.0.5",
"@testing-library/react": "^14.0.0",
"@types/node": "^20.8.10",
"@types/react": "^18.2.28",
"@types/react-dom": "^18.2.13",
"@vanilla-extract/vite-plugin": "^3.9.0",
"@vitejs/plugin-react-swc": "^3.3.2",
"eslint": "^8.52.0",
"c8": "^8.0.1",
"concurrently": "^8.2.2",
"eslint": "^8.52.0",
"global-jsdom": "^9.1.0",
"jsdom": "^22.1.0",
"prettier": "^3.0.3",
"typescript": "^5.2.2",
"vite": "^4.4.5"
"vite": "^4.4.5",
"vite-node": "^0.34.6"
}
}
16 changes: 16 additions & 0 deletions apps/vite-sample/src/App.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { test } from "@japa/runner";
import { fireEvent, render } from "@testing-library/react";

import { App } from "./App.js";

test.group("App", () => {
test("update counter on click", ({ expect }) => {
const result = render(<App />);
const button = result.getByRole("button");

expect(button.textContent).toBe("count is 0");
fireEvent.click(button);

expect(button.textContent).toBe("count is 1");
});
});
1 change: 1 addition & 0 deletions apps/vite-sample/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["**/*.spec.ts", "**/*.spec.tsx"],
"compilerOptions": {
"noEmit": false,
"outDir": "build",
Expand Down
8 changes: 0 additions & 8 deletions apps/vite-sample/tsconfig.node.json

This file was deleted.

1 change: 1 addition & 0 deletions apps/vite-sample/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "global-jsdom/register";
18 changes: 18 additions & 0 deletions libs/react-sample/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://json.schemastore.org/eslintrc",
"extends": ["../../.eslintrc"],
"overrides": [
{
"files": ["japa.unit.ts"],
"rules": {
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": true,
"optionalDependencies": false
}
]
}
}
]
}
1 change: 1 addition & 0 deletions libs/react-sample/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/coverage
20 changes: 20 additions & 0 deletions libs/react-sample/japa.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect } from "@japa/expect";
import { configure, processCLIArgs, run } from "@japa/runner";
import { setFileScope } from "@vanilla-extract/css/fileScope";

processCLIArgs(process.argv.splice(2));
configure({
files: ["src/**/*.spec.ts?(x)"],
importer: (filePath) => import(filePath.toString()),
plugins: [
expect(),
function vanillaExtractMockPlugin() {
setFileScope("mock");
},
function jsDomPlugin() {
import("global-jsdom/register");
},
],
});

await run();
10 changes: 9 additions & 1 deletion libs/react-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,29 @@
"lint:fix:format": "prettier --write --list-different ./",
"lint:fix:semantic": "eslint ./ --fix",
"test": "pnpm test:unit",
"test:unit": "echo \"Warning: no test specified\" && exit 0"
"test:unit": "c8 --all --include src tsx japa.unit.ts"
},
"peerDependencies": {
"@vanilla-extract/css": "^1.13.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@japa/expect": "3.0.0",
"@japa/runner": "3.0.5",
"@testing-library/react": "^14.0.0",
"@types/node": "^20.8.10",
"@types/react": "^18.2.28",
"@types/react-dom": "^18.2.13",
"@vanilla-extract/css": "^1.13.0",
"c8": "^8.0.1",
"concurrently": "^8.2.2",
"eslint": "^8.52.0",
"global-jsdom": "^9.1.0",
"jsdom": "^22.1.0",
"prettier": "^3.0.3",
"rimraf": "^5.0.5",
"tsx": "^3.14.0",
"typescript": "^5.2.2"
}
}
18 changes: 18 additions & 0 deletions libs/react-sample/src/components/custom/Button/Button.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { test } from "@japa/runner";
import { render } from "@testing-library/react";

import { Button } from "./Button.js";

test.group("Button", () => {
test("render button if href is undefined", ({ expect }) => {
expect(render(<Button />).container.firstChild?.nodeName).toStrictEqual(
"BUTTON",
);
});

test("return anchor if href is defined", ({ expect }) => {
expect(
render(<Button href="#" />).container.firstChild?.nodeName,
).toStrictEqual("A");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { test } from "@japa/runner";
import { render } from "@testing-library/react";

import { PressablePrimitive } from "./PressablePrimitive.js";

test.group("PressablePrimitive", () => {
test("render button if href is undefined", ({ expect }) => {
expect(
render(<PressablePrimitive />).container.firstChild?.nodeName,
).toStrictEqual("BUTTON");
});

test("return anchor if href is defined", ({ expect }) => {
expect(
render(<PressablePrimitive href="#" />).container.firstChild?.nodeName,
).toStrictEqual("A");
});
});
1 change: 1 addition & 0 deletions libs/react-sample/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["**/*.spec.ts", "**/*.spec.tsx"],
"compilerOptions": {
"noEmit": false,
"outDir": "build",
Expand Down
1 change: 1 addition & 0 deletions libs/react-sample/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "global-jsdom/register";
18 changes: 18 additions & 0 deletions libs/sample/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://json.schemastore.org/eslintrc",
"extends": ["../../.eslintrc"],
"overrides": [
{
"files": ["japa.unit.ts"],
"rules": {
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": true,
"optionalDependencies": false
}
]
}
}
]
}
1 change: 1 addition & 0 deletions libs/sample/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/coverage
11 changes: 11 additions & 0 deletions libs/sample/japa.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect } from "@japa/expect";
import { configure, processCLIArgs, run } from "@japa/runner";

processCLIArgs(process.argv.splice(2));
configure({
files: ["src/**/*.spec.ts?(x)"],
importer: (filePath) => import(filePath.toString()),
plugins: [expect()],
});

await run();
8 changes: 6 additions & 2 deletions libs/sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@
"lint:fix:format": "prettier --write --list-different ./",
"lint:fix:semantic": "eslint ./ --fix",
"test": "pnpm test:unit",
"test:unit": "echo \"Warning: no test specified\" && exit 0"
"test:unit": "c8 --all --include src tsx japa.unit.ts"
},
"devDependencies": {
"eslint": "^8.52.0",
"@japa/expect": "3.0.0",
"@japa/runner": "3.0.5",
"c8": "^8.0.1",
"concurrently": "^8.2.2",
"eslint": "^8.52.0",
"prettier": "^3.0.3",
"rimraf": "^5.0.5",
"tsx": "^3.14.0",
"typescript": "^5.2.2"
}
}
7 changes: 7 additions & 0 deletions libs/sample/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from "@japa/runner";

import { sample } from "./index.js";

test("return package name", ({ expect }) => {
expect(sample()).toStrictEqual({ pkg: "@pnpm-monorepo/sample-lib" });
});
Loading