@plumeria/swc-jest 19.8.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) zss-in-js contributer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # @plumeria/swc-jest
2
+
3
+ Jest transformer for Plumeria.
4
+ It compiles the styling prop away, then hands the file to [`@swc/jest`](https://www.npmjs.com/package/@swc/jest), so components render in a test with the class names the build gives them.
5
+
6
+ ## Installation
7
+
8
+ ```bash
9
+ npm install -D @plumeria/swc-jest @swc/core
10
+ # or
11
+ yarn add -D @plumeria/swc-jest @swc/core
12
+ # or
13
+ pnpm add -D @plumeria/swc-jest @swc/core
14
+ ```
15
+
16
+ ## How to Use
17
+
18
+ Put it where `@swc/jest` was. Jest runs one transformer per pattern, so it replaces the entry rather than sitting beside it:
19
+
20
+ ```js
21
+ // jest.config.js
22
+ module.exports = {
23
+ testEnvironment: 'jsdom',
24
+ transform: {
25
+ '^.+\\.(t|j)sx?$': '@plumeria/swc-jest',
26
+ },
27
+ };
28
+ ```
29
+
30
+ SWC is configured as before: from `.swcrc`, or from the options next to the transformer.
31
+
32
+ ## Options
33
+
34
+ ```js
35
+ module.exports = {
36
+ transform: {
37
+ '^.+\\.(t|j)sx?$': [
38
+ '@plumeria/swc-jest',
39
+ {
40
+ styleProp: 'sx',
41
+ jsc: { transform: { react: { runtime: 'automatic' } } },
42
+ },
43
+ ],
44
+ },
45
+ };
46
+ ```
47
+
48
+ | Option | Default | Description |
49
+ | :-- | :-- | :-- |
50
+ | `styleProp` | `'classStyle'` | The JSX prop that carries styles. |
51
+ | `withoutLogicalProperties` | `false` | Reject the logical name of a property that also has a physical name. Takes `true` or `{ sizes?: boolean }`. |
52
+ | `withoutPhysicalProperties` | `false` | Reject the physical name of a property that also has a logical name. Takes `true` or `{ sizes?: boolean }`. |
53
+
54
+ Every other key is passed to `@swc/jest` unchanged. Keep these three the same as in the bundler config.
55
+
56
+ ## Do
57
+
58
+ Assert what the component does. The class names are real, so a branch that picks a different style shows up as a different `className`:
59
+
60
+ ```tsx
61
+ test('the active tab is styled apart from the rest', () => {
62
+ render(<Tabs active="b" />);
63
+
64
+ const a = screen.getByRole('tab', { name: 'A' });
65
+ const b = screen.getByRole('tab', { name: 'B' });
66
+
67
+ expect(b.getAttribute('aria-selected')).toBe('true');
68
+ expect(b.className).not.toBe(a.className);
69
+ });
70
+ ```
71
+
72
+ - Test text, roles, state and events, as you would without Plumeria.
73
+ - Compare class names between two renders or two elements to check a conditional style took effect.
74
+ - Import styles from other files freely. They compile as in the build, and Jest's cache follows them: change a shared style and the files that use it are compiled again.
75
+
76
+ ## Don't
77
+
78
+ ```tsx
79
+ expect(button.className).toBe('xqqbxt1d xq96bg3w'); // don't
80
+ expect(getComputedStyle(button).color).toBe('red'); // don't
81
+ ```
82
+
83
+ - Don't hard-code a generated class name. Each one is a hashed property–value pair, so adding a property to the style breaks the test without anything being wrong.
84
+ - Don't assert computed styles. jsdom applies no stylesheet, and none is written: `getComputedStyle` returns initial values. Check the cascade, `@media` and pseudo-classes in a real browser.
85
+ - Don't list it beside `@swc/jest`, `ts-jest` or `babel-jest` for the same files. Only one transformer runs per pattern.
86
+ - Don't rely on the test run for type checking. SWC strips types; run `tsc` for that.
87
+ - Don't use it to test what a style compiles to. Call the transform directly for that; see [Driving the transform](https://plumeria.dev/docs/api-reference/plugins/unplugin#driving-the-transform).
88
+
89
+ ## License
90
+
91
+ Plumeria is [MIT licensed](https://github.com/zss-in-js/plumeria/blob/main/LICENSE).
@@ -0,0 +1,12 @@
1
+ import swcJest from '@swc/jest';
2
+ import type { PropertyPolicyOptions } from '@plumeria/compiler';
3
+ type SwcOptions = NonNullable<Parameters<typeof swcJest.createTransformer>[0]>;
4
+ type SwcTransformer = ReturnType<typeof swcJest.createTransformer>;
5
+ export interface TransformerOptions extends SwcOptions, PropertyPolicyOptions {
6
+ styleProp?: string;
7
+ }
8
+ export declare function createTransformer(options?: TransformerOptions): SwcTransformer;
9
+ declare const _default: {
10
+ createTransformer: typeof createTransformer;
11
+ };
12
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createTransformer = createTransformer;
7
+ const jest_1 = __importDefault(require("@swc/jest"));
8
+ const compiler_1 = require("@plumeria/compiler");
9
+ function createTransformer(options = {}) {
10
+ const { styleProp = compiler_1.DEFAULT_STYLE_PROP, withoutLogicalProperties, withoutPhysicalProperties, ...swcOptions } = options;
11
+ const propertyPolicy = (0, compiler_1.resolvePropertyPolicy)({
12
+ withoutLogicalProperties,
13
+ withoutPhysicalProperties,
14
+ });
15
+ const swc = jest_1.default.createTransformer(swcOptions);
16
+ const compiled = new Map();
17
+ const compile = (src, filename, jestOptions) => {
18
+ if (filename.includes('node_modules') ||
19
+ !(0, compiler_1.needsCompile)(src, styleProp, filename)) {
20
+ return src;
21
+ }
22
+ return (0, compiler_1.transformSource)({
23
+ source: src,
24
+ moduleId: filename,
25
+ filePath: filename,
26
+ root: jestOptions.config.rootDir,
27
+ styleProp,
28
+ propertyPolicy,
29
+ isDev: process.env.NODE_ENV !== 'production',
30
+ collectOndemandSheets: false,
31
+ }).code;
32
+ };
33
+ const take = (src, filename, jestOptions) => {
34
+ const cached = compiled.get(filename);
35
+ compiled.delete(filename);
36
+ return cached && cached.src === src
37
+ ? cached.code
38
+ : compile(src, filename, jestOptions);
39
+ };
40
+ return {
41
+ canInstrument: swc.canInstrument,
42
+ process(...[src, filename, jestOptions]) {
43
+ return swc.process(take(src, filename, jestOptions), filename, jestOptions);
44
+ },
45
+ processAsync(...[src, filename, jestOptions]) {
46
+ return swc.processAsync(take(src, filename, jestOptions), filename, jestOptions);
47
+ },
48
+ getCacheKey(...[src, filename, jestOptions]) {
49
+ const code = compile(src, filename, jestOptions);
50
+ compiled.set(filename, { src, code });
51
+ return swc.getCacheKey(code, filename, jestOptions);
52
+ },
53
+ };
54
+ }
55
+ exports.default = { createTransformer };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@plumeria/swc-jest",
3
+ "version": "19.8.2",
4
+ "description": "Plumeria Jest transformer on @swc/jest",
5
+ "author": "Refirst 11",
6
+ "license": "MIT",
7
+ "homepage": "https://plumeria.dev",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/zss-in-js/plumeria.git",
11
+ "directory": "packages/swc-jest"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/zss-in-js/plumeria/issues"
15
+ },
16
+ "sideEffects": false,
17
+ "main": "dist/index.js",
18
+ "types": "dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist/"
27
+ ],
28
+ "dependencies": {
29
+ "@swc/jest": "^0.2.39",
30
+ "@plumeria/compiler": "19.8.2"
31
+ },
32
+ "peerDependencies": {
33
+ "@swc/core": "*"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public",
37
+ "provenance": true
38
+ },
39
+ "scripts": {
40
+ "build": "rimraf dist && pnpm cjs",
41
+ "cjs": "tsc --project tsconfig.cjs.json"
42
+ }
43
+ }