@kirana-style/jest 0.0.0-stage → 0.1.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,3 +1,19 @@
1
- # Temporary Holding Version
1
+ # @kirana-style/jest
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The Jest adapter for Kirana ([11-integrations](../../../../spec/11-integrations.md)): a
4
+ transformer that compiles a module's styles, then hands it to the transformer the project already
5
+ uses.
6
+
7
+ ```js
8
+ // jest.config.js
9
+ export default {
10
+ transform: {
11
+ "\\.[jt]sx?$": ["@kirana-style/jest", { transformer: "babel-jest" }],
12
+ },
13
+ }
14
+ ```
15
+
16
+ Tests see the class names the application ships; no stylesheet is written. Options: `root`,
17
+ `config`, `transformer` (default `babel-jest`), `transformerConfig`. Jest caches a module by its own
18
+ source, so after changing tokens another module reads, run Jest once with `--no-cache`. With Vitest,
19
+ use `@kirana-style/vite` instead.
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "@kirana-style/jest",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Kirana for Jest: a transformer that compiles modules, then hands them to your usual one.",
5
+ "license": "MIT",
6
+ "author": "The Kirana authors",
7
+ "homepage": "https://kirana.style",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/kirana-style/kirana.git",
11
+ "directory": "packages/kirana/npm/jest"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/kirana-style/kirana/issues"
15
+ },
16
+ "type": "module",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./src/index.d.ts",
20
+ "default": "./src/index.js"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "dependencies": {
25
+ "@kirana-style/compiler": "0.1.0",
26
+ "@kirana-style/core": "0.1.0"
27
+ },
28
+ "peerDependencies": {
29
+ "jest": ">=29.0.0"
30
+ },
31
+ "peerDependenciesMeta": {
32
+ "jest": {
33
+ "optional": true
34
+ }
35
+ },
36
+ "files": [
37
+ "src",
38
+ "bin",
39
+ "*.d.ts",
40
+ "*.css",
41
+ "README.md",
42
+ "builders.json",
43
+ "schemas",
44
+ "gatsby-node.mjs",
45
+ "rules",
46
+ "recommended.json",
47
+ "strict.json"
48
+ ]
49
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import type { DriverOptions } from "@kirana-style/compiler";
2
+ import type { KiranaConfig } from "@kirana-style/core/config";
3
+ export type KiranaJestOptions = {
4
+ /** The project root: where `kirana.config.*` and `@kirana-style/core` are found. Default: the working directory. */
5
+ root?: string;
6
+ /** Options that override `kirana.config.*`. */
7
+ config?: KiranaConfig;
8
+ /** The transformer that runs after Kirana. Default: `babel-jest`. */
9
+ transformer?: string;
10
+ /** Options for that transformer. */
11
+ transformerConfig?: unknown;
12
+ /** Injected Driver options (tests). */
13
+ driver?: Pick<DriverOptions, "native">;
14
+ };
15
+ type Transformed = string | {
16
+ code: string;
17
+ map?: unknown;
18
+ };
19
+ export declare function createTransformer(options?: KiranaJestOptions): {
20
+ canInstrument: boolean;
21
+ process(source: string, file: string, transformOptions: unknown): Transformed;
22
+ };
23
+ declare const _default: {
24
+ createTransformer: typeof createTransformer;
25
+ };
26
+ export default _default;
package/src/index.js ADDED
@@ -0,0 +1,55 @@
1
+ /**
2
+ * The Jest host adapter (KRN-INT-003): a transformer that compiles a module's styles, then hands the
3
+ * result to the transformer the project already uses (`babel-jest` unless configured), which strips
4
+ * types and JSX as before. Tests see the class names the application ships; no stylesheet is
5
+ * written, because Jest renders no CSS.
6
+ *
7
+ * transform: { "\\.[jt]sx?$": ["@kirana-style/jest", { transformer: "babel-jest" }] }
8
+ *
9
+ * Jest's `process` is synchronous, so the Driver is opened with `Driver.createSync`. Jest caches a
10
+ * module by its own source: after changing tokens another module reads, run Jest once with
11
+ * `--no-cache`.
12
+ */
13
+ import { createRequire } from "node:module";
14
+ import path from "node:path";
15
+ import { Driver, formatDiagnostic } from "@kirana-style/compiler";
16
+ import { shouldCompile } from "@kirana-style/compiler/host";
17
+ export function createTransformer(options = {}) {
18
+ const root = options.root ?? process.cwd();
19
+ let driver;
20
+ let next;
21
+ const chained = () => {
22
+ if (next)
23
+ return next;
24
+ const loaded = createRequire(path.join(root, "package.json"))(options.transformer ?? "babel-jest");
25
+ const module = "default" in loaded ? loaded.default : loaded;
26
+ next = module.createTransformer
27
+ ? module.createTransformer(options.transformerConfig)
28
+ : module;
29
+ return next;
30
+ };
31
+ return {
32
+ canInstrument: false,
33
+ process(source, file, transformOptions) {
34
+ driver ??= Driver.createSync({
35
+ root,
36
+ config: options.config,
37
+ ...options.driver,
38
+ });
39
+ let code = source;
40
+ if (shouldCompile(file, source, driver.libraries)) {
41
+ const result = driver.transform(file, source);
42
+ for (const diagnostic of result.diagnostics) {
43
+ const message = formatDiagnostic(diagnostic, source);
44
+ if (diagnostic.severity === "error")
45
+ throw new Error(message);
46
+ if (diagnostic.severity === "warning")
47
+ console.warn(message);
48
+ }
49
+ code = result.code ?? source;
50
+ }
51
+ return chained().process(code, file, transformOptions);
52
+ },
53
+ };
54
+ }
55
+ export default { createTransformer };