项目作者: vangware

项目描述 :
✅ Equality test with enforced legibility
高级语言: TypeScript
项目地址: git://github.com/vangware/test.git
创建时间: 2020-07-18T00:32:59Z
项目社区:https://github.com/vangware/test

开源协议:MIT License

下载


This repository’s code was moved to lou.codes.


![Coverage][coverage-badge] ![License][license-badge]
![NPM Version][npm-version-badge] ![Open Issues][open-issues-badge]

✅ Equality test with enforced readability, based on the concept of
RITEway and inspired by uvu.

Usage

📦 Node

Install @vangware/tests as a dev dependency:

  1. pnpm add -D @vangware/test
  2. # or
  3. npm install -D @vangware/test
  4. # or
  5. yarn add --dev @vangware/test

Add a test script to package.json:

  1. {
  2. "scripts": {
  3. "test": "test"
  4. }
  5. }

Add TypeScript support

To support TypeScript, install tsx as a dev dependency:

bash pnpm add -D tsx # or npm install -D tsx # or yarn add --dev tsx

And update package.json:

json { "scripts": { "test": "NODE_OPTIONS='--loader tsx --no-warnings' test" } }


Add coverage

To add coverage, install c8 as a dev dependency:

bash pnpm add -D c8 # or npm install -D c8 # or yarn add --dev c8

And update package.json:

json { "scripts": { "test": "c8 test" } }

If you added TypeScript support, then update package.json like this instead:

And update package.json:

json { "scripts": { "test": "NODE_OPTIONS='--loader tsx --no-warnings' c8 test" } }

Run tests:

  1. pnpm test
  2. # or
  3. npm test
  4. # or
  5. yarn test

🦕 Deno

Import @vangware/test using the npm: prefix, and use it directly:

  1. import { test } from "npm:@vangware/test";
  2. import { add } from "../src/add.js";
  3. test({
  4. given: "a 1 and a 2",
  5. must: "return 3",
  6. received: () => add(2)(1),
  7. wanted: () => 3,
  8. }).then(console.log);

🌎 Browser

Import @vangware/test using esm.sh, and use it directly:

  1. import { test } from "https://esm.sh/@vangware/test";
  2. import { add } from "../src/add.js";
  3. test({
  4. given: "a 1 and a 2",
  5. must: "return 3",
  6. received: () => add(2)(1),
  7. wanted: () => 3,
  8. }).then(console.log);

Writing tests

TypeScript

  1. import type { Tests } from "@vangware/test";
  2. import { add } from "../src/add.js";
  3. export default [
  4. {
  5. given: "a 1 and a 2",
  6. must: "return 3",
  7. received: () => add(2)(1),
  8. wanted: () => 3,
  9. },
  10. {
  11. given: "a 1 and a -2",
  12. must: "return -1",
  13. received: () => add(-2)(1),
  14. wanted: () => -1,
  15. },
  16. ] satisfies Tests<number>;

JavaScript

  1. import { add } from "../src/add.js";
  2. /** @satisfies {import("@vangware/test").Tests<number>} */
  3. export default [
  4. {
  5. given: "a 1 and a 2",
  6. must: "return 3",
  7. received: () => add(2)(1),
  8. wanted: () => 3,
  9. },
  10. {
  11. given: "a 1 and a -2",
  12. must: "return -1",
  13. received: () => add(-2)(1),
  14. wanted: () => -1,
  15. },
  16. ];

Other alternatives

Instead of exporting an Array of Test as default, the export can also be a
single Test:

  1. import type { Test } from "@vangware/test";
  2. import { add } from "../src/add.js";
  3. export default {
  4. given: "a 1 and a 2",
  5. must: "return 3",
  6. received: () => add(2)(1),
  7. wanted: () => 3,
  8. } satisfies Test<number>;

Or multiple exports with different tests:

  1. import type { Test } from "@vangware/test";
  2. import { add } from "../src/add.js";
  3. export const test1: Test<number> = {
  4. given: "a 1 and a 2",
  5. must: "return 3",
  6. received: () => add(2)(1),
  7. wanted: () => 3,
  8. };
  9. export const test2: Test<number> = {
  10. given: "a 1 and a -2",
  11. must: "return -1",
  12. received: () => add(-2)(1),
  13. wanted: () => -1,
  14. };

It can also be used directly without the test bin by importing the different
utils directly (like with the Deno and Browser examples above):

  1. import { test } from "@vangware/test";
  2. import { customFormatter } from "./customFormatter.js";
  3. test({
  4. given: "a 1 and a 2",
  5. must: "return 3",
  6. received: () => add(2)(1),
  7. wanted: () => 3,
  8. }).then(customFormatter);

Default output

@vangware/tests provides a default output for the tests. It looks like this:

  1. [TEST] ./tests/example.test.ts
  2. [FAIL] Given a 1 and a 2, must return 3, but...
  3. it has the wrong value. Wanted 3 but received 4.

And if the wanted/received type is more complex, like an object, then the output
goes into details about the error:

  1. [TEST] ./tests/example.test.ts
  2. [FAIL] Given an object, must add a single property, but...
  3. foo.bar has the wrong value. Wanted 1 but received 2.
  4. foo.baz.1 is missing.
  5. bar was set with the value "bar".

But developers can choose to run test directly and use their own formatter, as
it was pointed out in the previous section.

[coverage-badge]:
https://img.shields.io/coveralls/github/vangware/test.svg?style=for-the-badge&labelColor=666&color=0a8&link=https://coveralls.io/github/vangware/test

[license-badge]:
@vangware/test.svg?style=for-the-badge&labelColor=666&color=0a8&link=https://github.com/vangware/test/blob/main/LICENSE"">https://img.shields.io/npm/l/@vangware/test.svg?style=for-the-badge&labelColor=666&color=0a8&link=https://github.com/vangware/test/blob/main/LICENSE
[npm-version-badge]:
@vangware/test.svg?style=for-the-badge&labelColor=666&color=0a8&link=https://npm.im/@vangware/test"">https://img.shields.io/npm/v/@vangware/test.svg?style=for-the-badge&labelColor=666&color=0a8&link=https://npm.im/@vangware/test
[open-issues-badge]:
https://img.shields.io/github/issues/vangware/test.svg?style=for-the-badge&labelColor=666&color=0a8&link=https://github.com/vangware/test/issues