Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Codegen OpenAPI's unit tests to Vitest #4137

Merged
merged 47 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
f52e48d
Change jest config to vitest
aryaemami59 Jan 29, 2024
6bf4f75
Change jest to vitest
aryaemami59 Jan 29, 2024
d631545
Fix `test` command
aryaemami59 Jan 29, 2024
ffa04f0
Fix `tsconfig.json`
aryaemami59 Jan 29, 2024
eb77694
Rename `jest.setup.ts` to `vitest.setup.ts`
aryaemami59 Jan 29, 2024
e92edaa
Fix `setupFiles` in `vitest.config.mts`
aryaemami59 Jan 29, 2024
e6f14ec
Bump `msw`
aryaemami59 Jan 29, 2024
1435e48
Update msw code snippets with v2 syntax
aryaemami59 Jan 29, 2024
739e37c
Remove `babel.config.js`
aryaemami59 Jan 29, 2024
444bb61
Disable worker threads to make `process.chdir` work
aryaemami59 Jan 29, 2024
a14f901
Update `tsconfig.json`s to make `import.meta.dirname` work
aryaemami59 Jan 29, 2024
7507f80
Drop jest related packages
aryaemami59 Jan 29, 2024
a03c3f8
Fix `tsconfig.json` to exclude `vitest.config.mts` from build
aryaemami59 Jan 29, 2024
29407bd
Add `test:watch` NPM script
aryaemami59 Jan 29, 2024
220c0c2
Fix `test:update` to use `vitest` instead of `jest`
aryaemami59 Jan 29, 2024
b808700
Fix `prepare` command to use `yarn` instead of `npm`
aryaemami59 Jan 29, 2024
a133bad
Add `rimraf` to `devDependencies`
aryaemami59 Jan 29, 2024
6a815e6
Add `clean` NPM script
aryaemami59 Jan 29, 2024
389296a
Fix `vitest.setup.ts`
aryaemami59 Jan 29, 2024
4f982de
Bump `node-fetch`
aryaemami59 Jan 29, 2024
6466cc2
Update snapshots
aryaemami59 Jan 29, 2024
ea195e7
Replace inline `require` with dynamic import
aryaemami59 Jan 29, 2024
32843ff
Manually define `__dirname`
aryaemami59 Jan 29, 2024
9065804
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 29, 2024
71eb9c5
Remove `pool: 'typescript'` in `vitest.config.mts`
aryaemami59 Jan 31, 2024
69a2712
Update snapshots further
aryaemami59 Jan 31, 2024
6c6449a
Skip failing tests for now
aryaemami59 Jan 31, 2024
743cf07
Fix `test:update:enum` NPM script
aryaemami59 Jan 31, 2024
4b7939b
Revert "Fix `test:update:enum` NPM script"
aryaemami59 Jan 31, 2024
a285872
Skip more failing tests
aryaemami59 Jan 31, 2024
1aa707f
Fix default imports
aryaemami59 Jan 31, 2024
d8edc4f
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Feb 3, 2024
8899bee
Re-bump `node-fetch`
aryaemami59 Feb 3, 2024
65fa51e
Update snapshots
aryaemami59 Feb 3, 2024
b255e75
Fix failing tests
aryaemami59 Feb 3, 2024
fe47ae2
Try to make CI happy
aryaemami59 Feb 3, 2024
549a614
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Feb 4, 2024
548dd59
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Feb 7, 2024
528d916
Update lockfile
aryaemami59 Feb 7, 2024
04ea106
Try to make CI happy
aryaemami59 Feb 7, 2024
dbe9a8a
Try things
aryaemami59 Feb 7, 2024
22ebf49
Try more things
aryaemami59 Feb 7, 2024
0c5164f
Bump Node
aryaemami59 Feb 7, 2024
cd4514c
Silence failing test for now
aryaemami59 Feb 7, 2024
6d8c918
Revert "Silence failing test for now"
aryaemami59 Feb 7, 2024
d289b24
Fix unit tests
aryaemami59 Feb 7, 2024
66485e3
Change timeout for unit tests
aryaemami59 Feb 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};

"
`;
Loading
Loading