@aljazmc/testero 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,30 +1,35 @@
1
1
  ![Custom](https://img.shields.io/badge/project_status-mostly_working-green) ![Custom](https://img.shields.io/badge/breaking_changes-common-red) ![NPM Downloads](https://img.shields.io/npm/dm/%40aljazmc%2Ftestero) ![GitHub package.json version](https://img.shields.io/github/package-json/v/aljazmc/aljazmc?filename=packages%2Ftestero%2Fpackage.json&color=green) ![GitHub License](https://img.shields.io/github/license/aljazmc/aljazmc)
2
2
 
3
- # > testero
3
+ # testero
4
4
 
5
- A simple, Jest-like test runner for JavaScript, TypeScript and JSX, compiling tests with esbuild. Easy to understand, easy to customize and easy to extend.
5
+ A simple, [Jest](https://jestjs.io)-like test runner for JavaScript,
6
+ TypeScript and JSX, compiling tests with [esbuild](https://esbuild.github.io).
7
+ Easy to understand, easy to customize and easy to extend.
6
8
 
7
- ## > Install
9
+ ## Install
8
10
 
9
11
  ```sh
10
12
  npm install --save-dev @aljazmc/testero
11
13
  ```
12
14
 
13
- ## > Usage
15
+ testero needs Node.js 22.18 or later, or 24.2 or later.
16
+
17
+ ## Usage
14
18
 
15
19
  ```sh
16
20
  testero # run every test file
17
- testero math unit/ # run test files whose path matches a pattern
18
- testero -t "adds numbers" # run tests whose full name matches
21
+ testero math unit/ # run the test files whose path matches a pattern
22
+ testero -t "adds numbers" # run the tests whose full name matches
19
23
  testero --verbose --bail # list every test, stop at the first failing file
20
24
  testero --listTests # print the test files instead of running them
21
25
  testero --config # write the defaults to testero.config.ts, --config js to testero.config.js
22
- testero --help
26
+ testero --help # show every option
23
27
  ```
24
28
 
25
- ## > Writing tests
29
+ ## Writing tests
26
30
 
27
- testero runs test files written in JavaScript, TypeScript or JSX, with the same globals as [Jest](https://jestjs.io):
31
+ Test files are written in JavaScript, TypeScript or JSX, with the same
32
+ globals as Jest:
28
33
 
29
34
  ```ts
30
35
  import { add } from "../src/math.ts";
@@ -51,47 +56,89 @@ describe("add", () => {
51
56
  });
52
57
  ```
53
58
 
54
- - `describe`, `test` and `it`, with `.only`, `.skip`, `.each` and `test.todo`, also `fdescribe`, `xdescribe`, `fit`, `xit` and `xtest`
55
- - `beforeAll`, `afterAll`, `beforeEach` and `afterEach`; tests and hooks may return promises or take a `done` callback, and time out
56
- - `expect` with Jest's matchers, `.not`, `.resolves`, `.rejects`, `expect.extend`, `expect.assertions` and `expect.hasAssertions`
57
- - asymmetric matchers: `expect.any`, `expect.anything`, `expect.objectContaining`, `expect.arrayContaining`, `expect.stringContaining`, `expect.stringMatching`, `expect.closeTo` and their `expect.not` variants
58
- - `testero.fn`, `testero.spyOn`, `testero.clearAllMocks`, `testero.resetAllMocks`, `testero.restoreAllMocks` and `testero.setTimeout`, also as `jest`; imports from `@jest/globals` get the same functions
59
+ | Globals | What they do |
60
+ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
61
+ | `describe`, `test`, `it` | Declare suites and tests, with `.only`, `.skip`, `.each` and `test.todo`. |
62
+ | `beforeAll`, `afterAll`, `beforeEach`, `afterEach` | Run code around the tests of a suite. |
63
+ | `expect` | Asserts with Jest's matchers, `.not`, `.resolves` and `.rejects`, and has `expect.extend`, `expect.assertions` and `expect.hasAssertions`. |
64
+ | `expect.any`, `expect.anything`, `expect.objectContaining`, `expect.arrayContaining`, `expect.stringContaining`, `expect.stringMatching`, `expect.closeTo` | Asymmetric matchers, also as `expect.not.*`. |
65
+ | `testero`, `jest` | Mocks: `fn`, `spyOn`, `clearAllMocks`, `resetAllMocks` and `restoreAllMocks`, and `setTimeout` for the tests of a file. |
66
+
67
+ `fdescribe`, `xdescribe`, `fit`, `xit` and `xtest` work too. Tests and
68
+ hooks may return promises or take a `done` callback, and they time out.
69
+ Imports from `@jest/globals` get the same functions.
59
70
 
60
- testero reports each failure with its message, a diff of the expected and received values, and the code where it happened.
71
+ Each failure is reported with its message, a diff of the expected and
72
+ received values, and the code where it happened.
61
73
 
62
- ## > Configuration
74
+ ### TypeScript
63
75
 
64
- testero reads `testero.config.ts` or `testero.config.js` from the current directory, with Jest's names and defaults. Objects are merged deeply into the defaults, anything else (like `ignore`) replaces them:
76
+ For the types of the globals, add `@aljazmc/testero/globals` to the
77
+ `types` of your `tsconfig.json`, instead of `@types/jest`:
78
+
79
+ ```json
80
+ {
81
+ "compilerOptions": {
82
+ "types": ["node", "@aljazmc/testero/globals"]
83
+ }
84
+ }
85
+ ```
86
+
87
+ ## Configuration
88
+
89
+ testero reads `testero.config.ts` or `testero.config.js` from the
90
+ current directory, with Jest's names and defaults. Objects are merged
91
+ deeply into the defaults, and anything else, like `ignore`, replaces
92
+ them:
65
93
 
66
94
  ```js
67
95
  export default {
68
96
  ignore: [".git", "node_modules"],
69
97
  rules: {
70
- // globs of test files in order, the last match decides, "!" excludes
71
- testMatch: [
72
- "**/__tests__/**/*.{js,jsx,ts,tsx,mjs,cjs,mts,cts}",
73
- "**/*.{spec,test}.{js,jsx,ts,tsx,mjs,cjs,mts,cts}",
74
- ],
75
- testTimeout: 5000, // milliseconds a test or hook may take
76
- bail: false, // stop after the first failing test file
77
- verbose: false, // list every test
78
- passWithNoTests: false, // succeed when there are no test files
79
- clearMocks: false, // clear the calls of every mock before each test
80
- resetMocks: false, // also remove their implementations
81
- restoreMocks: false, // also restore spies to the originals
98
+ testMatch: ["**/tests/**/*.test.ts"],
99
+ clearMocks: true,
82
100
  },
83
101
  };
84
102
  ```
85
103
 
86
- ## > How it works
104
+ `ignore` lists the files and folders that are never searched for tests.
105
+ The `rules` are:
106
+
107
+ | Option | Default | What it does |
108
+ | ----------------- | ----------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
109
+ | `testMatch` | `["**/__tests__/**/*.{js,jsx,ts,tsx,mjs,cjs,mts,cts}", "**/*.{spec,test}.{js,jsx,ts,tsx,mjs,cjs,mts,cts}"]` | Globs of the test files, in order. The last match decides, and `!` excludes. |
110
+ | `testTimeout` | `5000` | The milliseconds a test or hook may take. |
111
+ | `bail` | `false` | Stops after the first failing test file. |
112
+ | `verbose` | `false` | Lists every test, not only the failing ones. |
113
+ | `passWithNoTests` | `false` | Succeeds when there are no test files. |
114
+ | `clearMocks` | `false` | Clears the calls of every mock before each test. |
115
+ | `resetMocks` | `false` | Also removes their implementations. |
116
+ | `restoreMocks` | `false` | Also restores the original functions of spies. |
117
+
118
+ ## How it works
119
+
120
+ ```
121
+ test file
122
+ ├─> src/transformer.ts bundles it with esbuild, injecting the globals
123
+ ├─> src/collector.ts collects its suites and tests into a tree
124
+ ├─> src/runner.ts runs them, with hooks, timeouts and done callbacks
125
+ └─> src/reporter.ts prints the results like Jest does
126
+ ```
127
+
128
+ - `src/transformer.ts` bundles each test file and the local files it imports into one module with esbuild, the only dependency. Installed packages stay external, and the globals are injected where the file uses them.
129
+ - `src/collector.ts` holds the `describe`, `test` and hook functions, collecting what a test file declares into a tree of suites (`src/suite.ts`).
130
+ - `src/runner.ts` runs the tests of the tree in order, with focus, skips, hooks, timeouts and `done` callbacks.
131
+ - `src/expect` holds the `expect` function, its matchers, deep equality, and printing values with diffs.
132
+ - `src/mock.ts` holds mock functions and spies.
133
+ - `src/reporter.ts` prints the results like Jest's default reporter.
134
+ - `src/testero.ts` finds the test files and runs them one after another.
135
+ - `@aljazmc/config` (`shared/config`) loads `testero.config.ts`, finds the files and matches `testMatch`, and is shared with every package.
136
+
137
+ Each test file is bundled on its own, so it gets fresh copies of the
138
+ files it imports, and its spies are restored after it. Test files run
139
+ one after another in one process. There are no workers, module mocks,
140
+ fake timers, snapshots or coverage.
87
141
 
88
- - `src/transformer.ts`: bundles each test file and the local files it imports with [esbuild](https://esbuild.github.io), the only dependency, into one module; installed packages stay external and the globals are injected where the file uses them
89
- - `src/collector.ts`: the `describe`, `test` and hook functions, collecting what a test file declares into a tree of suites (`src/suite.ts`)
90
- - `src/runner.ts`: runs the tests of the tree in order, with focus, skips, hooks, timeouts and `done` callbacks
91
- - `src/expect`: the `expect` function, its matchers, deep equality and value printing with diffs
92
- - `src/mock.ts`: mock functions and spies
93
- - `src/reporter.ts`: prints results like Jest's default reporter
94
- - `src/testero.ts`: finds the test files and runs them one after another
95
- - `@aljazmc/config` (`shared/config`): loads `testero.config.ts`, finds the files and matches `testMatch`, shared with every package
142
+ ## License
96
143
 
97
- testero bundles each test file on its own, so it gets fresh copies of the files it imports, and spies are restored after it. Test files run one after another in one process, without workers, module mocking, fake timers, snapshots or coverage.
144
+ MIT, see [LICENSE](LICENSE).