Skip to content

Commit

Permalink
Merge pull request #4137 from aryaemami59/codegen-vitest
Browse files Browse the repository at this point in the history
Migrate Codegen OpenAPI's unit tests to Vitest
  • Loading branch information
EskiMojo14 committed Feb 7, 2024
2 parents 00ebb5c + 66485e3 commit 3befbb6
Show file tree
Hide file tree
Showing 22 changed files with 1,105 additions and 2,173 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

strategy:
matrix:
node-version: ['16.x']
node-version: ['18.x']

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"prettier": "^3.2.4",
"release-it": "^14.12.5",
"serve": "^14.2.0",
"ts-node": "^10.9.2",
"typescript": "^5.2.2"
},
"resolutions": {
Expand All @@ -53,7 +54,6 @@
"react": "npm:18.2.0",
"react-dom": "npm:18.2.0",
"resolve": "1.22.1",
"ts-node": "10.4.0",
"@types/react": "npm:18.0.12",
"@types/react-dom": "npm:18.0.5",
"@types/inquirer": "npm:8.2.1",
Expand Down
4 changes: 0 additions & 4 deletions packages/rtk-query-codegen-openapi/babel.config.js

This file was deleted.

18 changes: 0 additions & 18 deletions packages/rtk-query-codegen-openapi/jest.config.js

This file was deleted.

30 changes: 14 additions & 16 deletions packages/rtk-query-codegen-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
"rtk-query-codegen-openapi": "lib/bin/cli.js"
},
"scripts": {
"build": "tsc && chmod +x lib/bin/cli.js",
"prepare": "npm run build && chmod +x ./lib/bin/cli.js",
"build": "yarn clean && tsc && chmod +x lib/bin/cli.js",
"clean": "rimraf lib",
"prepare": "yarn build && chmod +x lib/bin/cli.js",
"format": "prettier --write \"src/**/*.ts\"",
"test:update": "jest --runInBand --updateSnapshot",
"test:update": "vitest --run --update",
"test:update:enum": "lib/bin/cli.js test/config.example.enum.ts",
"test": "jest --runInBand",
"test": "vitest --run",
"test:watch": "vitest --watch",
"cli": "esr src/bin/cli.ts"
},
"files": [
Expand All @@ -36,35 +38,31 @@
"@reduxjs/toolkit": "^1.6.0",
"@types/commander": "^2.12.2",
"@types/glob-to-regexp": "^0.4.0",
"@types/jest": "^27",
"@types/lodash": "^4.14.165",
"@types/node": "^14.14.12",
"@types/node-fetch": "^2.6.11",
"@types/prettier": "^2.1.6",
"@types/node": "^20.11.10",
"@types/semver": "^7.3.9",
"babel-jest": "^26.6.3",
"chalk": "^4.1.0",
"del": "^6.0.0",
"esbuild": "~0.17",
"esbuild-runner": "^2.2.1",
"husky": "^4.3.6",
"jest": "^29",
"msw": "^0.40.2",
"node-fetch": "^2.7.0",
"msw": "^2.1.5",
"node-fetch": "^3.3.2",
"openapi-types": "^9.1.0",
"pretty-quick": "^3.1.0",
"ts-jest": "^29",
"ts-node": "^10.4.0",
"rimraf": "^5.0.5",
"ts-node": "^10.9.2",
"vitest": "^1.2.2",
"yalc": "^1.0.0-pre.47"
},
"dependencies": {
"@apidevtools/swagger-parser": "^10.0.2",
"commander": "^6.2.0",
"oazapfts": "^4.8.0",
"prettier": "^2.2.1",
"prettier": "^3.2.4",
"semver": "^7.3.5",
"swagger2openapi": "^7.0.4",
"typescript": "^5.0.0"
"typescript": "^5.3.3"
},
"husky": {
"hooks": {
Expand Down
5 changes: 2 additions & 3 deletions packages/rtk-query-codegen-openapi/src/bin/cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env node

import { dirname, resolve } from 'path';
import { generateEndpoints, parseConfig } from '../';

import program from 'commander';
import { dirname, resolve } from 'node:path';
import { generateEndpoints, parseConfig } from '../';

let ts = false;
try {
Expand Down
24 changes: 11 additions & 13 deletions packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import * as path from 'path';

import { camelCase } from 'lodash';
import path from 'node:path';
import ApiGenerator, {
getOperationName as _getOperationName,
getReferenceName,
isReference,
supportDeepObjects,
} from 'oazapfts/lib/codegen/generate';
import type { EndpointMatcher, EndpointOverrides, GenerationOptions, OperationDefinition, TextMatcher } from './types';
import { capitalize, getOperationDefinitions, getV3Doc, removeUndefined, isQuery as testIsQuery } from './utils';
import {
createPropertyAssignment,
createQuestionToken,
isValidIdentifier,
keywordType,
} from 'oazapfts/lib/codegen/tscodegen';
import { generateCreateApiCall, generateEndpointDefinition, generateImportNode, generateTagTypes } from './codegen';

import type { ObjectPropertyDefinitions } from './codegen';
import type { OpenAPIV3 } from 'openapi-types';
import { camelCase } from 'lodash';
import { factory } from './utils/factory';
import { generateReactHooks } from './generators/react-hooks';
import ts from 'typescript';
import type { ObjectPropertyDefinitions } from './codegen';
import { generateCreateApiCall, generateEndpointDefinition, generateImportNode, generateTagTypes } from './codegen';
import { generateReactHooks } from './generators/react-hooks';
import type { EndpointMatcher, EndpointOverrides, GenerationOptions, OperationDefinition, TextMatcher } from './types';
import { capitalize, getOperationDefinitions, getV3Doc, removeUndefined, isQuery as testIsQuery } from './utils';
import { factory } from './utils/factory';

const generatedApiName = 'injectedRtkApi';

Expand Down Expand Up @@ -136,7 +134,7 @@ export async function generateApi(
if (apiFile.startsWith('.')) {
apiFile = path.relative(path.dirname(outputFile), apiFile);
apiFile = apiFile.replace(/\\/g, '/');
if (!apiFile.startsWith('.')) apiFile = './' + apiFile;
if (!apiFile.startsWith('.')) apiFile = `./${apiFile}`;
}
}
apiFile = apiFile.replace(/\.[jt]sx?$/, '');
Expand Down Expand Up @@ -191,7 +189,7 @@ export async function generateApi(
);

function extractAllTagTypes({ operationDefinitions }: { operationDefinitions: OperationDefinition[] }) {
let allTagTypes = new Set<string>();
const allTagTypes = new Set<string>();

for (const operationDefinition of operationDefinitions) {
const { verb, pathItem } = operationDefinition;
Expand Down Expand Up @@ -280,7 +278,7 @@ export async function generateApi(
}
// if there are still any naming conflicts, prepend with underscore
while (name in queryArg) {
name = '_' + name;
name = `_${name}`;
}
return name;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/rtk-query-codegen-openapi/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import fs from 'fs';
import fs from 'node:fs';
import path from 'node:path';
import type { CommonOptions, ConfigFile, GenerationOptions, OutputFileOptions } from './types';
import { isValidUrl, prettify } from './utils';
export type { ConfigFile } from './types';
Expand All @@ -11,11 +11,11 @@ export async function generateEndpoints(options: GenerationOptions): Promise<str
? options.schemaFile
: path.resolve(process.cwd(), schemaLocation);

const sourceCode = await enforceOazapftsTsVersion(() => {
const { generateApi } = require('./generate');
const sourceCode = await enforceOazapftsTsVersion(async () => {
const { generateApi } = await import('./generate');
return generateApi(schemaAbsPath, options);
});
const outputFile = options.outputFile;
const { outputFile } = options;
if (outputFile) {
fs.writeFileSync(path.resolve(process.cwd(), outputFile), await prettify(outputFile, sourceCode));
} else {
Expand Down
3 changes: 1 addition & 2 deletions packages/rtk-query-codegen-openapi/src/utils/getV3Doc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import SwaggerParser from '@apidevtools/swagger-parser';
import type { OpenAPIV3 } from 'openapi-types';
// @ts-ignore
import converter from 'swagger2openapi';

import type { OpenAPIV3 } from 'openapi-types';

export async function getV3Doc(spec: string): Promise<OpenAPIV3.Document> {
const doc = await SwaggerParser.bundle(spec);
const isOpenApiV3 = 'openapi' in doc && doc.openapi.startsWith('3');
Expand Down
4 changes: 2 additions & 2 deletions packages/rtk-query-codegen-openapi/src/utils/prettier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as prettier from 'prettier';
import * as path from 'path';
import path from 'node:path';
import prettier from 'prettier';

const EXTENSION_TO_PARSER: Record<string, string> = {
ts: 'typescript',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`CLI options testing generation with \`config.example.js\` 1`] = `
import { api } from '../fixtures/emptyApi';
exports[`CLI options testing > generation with \`config.example.js\` 1`] = `
"import { api } from '../fixtures/emptyApi';
const injectedRtkApi = api.injectEndpoints({
endpoints: (build) => ({
updatePet: build.mutation<UpdatePetApiResponse, UpdatePetApiArg>({
Expand Down Expand Up @@ -217,11 +217,11 @@ export type User = {
phone?: string;
userStatus?: number;
};
"
`;
exports[`CLI options testing paths are relative to configfile, not to cwd 1`] = `
import { api } from '../fixtures/emptyApi';
exports[`CLI options testing > paths are relative to config file, not to cwd 1`] = `
"import { api } from '../fixtures/emptyApi';
const injectedRtkApi = api.injectEndpoints({
endpoints: (build) => ({
updatePet: build.mutation<UpdatePetApiResponse, UpdatePetApiArg>({
Expand Down Expand Up @@ -437,5 +437,5 @@ export type User = {
phone?: string;
userStatus?: number;
};
"
`;

0 comments on commit 3befbb6

Please sign in to comment.