Add options object to t.snapshot
MODIFY
packages/@romejs-runtime/rome/test.ts
packages/@romejs-runtime/rome/test.ts
@@ -11,6 +11,11 @@ export type SyncThrower = () => void;
export type ExpectedError = undefined | string | RegExp | Function;
+export type TestSnapshotOptions = {
+ filename?: string;
+ language?: string;
+};
+
export interface TestHelper {
// TODO this should be DiagnosticAdviceItem
addToAdvice(item: unknown): void;
@@ -45,15 +50,14 @@ export interface TestHelper {
snapshot(
expected: unknown,
message?: string,
- fileName?: string,
+ opts?: TestSnapshotOptions,
): Promise<string>;
snapshotNamed(
name: string,
expected: unknown,
message?: string,
- filename?: string,
+ opts?: TestSnapshotOptions,
): Promise<string>;
- getSnapshot(snapshotName: string): Promise<unknown>;
}
export type TestName = string | Array<string>;

MODIFY
packages/@romejs/core/test-worker/SnapshotManager.ts
packages/@romejs/core/test-worker/SnapshotManager.ts
* LICENSE file in the root directory of this source tree.
*/
-import {AbsoluteFilePath, AbsoluteFilePathMap} from '@romejs/path';
+import {
+ AbsoluteFilePath,
+ AbsoluteFilePathMap,
+ createAbsoluteFilePath,
+} from '@romejs/path';
import {exists, readFileText, unlink, writeFile} from '@romejs/fs';
import {TestRunnerOptions} from '../master/testing/types';
import TestWorkerRunner from './TestWorkerRunner';
@@ -38,6 +42,8 @@ type Snapshot = {
entries: Map<string, SnapshotEntry>;
};
+export const SNAPSHOT_EXT = '.test.md';
+
function buildEntriesKey(testName: string, entryName: string): string {
return `${testName}#${entryName}`;
}
@@ -45,7 +51,7 @@ function buildEntriesKey(testName: string, entryName: string): string {
export default class SnapshotManager {
constructor(runner: TestWorkerRunner, testPath: AbsoluteFilePath) {
this.defaultSnapshotPath = testPath.getParent().append(
- `${testPath.getExtensionlessBasename()}.test.md`,
+ `${testPath.getExtensionlessBasename()}${SNAPSHOT_EXT}`,
);
this.testPath = testPath;
@@ -70,6 +76,20 @@ export default class SnapshotManager {
}
}
+ normalizeSnapshotPath(filename: undefined | string): AbsoluteFilePath {
+ if (filename === undefined) {
+ return this.defaultSnapshotPath;
+ }
+
+ const path = createAbsoluteFilePath(filename);
+ const ext = path.getExtensions();
+ if (ext.endsWith(SNAPSHOT_EXT)) {
+ return path;
+ } else {
+ return path.addExtension(SNAPSHOT_EXT);
+ }
+ }
+
async init() {
await this.loadSnapshot(this.defaultSnapshotPath);
}
@@ -270,8 +290,9 @@ export default class SnapshotManager {
async get(
testName: string,
entryName: string,
- snapshotPath: AbsoluteFilePath = this.defaultSnapshotPath,
+ optionalFilename: undefined | string,
): Promise<undefined | string> {
+ const snapshotPath = this.normalizeSnapshotPath(optionalFilename);
let snapshot = this.snapshots.get(snapshotPath);
if (snapshot === undefined) {
@@ -298,15 +319,16 @@ export default class SnapshotManager {
entryName,
value,
language,
- snapshotPath = this.defaultSnapshotPath,
+ optionalFilename,
}: {
testName: string;
entryName: string;
value: string;
language: undefined | string;
- snapshotPath: undefined | AbsoluteFilePath;
+ optionalFilename: undefined | string;
},
) {
+ const snapshotPath = this.normalizeSnapshotPath(optionalFilename);
let snapshot = this.snapshots.get(snapshotPath);
if (snapshot === undefined) {
snapshot = {

MODIFY
packages/@romejs/core/test-worker/TestAPI.ts
packages/@romejs/core/test-worker/TestAPI.ts
@@ -23,8 +23,8 @@ import {
ExpectedError,
SyncThrower,
TestHelper,
+ TestSnapshotOptions,
} from '@romejs-runtime/rome/test';
-import {createAbsoluteFilePath} from '@romejs/path';
function formatExpectedError(expected: ExpectedError): string {
if (typeof expected === 'string') {
@@ -68,7 +68,7 @@ type SnapshotOptions = {
entryName: string;
expected: unknown;
message?: string;
- optionalFilename?: string;
+ opts?: TestSnapshotOptions;
};
export default class TestAPI implements TestHelper {
@@ -489,14 +489,14 @@ export default class TestAPI implements TestHelper {
snapshot(
expected: unknown,
message?: string,
- optionalFilename?: string,
+ opts?: TestSnapshotOptions,
): Promise<string> {
const id = this.snapshotCounter++;
return this.catchNamedSnapshot({
entryName: String(id),
expected,
message,
- optionalFilename,
+ opts,
});
}
@@ -504,36 +504,16 @@ export default class TestAPI implements TestHelper {
entryName: string,
expected: unknown,
message?: string,
- optionalFilename?: string,
+ opts?: TestSnapshotOptions,
): Promise<string> {
return this.catchNamedSnapshot({
entryName,
expected,
message,
- optionalFilename,
+ opts,
});
}
- async getSnapshot(entryName: string): Promise<unknown> {
- // TODO add `filename`
- return this.snapshotManager.get(this.testName, entryName);
- }
-
- _normalizeSnapshotFilename(filename: string): string {
- if (!filename.endsWith('test.md')) {
- const lastIndex = filename.lastIndexOf('.');
- let baseName = undefined;
- if (lastIndex === -1) {
- // extensionless file
- baseName = filename;
- } else {
- baseName = filename.substring(0, lastIndex);
- }
- return `${baseName}.test.md`;
- }
- return filename;
- }
-
catchNamedSnapshot(opts: SnapshotOptions): Promise<string> {
this.loadingSnapshots++;
return this.compareNamedSnapshot(opts).finally(() => {
@@ -546,10 +526,10 @@ export default class TestAPI implements TestHelper {
entryName,
message,
expected,
- optionalFilename,
+ opts = {},
}: SnapshotOptions,
): Promise<string> {
- let language: undefined | string;
+ let language: undefined | string = opts.language;
let formatted = '';
if (typeof expected === 'string') {
@@ -559,21 +539,11 @@ export default class TestAPI implements TestHelper {
formatted = prettyFormat(expected);
}
- if (entryName.toLocaleLowerCase().includes('javascript')) {
- language = 'javascript';
- }
-
- let snapshotPath = undefined;
- if (optionalFilename !== undefined) {
- optionalFilename = this._normalizeSnapshotFilename(optionalFilename);
- snapshotPath = createAbsoluteFilePath(optionalFilename);
- }
-
// Get the current snapshot
const existingSnapshot = await this.snapshotManager.get(
this.testName,
entryName,
- snapshotPath,
+ opts.filename,
);
if (existingSnapshot === undefined) {
// No snapshot exists, let's save this one!
@@ -582,7 +552,7 @@ export default class TestAPI implements TestHelper {
entryName,
value: formatted,
language,
- snapshotPath,
+ optionalFilename: opts.filename,
});
return entryName;
}

MODIFY
packages/@romejs/js-formatter/index.test.ts
packages/@romejs/js-formatter/index.test.ts
@@ -69,17 +69,28 @@ const promise = createFixtureTests(async (fixture, t) => {
const printed = formatJS(ast, formatOptions);
- const outputFile = inputFile.absolute.getParent().append(
- `${inputFile.absolute.getExtensionlessBasename()}.test.md`,
+ const snapshotFile = inputFile.absolute.getParent().append(
+ inputFile.absolute.getExtensionlessBasename(),
).join();
- await t.snapshotNamed('Javascript Input', inputContent, undefined, outputFile);
+ await t.snapshotNamed(
+ 'Input',
+ inputContent,
+ undefined,
+ {
+ filename: snapshotFile,
+ language: 'javascript',
+ },
+ );
await t.snapshotNamed(
- 'Javascript Output',
+ 'Output',
printed.code,
undefined,
- outputFile,
+ {
+ filename: snapshotFile,
+ language: 'javascript',
+ },
);
});

MODIFY
packages/@romejs/js-parser/index.test.ts
packages/@romejs/js-parser/index.test.ts
@@ -69,9 +69,9 @@ const promise = createFixtureTests(async (fixture, t) => {
});
const outputFile = inputFile.absolute.getParent().append(
- `${inputFile.absolute.getExtensionlessBasename()}.test.md`,
+ inputFile.absolute.getExtensionlessBasename(),
).join();
- await t.snapshot(ast, undefined, outputFile);
+ await t.snapshot(ast, undefined, {filename: outputFile});
});
// @ts-ignore Doesn't support top-level await lol

Add options object to t.snapshot