@aljazmc/testero 0.3.0 → 0.3.2
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 +17 -7
- package/bin/main.js +27 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
    
|
|
2
2
|
|
|
3
3
|
# > testero
|
|
4
4
|
|
|
5
5
|
A simple, Jest-like test runner. Easy to understand, easy to customize and easy to extend.
|
|
6
6
|
|
|
7
|
-
## >
|
|
7
|
+
## > Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install --save-dev @aljazmc/testero
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## > Usage
|
|
8
14
|
|
|
9
15
|
```sh
|
|
10
16
|
testero # run every test file
|
|
@@ -16,7 +22,9 @@ testero --config # write the defaults to testero.config.ts, --config j
|
|
|
16
22
|
testero --help
|
|
17
23
|
```
|
|
18
24
|
|
|
19
|
-
|
|
25
|
+
## > Writing tests
|
|
26
|
+
|
|
27
|
+
testero runs test files written in JavaScript, TypeScript or JSX, with the same globals as [Jest](https://jestjs.io):
|
|
20
28
|
|
|
21
29
|
```ts
|
|
22
30
|
import { add } from "../src/math.ts";
|
|
@@ -49,9 +57,11 @@ describe("add", () => {
|
|
|
49
57
|
- asymmetric matchers: `expect.any`, `expect.anything`, `expect.objectContaining`, `expect.arrayContaining`, `expect.stringContaining`, `expect.stringMatching`, `expect.closeTo` and their `expect.not` variants
|
|
50
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
|
|
51
59
|
|
|
52
|
-
|
|
60
|
+
testero reports each failure with its message, a diff of the expected and received values, and the code where it happened.
|
|
61
|
+
|
|
62
|
+
## > Configuration
|
|
53
63
|
|
|
54
|
-
|
|
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:
|
|
55
65
|
|
|
56
66
|
```js
|
|
57
67
|
export default {
|
|
@@ -73,7 +83,7 @@ export default {
|
|
|
73
83
|
};
|
|
74
84
|
```
|
|
75
85
|
|
|
76
|
-
## >
|
|
86
|
+
## > How it works
|
|
77
87
|
|
|
78
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
|
|
79
89
|
- `src/collector.ts`: the `describe`, `test` and hook functions, collecting what a test file declares into a tree of suites (`src/suite.ts`)
|
|
@@ -84,4 +94,4 @@ export default {
|
|
|
84
94
|
- `src/testero.ts`: finds the test files and runs them one after another
|
|
85
95
|
- `@aljazmc/config` (`shared/config`): loads `testero.config.ts`, finds the files and matches `testMatch`, shared with every package
|
|
86
96
|
|
|
87
|
-
|
|
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.
|
package/bin/main.js
CHANGED
|
@@ -1667,6 +1667,8 @@ var Transformer = class _Transformer {
|
|
|
1667
1667
|
".mts": "ts",
|
|
1668
1668
|
".tsx": "tsx"
|
|
1669
1669
|
};
|
|
1670
|
+
/** The import.meta properties that describe the source file. */
|
|
1671
|
+
static #meta = ["url", "filename", "dirname"];
|
|
1670
1672
|
/** Marks the resolutions the plugin asks esbuild for itself. */
|
|
1671
1673
|
static #own = /* @__PURE__ */ Symbol("testero");
|
|
1672
1674
|
/** Where compiled test files go, stack frames there are testero's own. */
|
|
@@ -1691,6 +1693,12 @@ var Transformer = class _Transformer {
|
|
|
1691
1693
|
sourcemap: "inline",
|
|
1692
1694
|
sourcesContent: false,
|
|
1693
1695
|
inject: [this.#globals],
|
|
1696
|
+
define: Object.fromEntries(
|
|
1697
|
+
_Transformer.#meta.map((name) => [
|
|
1698
|
+
`import.meta.${name}`,
|
|
1699
|
+
`__testero.${name}`
|
|
1700
|
+
])
|
|
1701
|
+
),
|
|
1694
1702
|
// CommonJS code in the bundle may require installed packages
|
|
1695
1703
|
banner: {
|
|
1696
1704
|
js: 'import { createRequire as __testeroRequire } from "node:module";\nconst require = __testeroRequire(import.meta.url);'
|
|
@@ -1709,6 +1717,21 @@ var Transformer = class _Transformer {
|
|
|
1709
1717
|
return {
|
|
1710
1718
|
name: "testero",
|
|
1711
1719
|
setup: (build2) => {
|
|
1720
|
+
build2.onResolve({ filter: /^testero-meta:/ }, (args) => ({
|
|
1721
|
+
path: args.path.slice("testero-meta:".length),
|
|
1722
|
+
namespace: "testero-meta"
|
|
1723
|
+
}));
|
|
1724
|
+
build2.onLoad(
|
|
1725
|
+
{ filter: /.*/, namespace: "testero-meta" },
|
|
1726
|
+
(args) => ({
|
|
1727
|
+
contents: `export default ${JSON.stringify({
|
|
1728
|
+
url: pathToFileURL2(args.path).href,
|
|
1729
|
+
filename: args.path,
|
|
1730
|
+
dirname: dirname(args.path)
|
|
1731
|
+
})};`,
|
|
1732
|
+
loader: "js"
|
|
1733
|
+
})
|
|
1734
|
+
);
|
|
1712
1735
|
build2.onResolve({ filter: /^[^./]/ }, async (args) => {
|
|
1713
1736
|
if (args.pluginData === _Transformer.#own) return void 0;
|
|
1714
1737
|
if (_Transformer.modules.includes(args.path))
|
|
@@ -1731,15 +1754,10 @@ var Transformer = class _Transformer {
|
|
|
1731
1754
|
});
|
|
1732
1755
|
build2.onLoad({ filter: /\.[cm]?[jt]sx?$/ }, (args) => {
|
|
1733
1756
|
if (_Transformer.#installed(args.path)) return void 0;
|
|
1734
|
-
const
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
};
|
|
1739
|
-
const contents = readFileSync2(args.path, "utf8").replace(
|
|
1740
|
-
/\bimport\.meta\.(url|filename|dirname)\b/g,
|
|
1741
|
-
(_, name) => JSON.stringify(values[name])
|
|
1742
|
-
);
|
|
1757
|
+
const source = readFileSync2(args.path, "utf8");
|
|
1758
|
+
const contents = /\bimport\.meta\b/.test(source) ? `${source}
|
|
1759
|
+
import __testero from ${JSON.stringify(`testero-meta:${args.path}`)};
|
|
1760
|
+
` : source;
|
|
1743
1761
|
return {
|
|
1744
1762
|
contents,
|
|
1745
1763
|
loader: _Transformer.#loaders[extname(args.path)] ?? "js",
|