@describe-me/vitest 0.2.0 → 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.
@@ -5,8 +5,8 @@
5
5
  export declare class SnapshotStore {
6
6
  private readonly dir;
7
7
  constructor(outDir: string);
8
- /** Write one snapshot and return its path relative to the manifest. */
9
- write(snapshot: unknown): string;
8
+ /** Write one serialized snapshot and return its path relative to the manifest. */
9
+ write(json: string): string;
10
10
  /** Drop the files no manifest frame points at any more. */
11
11
  collectGarbage(referenced: Iterable<string>): void;
12
12
  }
@@ -11,9 +11,8 @@ export class SnapshotStore {
11
11
  this.dir = join(outDir, 'snapshots');
12
12
  mkdirSync(this.dir, { recursive: true });
13
13
  }
14
- /** Write one snapshot and return its path relative to the manifest. */
15
- write(snapshot) {
16
- const json = JSON.stringify(snapshot);
14
+ /** Write one serialized snapshot and return its path relative to the manifest. */
15
+ write(json) {
17
16
  const hash = createHash('sha1').update(json).digest('hex').slice(0, 16);
18
17
  const file = `${hash}.json`;
19
18
  const abs = join(this.dir, file);
@@ -0,0 +1,15 @@
1
+ import type { ViteUserConfig } from 'vitest/config';
2
+ /**
3
+ * The Vite and Vitest config that makes a DOM environment load the browser
4
+ * build of styled-components, or undefined when the project does not use
5
+ * styled-components 5 or later.
6
+ *
7
+ * Vitest resolves the Node build, whose `createGlobalStyle` never inserts its
8
+ * CSS on the client, so global resets and fonts would be missing from every
9
+ * snapshot. The alias points `styled-components` at the browser ESM file. The
10
+ * optimizer is needed as well: jest-styled-components `require`s
11
+ * styled-components through Node, which ignores the alias, and would get a
12
+ * second instance whose styles its serializer cannot see. Bundling both
13
+ * together gives them one.
14
+ */
15
+ export declare function styledComponentsBrowserBuild(root: string): ViteUserConfig | undefined;
@@ -0,0 +1,69 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { createRequire } from 'node:module';
3
+ import { dirname, join, resolve } from 'node:path';
4
+ function resolverFor(root) {
5
+ // createRequire wants an absolute file inside the directory; it does not have to exist.
6
+ const requireFromRoot = createRequire(join(resolve(root), 'package.json'));
7
+ return (id) => {
8
+ try {
9
+ return requireFromRoot.resolve(id);
10
+ }
11
+ catch {
12
+ return undefined;
13
+ }
14
+ };
15
+ }
16
+ function withoutDotSlash(path) {
17
+ return path.replace(/^\.\//, '');
18
+ }
19
+ /** The file the package's `browser` map swaps its `module` entry for, relative to the package. */
20
+ function browserModule(pkg) {
21
+ if (!pkg.module || !pkg.browser || typeof pkg.browser === 'string') {
22
+ return undefined;
23
+ }
24
+ const moduleEntry = withoutDotSlash(pkg.module);
25
+ for (const [from, to] of Object.entries(pkg.browser)) {
26
+ if (withoutDotSlash(from) === moduleEntry && typeof to === 'string') {
27
+ return to;
28
+ }
29
+ }
30
+ return undefined;
31
+ }
32
+ /**
33
+ * The Vite and Vitest config that makes a DOM environment load the browser
34
+ * build of styled-components, or undefined when the project does not use
35
+ * styled-components 5 or later.
36
+ *
37
+ * Vitest resolves the Node build, whose `createGlobalStyle` never inserts its
38
+ * CSS on the client, so global resets and fonts would be missing from every
39
+ * snapshot. The alias points `styled-components` at the browser ESM file. The
40
+ * optimizer is needed as well: jest-styled-components `require`s
41
+ * styled-components through Node, which ignores the alias, and would get a
42
+ * second instance whose styles its serializer cannot see. Bundling both
43
+ * together gives them one.
44
+ */
45
+ export function styledComponentsBrowserBuild(root) {
46
+ const resolveFromRoot = resolverFor(root);
47
+ const manifest = resolveFromRoot('styled-components/package.json');
48
+ if (!manifest) {
49
+ return undefined;
50
+ }
51
+ const pkg = JSON.parse(readFileSync(manifest, 'utf8'));
52
+ const major = Number.parseInt(pkg.version ?? '', 10);
53
+ const browserFile = browserModule(pkg);
54
+ if (Number.isNaN(major) || major < 5 || !browserFile) {
55
+ return undefined;
56
+ }
57
+ const replacement = join(dirname(manifest), browserFile);
58
+ if (!existsSync(replacement)) {
59
+ return undefined;
60
+ }
61
+ const include = ['styled-components'];
62
+ if (resolveFromRoot('jest-styled-components')) {
63
+ include.push('jest-styled-components');
64
+ }
65
+ return {
66
+ resolve: { alias: [{ find: /^styled-components$/, replacement }] },
67
+ test: { deps: { optimizer: { client: { enabled: true, include } } } },
68
+ };
69
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@describe-me/vitest",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Vitest integration for describe-me: plugin, setup file and reporter",
5
5
  "keywords": [
6
6
  "vitest",
@@ -35,6 +35,10 @@
35
35
  "types": "./dist/setup.d.ts",
36
36
  "default": "./dist/setup.js"
37
37
  },
38
+ "./setup-dom": {
39
+ "types": "./dist/setup-dom.d.ts",
40
+ "default": "./dist/setup-dom.js"
41
+ },
38
42
  "./reporter": {
39
43
  "types": "./dist/reporter.d.ts",
40
44
  "default": "./dist/reporter.js"
@@ -50,24 +54,31 @@
50
54
  "LICENSE"
51
55
  ],
52
56
  "sideEffects": [
53
- "./dist/setup.js"
57
+ "./dist/setup.js",
58
+ "./dist/setup-dom.js"
54
59
  ],
55
60
  "dependencies": {
56
- "@describe-me/core": "^0.2.0"
61
+ "@describe-me/core": "^0.4.0"
57
62
  },
58
63
  "peerDependencies": {
59
64
  "vitest": ">=4",
60
- "typescript": ">=5.0"
65
+ "typescript": ">=5.0",
66
+ "@testing-library/user-event": ">=14"
61
67
  },
62
68
  "peerDependenciesMeta": {
63
69
  "typescript": {
64
70
  "optional": true
71
+ },
72
+ "@testing-library/user-event": {
73
+ "optional": true
65
74
  }
66
75
  },
67
76
  "devDependencies": {
68
77
  "vitest": "^5.0.1",
69
78
  "@types/node": "^22",
70
- "typescript": "^6"
79
+ "typescript": "^6",
80
+ "@testing-library/user-event": "^14.6.1",
81
+ "@testing-library/dom": "^10.4.0"
71
82
  },
72
83
  "scripts": {
73
84
  "build": "rimraf dist && tsc -p tsconfig.json"