Skip to content

Commit

Permalink
test(dts-gen): make skipped test in dts-gen work (#3041)
Browse files Browse the repository at this point in the history
  • Loading branch information
nameless-mc authored Oct 29, 2024
1 parent 67c0bb8 commit d662e9d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
9 changes: 5 additions & 4 deletions packages/dts-gen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"lint:prettier": "prettier --parser typescript --check \"src/**/*.{ts,tsx}\" ./kintone.d.ts",
"lint": "run-p lint:*",
"help": "node dist/index.js --help",
"test": "jest",
"test:ci": "jest --runInBand",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules npx jest",
"test:ci": "cross-env NODE_OPTIONS=--experimental-vm-modules npx jest --runInBand",
"build:integration": "webpack --mode development --config webpack.config.js"
},
"engines": {
Expand All @@ -39,10 +39,11 @@
"devDependencies": {
"@types/eslint": "^9.6.1",
"@types/lodash": "^4.17.12",
"assert": "^2.1.0",
"cross-env": "^7.0.3",
"ts-loader": "^9.5.1",
"webpack": "^5.95.0",
"webpack-cli": "^5.1.4",
"assert": "^2.1.0"
"webpack-cli": "^5.1.4"
},
"bin": {
"kintone-dts-gen": "dist/index.js"
Expand Down
54 changes: 43 additions & 11 deletions packages/dts-gen/src/templates/template.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
import { TypeDefinitionTemplate as t } from "./template";
import { DemoClient } from "../kintone/clients/demo-client";
import { DemoFullWidthSymbolClient } from "../kintone/clients/demo-fullwidth-symbol-client";
import { FieldTypeConverter } from "../converters/fileldtype-converter";
import { objectValues } from "../utils/objectvalues";
import * as fs from "fs";
import { ESLint } from "eslint";
import path from "path";
import tsPreset from "@cybozu/eslint-config/flat/presets/typescript";
import * as prettierPluginTypescript from "prettier/plugins/typescript";
import * as prettierPluginEstree from "prettier/plugins/estree";
import { format } from "prettier/standalone";
import { convertToTsExpression } from "./converter";

// TODO: Make it work and remove skip
// Due to an internal dynamic import that stopped working after updating to ESLint v9, it was temporarily skipped.
describe.skip("renderAsFile", () => {
const TEMP_TEST_TYPEDEF = "tmp.test-renderAsFile-fields.d.ts";
const writeAndLint = async (filepath: string, expression: string) => {
await fs.promises.mkdir(path.dirname(filepath), { recursive: true });
// It fails when linting a string, so it is saved to a file first.
await fs.promises.writeFile(filepath, expression);
const eslint = new ESLint({
cwd: path.resolve(__dirname, "..", ".."),
fix: true,
baseConfig: tsPreset,
overrideConfig: {
rules: {
"@typescript-eslint/no-namespace": [
"error",
{ allowDeclarations: true },
],
},
},
});
const eslintResult = (await eslint.lintFiles(filepath))[0];
const eslintOutput = eslintResult.output;
const prettySource = await format(eslintOutput, {
parser: "typescript",
plugins: [prettierPluginTypescript, prettierPluginEstree],
});

await fs.promises.mkdir(path.dirname(filepath), { recursive: true });
await fs.promises.writeFile(filepath, prettySource);
};

describe("convertToTsExpression", () => {
const TEMP_TEST_TYPEDEF = "tmp.test-convertToTsExpression-fields.d.ts";
test("generate type definition file", async () => {
const client = new DemoClient();
const fieldTypeGroups = await client
Expand All @@ -27,10 +59,11 @@ describe.skip("renderAsFile", () => {
namespace: "kintone.types",
fieldTypeGroups,
};
await t.renderAsFile(TEMP_TEST_TYPEDEF, input);
const tsExpression = convertToTsExpression(input).tsExpression();
await writeAndLint(TEMP_TEST_TYPEDEF, tsExpression);

const expected = fs
.readFileSync(`./resources/test-renderAsFile-fields.d.ts`)
.readFileSync(`./resources/test-convertToTsExpression-fields.d.ts`)
.toString()
.trim()
.replace(/\r?\n/g, "");
Expand All @@ -53,9 +86,7 @@ describe.skip("renderAsFile", () => {
});
});

// TODO: Make it work and remove skip
// Due to an internal dynamic import that stopped working after updating to ESLint v9, it was temporarily skipped.
describe.skip("fullWidthSymbol Test", () => {
describe("fullWidthSymbol Test", () => {
const TEMP_TEST_TYPEDEF = "tmp.test-fullWidthSymbol-fields.d.ts";
test("generate type definition file", async () => {
const client = new DemoFullWidthSymbolClient();
Expand All @@ -75,7 +106,8 @@ describe.skip("fullWidthSymbol Test", () => {
namespace: "kintone.types",
fieldTypeGroups,
};
await t.renderAsFile(TEMP_TEST_TYPEDEF, input);
const tsExpression = convertToTsExpression(input).tsExpression();
await writeAndLint(TEMP_TEST_TYPEDEF, tsExpression);

const expected = fs
.readFileSync("./resources/test-fullWidthSymbol-fields.d.ts")
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d662e9d

Please sign in to comment.