Skip to content

Commit

Permalink
chore: added preliminary internal unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed May 6, 2023
1 parent 8e33320 commit 31fc3a3
Show file tree
Hide file tree
Showing 13 changed files with 675 additions and 53 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Format

on:
push:
branches:
- main
pull_request:

jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/prepare
- run: yarn test
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"configurations": [
{
"args": ["run", "${relativeFile}"],
"autoAttachChildProcesses": true,
"console": "integratedTerminal",
"name": "Debug Current Test File",
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
"request": "launch",
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"smartStep": true,
"type": "node"
}
],
"version": "0.2.0"
}
16 changes: 16 additions & 0 deletions docs/Development.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,19 @@ The function should return an object with:
- `restore`: Restores the original method on the container.

See [`src/spies/jest.ts`](../src/spies/jest.ts) as an example.

## Unit Tests

This repository uses [Vitest](https://vitest.dev) for a small amount of unit test coverage.
You can run it locally on the command-line:

```shell
pnpm run test
```

Test environments and spy frameworks don't need to be unit tested just yet.

### Debugging Tests

This repository includes a [VS Code launch configuration](https://code.visualstudio.com/docs/editor/debugging) for debugging unit tests.
To launch it, open a test file, then run _Debug Current Test File_ from the VS Code Debug panel (or press F5).
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"pretty-quick": "^3.1.3",
"typescript": "^4.7.4"
"typescript": "^4.7.4",
"vitest": "^0.31.0"
},
"main": "src/index.js",
"husky": {
Expand Down Expand Up @@ -43,7 +44,8 @@
"lint": "yarn eslint \"src/*.ts\" --max-warnings 0 --report-unused-disable-directives",
"lint:md": "yarn markdownlint \"**/*.md\" \".github/**/*.md\"",
"lint:spelling": "cspell \"**\" \".github/**/*\"",
"prepare": "husky install"
"prepare": "husky install",
"test": "vitest"
},
"version": "0.2.1"
}
49 changes: 0 additions & 49 deletions src/complaining.ts

This file was deleted.

24 changes: 24 additions & 0 deletions src/complaining/createComplaint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { TestComplaint } from "../environments/testEnvironmentTypes";
import { SpyCallArgs } from "../spies/spyTypes";
import { formatMethodComplaint } from "./formatMethodComplaint";

export const createComplaint = (
methodsWithCalls: [keyof Console, SpyCallArgs[]][],
): TestComplaint => {
const methodComplaints = methodsWithCalls.map(formatMethodComplaint).join("\n");
const s = methodsWithCalls.length === 1 ? "" : "s";

// It looks like something wrote to the console during your test!
// Put a breakpoint on this line and check the methodsWithCalls variable to see details.
const error = new Error(
`Oh no! Your test called the following console method${s}:\n${methodComplaints}`,
);

return {
error,
methodComplaints: methodsWithCalls.map(([methodName, methodCalls]) => ({
methodCalls,
methodName,
})),
};
};
16 changes: 16 additions & 0 deletions src/complaining/formatComplaintCall.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { formatComplaintCall } from "./formatComplaintCall";

describe("formatComplaintCall", () => {
it.each([
[[false], "false"],
[[true], "true"],
[[{}], "{}"],
[[{ inner: {} }], '{"inner":{}}'],
[[1, "two", [3]], '1, "two", [3]'],
])(`formats %p as %p`, (args, expected) => {
const actual = formatComplaintCall(args);

expect(actual).toBe(expected);
});
});
8 changes: 8 additions & 0 deletions src/complaining/formatComplaintCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { SpyCallArgs } from "../spies/spyTypes";

const formatComplaintLineArg = (arg: unknown) => {
return JSON.stringify(arg) || JSON.stringify(`${arg}`);
};

export const formatComplaintCall = (call: SpyCallArgs) =>
call.map(formatComplaintLineArg).join(", ");
21 changes: 21 additions & 0 deletions src/complaining/formatMethodComplaint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { SpyCallArgs } from "../spies/spyTypes";
import { formatComplaintCall } from "./formatComplaintCall";

const lineThreshold = 3;

export const formatMethodComplaint = ([methodName, calls]: [keyof Console, SpyCallArgs[]]) => {
const summary = ` * ${methodName} (${calls.length} call${calls.length === 1 ? "" : "s"})`;

const lines = calls
.slice(0, Math.min(calls.length, lineThreshold))
.map(formatComplaintLineWithIndex);

if (calls.length > lineThreshold) {
lines.push(`...${calls.length - lineThreshold} more`);
}

return `${summary}\n${lines.map((line) => ` > Call ${line}`).join("\n")}`;
};

const formatComplaintLineWithIndex = (call: SpyCallArgs, i: number) =>
`${i}: ${formatComplaintCall(call)}`;
3 changes: 3 additions & 0 deletions src/complaining/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { createComplaint } from "./createComplaint";
export { formatComplaintCall } from "./formatComplaintCall";
export { formatMethodComplaint } from "./formatMethodComplaint";
9 changes: 9 additions & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("vitest/config");
exports.default = (0, config_1.defineConfig)({
test: {
clearMocks: true,
exclude: ["lib", "node_modules", "src/**/*.js", "src/*/*.js"],
},
});
8 changes: 8 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
clearMocks: true,
exclude: ["lib", "node_modules", "src/**/*.js", "src/*/*.js"],
},
});
Loading

0 comments on commit 31fc3a3

Please sign in to comment.