Skip to content

Commit

Permalink
fix(test): fix mutation test not actually reading tsconfig (#1511)
Browse files Browse the repository at this point in the history
<!-- 👋 Hi, thanks for sending a PR to TypeStat! 💖.
Please fill out all fields below and make sure each item is true and [x]
checked.
Otherwise we may not be able to review your PR. -->

## PR Checklist

- [x] Addresses an existing open issue: fixes #1454 
- [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

<!-- Description of what is changed and how the code change does that.
-->

- Refactor file reading in testSetup.ts
- Fix reading tsConfig settings

Some files changed to be what they should be, and some are now exposing
bugs (`test/cases/fixes/incompleteTypes/returnTypes/expected.ts`) .

I'm not sure why the type is being removed in
`test/cases/fixes/noInferableTypes/variableDeclarations/expected.ts` but
I think this is also exposing some type. Or it may be that Map would
need some different tsconfig settings. - Filed new issue about it
#1512
  • Loading branch information
rubiesonthesky authored Apr 1, 2024
1 parent b5bfd34 commit be900f9
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 22 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test-mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare
- run: pnpm run build --no-dts
- run: pnpm run test:mutation --coverage
- run: pnpm run test:mutation --coverage --no-file-parallelism
- name: Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
29 changes: 18 additions & 11 deletions src/tests/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,27 @@ export const runMutationTest = async (dirPath: string) => {
throw new Error(`${dirPath} should have a file named original.*`);
}

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

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

const projectPath = path.join(dirPath, "tsconfig.json");
const compilerOptions: ts.CompilerOptions = (
ts.parseConfigFileTextToJson(projectPath, rawTypeStatOptions) as {
config: ts.CompilerOptions;
}
).config;
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 output = {
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down Expand Up @@ -66,7 +73,7 @@ export const runMutationTest = async (dirPath: string) => {
mutationsProvider: provider,
});

const actualContent = await fs.readFile(actualFile, { encoding: "utf-8" });
const actualContent = await readFile(actualFileName);
const expectFileName = `expected.ts${fileNameSuffix}`;
const expectedFilePath = path.join(dirPath, expectFileName);

Expand Down
2 changes: 1 addition & 1 deletion test/cases/fixes/incompleteTypes/returnTypes/expected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
return "";
};

async function navigateTo(): Promise<boolean> {
async function navigateTo(): Promise<boolean> | boolean {
return await new Promise(() => "");
}

Expand Down
4 changes: 2 additions & 2 deletions test/cases/fixes/incompleteTypes/variableTypes/expected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ import * as React from "react";
// Async

async function _() {
let stringFromNullImmediate: string = await Promise.resolve<null>(null);
let stringFromNullImmediate: string | null = await Promise.resolve<null>(null);

let stringFromUndefinedLater: string;
let stringFromUndefinedLater: string | undefined;
stringFromUndefinedLater = await Promise.resolve<undefined>(undefined);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@
// Async

async function _() {
let stringFromNullImmediate: string = await Promise.resolve<null>(null);
let stringFromNullImmediate: string | null = await Promise.resolve<null>(null);

let stringFromUndefinedLater: string;
let stringFromUndefinedLater: string | undefined;
stringFromUndefinedLater = await Promise.resolve<undefined>(undefined);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@

// should keep their types
// map
type TypeSummariesPerNodeByName = Map<string, unknown>;
const incompleteTypes: TypeSummariesPerNodeByName = new Map();
type TypeSummariesPerNodeByName = Map<string, number>;
const incompleteTypes = new Map();
// array
interface Mutation {
readonly range: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@

// should keep their types
// map
type TypeSummariesPerNodeByName = Map<string, unknown>;
type TypeSummariesPerNodeByName = Map<string, number>;
const incompleteTypes: TypeSummariesPerNodeByName = new Map();
// array
interface Mutation {
Expand Down
2 changes: 1 addition & 1 deletion test/fixMissingProperties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ describe("Missing properties", () => {
);
const { actualContent, expectedFilePath } = await runMutationTest(caseDir);
await expect(actualContent).toMatchFileSnapshot(expectedFilePath);
});
}, 10000);
});

0 comments on commit be900f9

Please sign in to comment.