Skip to content

Commit

Permalink
chore(tests): snapshot mutation options (#1530)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: fixes #1529
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/TypeStat/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/TypeStat/blob/main/.github/CONTRIBUTING.md)
were taken 🐕

## Overview

Snapshot options used in integration tests.
  • Loading branch information
rubiesonthesky authored Apr 7, 2024
1 parent 18c00a9 commit edfc365
Show file tree
Hide file tree
Showing 30 changed files with 3,699 additions and 81 deletions.
5 changes: 3 additions & 2 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"endlines",
"execa",
"extensionless",
"hackily",
"globbed",
"hackily",
"IIFE",
"inferables",
"intrinsic",
Expand All @@ -33,6 +33,7 @@
"tsup",
"typestat",
"undefineds",
"uniquify"
"uniquify",
"vitest"
]
}
60 changes: 29 additions & 31 deletions src/tests/testSetup.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { runMutations } from "automutate";
import fs from "node:fs/promises";
import path from "node:path";
import ts from "typescript";

import { fillOutRawOptions } from "../options/fillOutRawOptions.js";
import { parseRawCompilerOptions } from "../options/parseRawCompilerOptions.js";
import { RawTypeStatOptions } from "../options/types.js";
import { createTypeStatProvider } from "../runtime/createTypeStatProvider.js";

export interface MutationTestResult {
actualContent: string;
expectedFilePath: string;
options: string;
}

export const runMutationTest = async (dirPath: string) => {
export const runMutationTest = async (
dirPath: string,
): Promise<MutationTestResult> => {
const originalFileName = (await fs.readdir(dirPath)).find((file) =>
file.startsWith("original."),
);
Expand All @@ -21,26 +24,21 @@ export const runMutationTest = async (dirPath: string) => {
}

const readFile = (filename: string) =>
fs.readFile(path.join(dirPath, filename), {
encoding: "utf-8",
});
fs.readFile(path.join(dirPath, filename), "utf-8");

const originalFile = path.join(dirPath, originalFileName);
const fileNameSuffix = originalFileName.endsWith("x") ? "x" : "";
const actualFileName = `actual.ts${fileNameSuffix}`;
const actualFile = path.join(dirPath, actualFileName);
// file needs to exists before creating compiler options
await fs.copyFile(originalFile, actualFile);

const rawTypeStatOptions = await readFile("typestat.json");
const rawOptions = JSON.parse(rawTypeStatOptions) as RawTypeStatOptions;

const projectPath = path.join(dirPath, "tsconfig.json");
const rawCompilerOptionsJson = await readFile("tsconfig.json");
const convertResult = ts.parseConfigFileTextToJson(
"tsconfig.json",
rawCompilerOptionsJson,
);

if (convertResult.error) {
console.error(convertResult.error);
throw new Error("Error while reading tsconfig");
}

const compilerOptions = convertResult.config as ts.CompilerOptions;
const compilerOptions = await parseRawCompilerOptions(dirPath, projectPath);

const output = {
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand All @@ -50,22 +48,17 @@ export const runMutationTest = async (dirPath: string) => {
stdout: () => {},
};

const fileNameSuffix = originalFileName.endsWith("x") ? "x" : "";
const originalFile = path.join(dirPath, originalFileName);
const actualFileName = `actual.ts${fileNameSuffix}`;
const actualFile = path.join(dirPath, actualFileName);

await fs.copyFile(originalFile, actualFile);
const pendingOptions = fillOutRawOptions({
argv: { args: [] },
compilerOptions,
cwd: dirPath,
output,
projectPath,
rawOptions,
});

const provider = createTypeStatProvider({
...fillOutRawOptions({
argv: { args: [] },
compilerOptions,
cwd: dirPath,
output,
projectPath,
rawOptions,
}),
...pendingOptions,
fileNames: [actualFile],
});

Expand All @@ -77,5 +70,10 @@ export const runMutationTest = async (dirPath: string) => {
const expectFileName = `expected.ts${fileNameSuffix}`;
const expectedFilePath = path.join(dirPath, expectFileName);

return { actualContent, expectedFilePath };
const optionsSnapshot = JSON.stringify(pendingOptions, null, 2).replaceAll(
dirPath,
"<rootDir>",
);

return { actualContent, expectedFilePath, options: optionsSnapshot };
};
81 changes: 81 additions & 0 deletions test/__snapshots__/cleanups.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Cleanups > suppressTypeErrors > options 1`] = `
"{
"cleanups": {
"suppressTypeErrors": true
},
"compilerOptions": {
"compilerOptions": {
"strictNullChecks": true
},
"files": [
"actual.ts"
],
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false
},
"files": {
"above": "",
"below": "",
"renameExtensions": false
},
"filters": [],
"fixes": {
"importExtensions": false,
"incompleteTypes": false,
"missingProperties": false,
"noImplicitAny": false,
"noImplicitThis": false,
"noInferableTypes": false,
"strictNonNullAssertions": false
},
"hints": {
"react": {
"propTypes": "whenRequired",
"propTypesOptionality": "asWritten"
}
},
"mutators": [
[
"fixImportExtensions",
null
],
[
"fixIncompleteTypes",
null
],
[
"fixMissingProperties",
null
],
[
"fixNoImplicitAny",
null
],
[
"fixNoImplicitThis",
null
],
[
"fixNoInferableTypes",
null
],
[
"fixStrictNonNullAssertions",
null
]
],
"output": {},
"package": {
"directory": "<rootDir>",
"file": "<rootDir>/package.json"
},
"postProcess": {
"shell": []
},
"projectPath": "<rootDir>/tsconfig.json",
"types": {}
}"
`;
192 changes: 192 additions & 0 deletions test/__snapshots__/customMutators.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Custom mutators > empty array > options 1`] = `
"{
"cleanups": {
"suppressTypeErrors": false
},
"compilerOptions": {
"files": [
"actual.ts"
],
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false
},
"files": {
"above": "",
"below": "",
"renameExtensions": false
},
"filters": [],
"fixes": {
"importExtensions": false,
"incompleteTypes": false,
"missingProperties": false,
"noImplicitAny": false,
"noImplicitThis": false,
"noInferableTypes": false,
"strictNonNullAssertions": false
},
"hints": {
"react": {
"propTypes": "whenRequired",
"propTypesOptionality": "asWritten"
}
},
"mutators": [
[
"fixImportExtensions",
null
],
[
"fixIncompleteTypes",
null
],
[
"fixMissingProperties",
null
],
[
"fixNoImplicitAny",
null
],
[
"fixNoImplicitThis",
null
],
[
"fixNoInferableTypes",
null
],
[
"fixStrictNonNullAssertions",
null
]
],
"output": {},
"package": {
"directory": "<rootDir>",
"file": "<rootDir>/package.json"
},
"postProcess": {
"shell": []
},
"projectPath": "<rootDir>/tsconfig.json",
"types": {}
}"
`;

exports[`Custom mutators > one mutator > options 1`] = `
"{
"cleanups": {
"suppressTypeErrors": false
},
"compilerOptions": {
"include": [
"actual.ts"
],
"compileOnSave": false,
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false
},
"files": {
"above": "",
"below": "",
"renameExtensions": false
},
"filters": [],
"fixes": {
"importExtensions": false,
"incompleteTypes": false,
"missingProperties": false,
"noImplicitAny": false,
"noImplicitThis": false,
"noInferableTypes": false,
"strictNonNullAssertions": false
},
"hints": {
"react": {
"propTypes": "whenRequired",
"propTypesOptionality": "asWritten"
}
},
"include": [
"actual.ts"
],
"mutators": [
[
"./sampleMutator.cjs",
null
]
],
"output": {},
"package": {
"directory": "<rootDir>",
"file": "<rootDir>/package.json"
},
"postProcess": {
"shell": []
},
"projectPath": "<rootDir>/tsconfig.json",
"types": {}
}"
`;

exports[`Custom mutators > two mutators > options 1`] = `
"{
"cleanups": {
"suppressTypeErrors": false
},
"compilerOptions": {
"files": [
"actual.ts"
],
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false
},
"files": {
"above": "",
"below": "",
"renameExtensions": false
},
"filters": [],
"fixes": {
"importExtensions": false,
"incompleteTypes": false,
"missingProperties": false,
"noImplicitAny": false,
"noImplicitThis": false,
"noInferableTypes": false,
"strictNonNullAssertions": false
},
"hints": {
"react": {
"propTypes": "whenRequired",
"propTypesOptionality": "asWritten"
}
},
"mutators": [
[
"./mutator1.cjs",
null
],
[
"./mutator2.cjs",
null
]
],
"output": {},
"package": {
"directory": "<rootDir>",
"file": "<rootDir>/package.json"
},
"postProcess": {
"shell": []
},
"projectPath": "<rootDir>/tsconfig.json",
"types": {}
}"
`;
Loading

0 comments on commit edfc365

Please sign in to comment.