|
1 |
| -import { ISnapshotProcessorOptions, SnapshotProcessor } from "."; |
2 |
| -import { ECompilerType, ITestContext, TCompilerOptions } from "../type"; |
| 1 | +import path from "path"; |
| 2 | +import { |
| 3 | + ECompilerType, |
| 4 | + ITestContext, |
| 5 | + ITestEnv, |
| 6 | + TCompilerOptions |
| 7 | +} from "../type"; |
| 8 | +import { Source } from "webpack-sources"; |
| 9 | +import { Compilation, Compiler } from "@rspack/core"; |
| 10 | +import { format as prettyFormat, PrettyFormatOptions } from "pretty-format"; |
| 11 | +import { getSerializers } from "jest-snapshot"; |
| 12 | +import { TTestContextOptions, TestContext } from "../test/context"; |
| 13 | +import { ISnapshotProcessorOptions, SnapshotProcessor } from "./snapshot"; |
| 14 | + |
| 15 | +const pathSerializer = require("jest-serializer-path"); |
| 16 | +const normalizePaths = pathSerializer.normalizePaths; |
| 17 | +const srcDir = path.resolve(__dirname, "../../tests/fixtures"); |
| 18 | +const distDir = path.resolve(__dirname, "../../tests/js/hook"); |
| 19 | + |
| 20 | +const sourceSerializer = { |
| 21 | + test(val: unknown) { |
| 22 | + return val instanceof Source; |
| 23 | + }, |
| 24 | + print(val: Source) { |
| 25 | + return val.source(); |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +const internalSerializer = { |
| 30 | + test(val: unknown) { |
| 31 | + return val instanceof Compiler || val instanceof Compilation; |
| 32 | + }, |
| 33 | + print(val: Compiler | Compilation) { |
| 34 | + return JSON.stringify(`${val.constructor.name}(internal ignored)`); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +const testPathSerializer = { |
| 39 | + test(val: unknown) { |
| 40 | + return typeof val === "string"; |
| 41 | + }, |
| 42 | + print(val: string) { |
| 43 | + return JSON.stringify( |
| 44 | + normalizePaths( |
| 45 | + // @ts-ignore |
| 46 | + val |
| 47 | + .replaceAll(srcDir, "<HOOK_SRC_DIR>") |
| 48 | + .replaceAll(distDir, "<HOOK_DIST_DIR>") |
| 49 | + ) |
| 50 | + ); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +const escapeRegex = true; |
| 55 | +const printFunctionName = false; |
| 56 | +const normalizeNewlines = (str: string) => str.replace(/\r\n|\r/g, "\n"); |
| 57 | +const serialize = (val: unknown, indent = 2, formatOverrides = {}) => |
| 58 | + normalizeNewlines( |
| 59 | + prettyFormat(val, { |
| 60 | + escapeRegex, |
| 61 | + indent, |
| 62 | + plugins: [ |
| 63 | + ...getSerializers(), |
| 64 | + sourceSerializer, |
| 65 | + internalSerializer, |
| 66 | + testPathSerializer |
| 67 | + ] as PrettyFormatOptions["plugins"], |
| 68 | + printFunctionName, |
| 69 | + ...formatOverrides |
| 70 | + }) |
| 71 | + ); |
| 72 | + |
| 73 | +export class HookCasesContext extends TestContext { |
| 74 | + protected promises: Promise<void>[] = []; |
| 75 | + protected count: number = 0; |
| 76 | + protected snapshots: Record< |
| 77 | + string | number, |
| 78 | + Array<[string | Buffer, string]> |
| 79 | + > = {}; |
| 80 | + protected snapshotsList: Array<string | number> = []; |
| 81 | + |
| 82 | + constructor( |
| 83 | + protected src: string, |
| 84 | + protected testName: string, |
| 85 | + protected options: TTestContextOptions |
| 86 | + ) { |
| 87 | + super(options); |
| 88 | + this.snapped = this.snapped.bind(this); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Snapshot function arguments and return value. |
| 93 | + * Generated snapshot is located in the same directory with the test source. |
| 94 | + * @example |
| 95 | + * compiler.hooks.compilation("name", context.snapped((...args) => { ... })) |
| 96 | + */ |
| 97 | + snapped(cb: (...args: unknown[]) => Promise<unknown>, prefix = "") { |
| 98 | + // eslint-disable-next-line |
| 99 | + let context = this; |
| 100 | + return function SNAPPED_HOOK(this: any, ...args: unknown[]) { |
| 101 | + let group = prefix ? prefix : context.count++; |
| 102 | + context._addSnapshot(args, "input", group); |
| 103 | + let output = cb.apply(this, args); |
| 104 | + if (output && typeof output.then === "function") { |
| 105 | + let resolve; |
| 106 | + context.promises.push(new Promise(r => (resolve = r))); |
| 107 | + return output |
| 108 | + .then((o: unknown) => { |
| 109 | + context._addSnapshot(o, "output (Promise resolved)", group); |
| 110 | + return o; |
| 111 | + }) |
| 112 | + .catch((o: unknown) => { |
| 113 | + context._addSnapshot(o, "output (Promise rejected)", group); |
| 114 | + return o; |
| 115 | + }) |
| 116 | + .finally(resolve); |
| 117 | + } |
| 118 | + context._addSnapshot(output, "output", group); |
| 119 | + return output; |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @internal |
| 125 | + */ |
| 126 | + _addSnapshot(content: unknown, name: string, group: string | number) { |
| 127 | + content = Buffer.isBuffer(content) |
| 128 | + ? content |
| 129 | + : serialize(content, undefined, { |
| 130 | + escapeString: true, |
| 131 | + printBasicPrototype: true |
| 132 | + }).replace(/\r\n/g, "\n"); |
| 133 | + (this.snapshots[group] = this.snapshots[group] || []).push([ |
| 134 | + content as Buffer | string, |
| 135 | + name |
| 136 | + ]); |
| 137 | + if (!this.snapshotsList.includes(group)) { |
| 138 | + this.snapshotsList.push(group); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @internal |
| 144 | + */ |
| 145 | + async collectSnapshots( |
| 146 | + options = { |
| 147 | + diff: {} |
| 148 | + } |
| 149 | + ) { |
| 150 | + await Promise.allSettled(this.promises); |
| 151 | + if (!this.snapshotsList.length) return; |
| 152 | + |
| 153 | + let snapshots = this.snapshotsList.reduce((acc, group, index) => { |
| 154 | + let block = this.snapshots[group || index].reduce( |
| 155 | + (acc, [content, name]) => { |
| 156 | + name = `## ${name || `test: ${index}`}\n\n`; |
| 157 | + let block = "```javascript\n" + content + "\n```\n"; |
| 158 | + return (acc += name + block + "\n"); |
| 159 | + }, |
| 160 | + "" |
| 161 | + ); |
| 162 | + group = Number.isInteger(group) ? `Group: ${index}` : group; |
| 163 | + group = `# ${group}\n\n`; |
| 164 | + return (acc += group + block); |
| 165 | + }, ""); |
| 166 | + |
| 167 | + // @ts-ignore |
| 168 | + expect(snapshots).toMatchFileSnapshot( |
| 169 | + path.join(this.src, "hooks.snap.txt"), |
| 170 | + options |
| 171 | + ); |
| 172 | + } |
| 173 | +} |
3 | 174 |
|
4 | 175 | interface IHookProcessorOptions<T extends ECompilerType>
|
5 | 176 | extends ISnapshotProcessorOptions<T> {
|
@@ -44,4 +215,9 @@ export class HookTaskProcessor extends SnapshotProcessor<ECompilerType.Rspack> {
|
44 | 215 | compiler.mergeOptions(this.hookOptions.options(context));
|
45 | 216 | }
|
46 | 217 | }
|
| 218 | + |
| 219 | + async check(env: ITestEnv, context: HookCasesContext) { |
| 220 | + await (context as any).collectSnapshots(); |
| 221 | + await super.check(env, context); |
| 222 | + } |
47 | 223 | }
|
0 commit comments