Skip to content

Commit 9ea8fbe

Browse files
committed
Initial commit with vitest
1 parent de6f7f1 commit 9ea8fbe

12 files changed

+750
-2167
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ coverage
66
.nyc_output
77
*.log
88
.tap_output
9-
__tests__/petstore.d.ts
9+
__tests__/snapshot.d.ts
1010

1111
.yarn/*
1212
!.yarn/patches

.trunk/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*out
2+
*logs
3+
*actions
4+
*notifications
5+
plugins
6+
user_trunk.yaml
7+
user.yaml
8+
tools

.trunk/configs/.markdownlint.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Autoformatter friendly markdownlint config (all formatting rules disabled)
2+
default: true
3+
blank_lines: false
4+
bullet: false
5+
html: false
6+
indentation: false
7+
line_length: false
8+
spaces: false
9+
url: false
10+
whitespace: false

.trunk/configs/.yamllint.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
rules:
2+
quoted-strings:
3+
required: only-when-needed
4+
extra-allowed: ['{|}']
5+
empty-values:
6+
forbid-in-block-mappings: true
7+
forbid-in-flow-mappings: true
8+
key-duplicates: {}
9+
octal-values:
10+
forbid-implicit-octal: true

.trunk/trunk.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: 0.1
2+
cli:
3+
version: 1.13.0
4+
plugins:
5+
sources:
6+
- id: trunk
7+
ref: v1.0.0
8+
uri: https://github.com/trunk-io/plugins
9+
lint:
10+
enabled:
11+
12+
13+
14+
- git-diff-check
15+
16+
17+
18+
19+
20+
21+
runtimes:
22+
enabled:
23+
24+
25+
actions:
26+
enabled:
27+
- trunk-announce
28+
- trunk-check-pre-push
29+
- trunk-fmt-pre-commit
30+
- trunk-upgrade-available

Makefile

-12
This file was deleted.

__tests__/index.spec.ts

+7-85
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,11 @@
1-
import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache';
1+
import fs from 'fs';
2+
import path from 'path';
23

3-
import { type JsonPutBody, TypedRESTDataSource } from '../src/index';
4+
import { expect, test } from 'vitest';
45

5-
import type { paths } from './petstore';
6+
import { generate } from '../src/index';
67

7-
interface ApolloContext {
8-
petId: number;
9-
}
10-
11-
class PetStoreDataSource extends TypedRESTDataSource<paths, ApolloContext> {
12-
override baseURL = 'https://petstore3.swagger.io/api/v3';
13-
14-
protected willSendRequest() {}
15-
16-
callSimpleMethod() {
17-
return this.openapi.GET(
18-
'/pet/{petId}',
19-
{
20-
params: {
21-
path: {
22-
petId: this.context.petId,
23-
},
24-
},
25-
},
26-
{
27-
cacheOptions: {
28-
ttl: 100000,
29-
},
30-
},
31-
);
32-
}
33-
34-
uploadBadImage() {
35-
return this.openapi.POST('/pet/{petId}/uploadImage', {
36-
params: {
37-
path: { petId: 1 },
38-
query: { additionalMetadata: 'test' },
39-
},
40-
body: 'binarydata',
41-
});
42-
}
43-
}
44-
45-
test('should create a client', async () => {
46-
const petStore = new PetStoreDataSource();
47-
const cache = new InMemoryLRUCache();
48-
expect(petStore).toBeInstanceOf(PetStoreDataSource);
49-
petStore.initialize({
50-
context: { petId: 1 },
51-
cache,
52-
});
53-
54-
const response = await petStore.callSimpleMethod();
55-
expect(response.name).toBe('Pet1');
56-
expect(cache.keys()).toHaveLength(1);
57-
58-
const response2 = await petStore.callSimpleMethod();
59-
expect(response2.name).toBe('Pet1');
60-
expect(cache.keys()).toHaveLength(1);
61-
62-
const reqSpy = jest.spyOn(
63-
petStore as unknown as { willSendRequest: () => void },
64-
'willSendRequest',
65-
);
66-
67-
await petStore.uploadBadImage().catch((error) => {
68-
expect(error.extensions?.response).toBeTruthy();
69-
expect(error.extensions?.response.status).toBe(415);
70-
});
71-
72-
expect(reqSpy).toHaveBeenCalledTimes(1);
73-
expect(reqSpy).toHaveBeenCalledWith(
74-
expect.objectContaining({
75-
method: 'POST',
76-
path: '/pet/1/uploadImage',
77-
body: 'binarydata',
78-
params: new URLSearchParams({ additionalMetadata: 'test' }),
79-
}),
80-
);
81-
82-
// This is essentially a build time test.
83-
const body: JsonPutBody<paths['/pet']> = {
84-
id: 1,
85-
name: 'Kiki',
86-
photoUrls: [],
87-
};
88-
expect(body).toBeTruthy();
8+
test('should generate a matching document', async () => {
9+
const now = await generate(path.resolve(__dirname, 'petstore.json'), {});
10+
expect(now).toMatch(fs.readFileSync(path.resolve(__dirname, 'snapshot.d.ts'), 'utf8'));
8911
});

package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,20 @@
4646
"@openapi-typescript-infra/coconfig": "^3.2.2",
4747
"@semantic-release/changelog": "^6.0.3",
4848
"@semantic-release/git": "^10.0.1",
49-
"@types/jest": "^29.5.3",
49+
"@types/yargs-parser": "^21.0.0",
5050
"@typescript-eslint/eslint-plugin": "^6.2.0",
5151
"@typescript-eslint/parser": "^6.2.0",
5252
"coconfig": "^0.12.2",
5353
"eslint": "^8.45.0",
5454
"eslint-config-prettier": "^8.8.0",
5555
"eslint-plugin-import": "^2.27.5",
56-
"eslint-plugin-jest": "^27.2.3",
57-
"jest": "^29.6.1",
5856
"prettier": "^3.0.0",
59-
"ts-jest": "^29.1.1",
6057
"ts-node": "^10.9.1",
61-
"typescript": "^5.1.6"
58+
"typescript": "^5.1.6",
59+
"vitest": "^0.33.0"
6260
},
6361
"scripts": {
64-
"test": "make && tsc -p tsconfig.json --noEmit && jest",
62+
"test": "tsc -p tsconfig.test.json --noEmit && vitest",
6563
"lint": "eslint .",
6664
"build": "tsc -p tsconfig.build.json && yarn dlx glob-chmod 755 build/bin/*",
6765
"watch": "tsc -p tsconfig.json -w --preserveWatchOutput",

src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ interface ComponentWithProps {
1111
}
1212

1313
export async function generate(file: string | typeof process.stdin, options: OpenAPITSOptions) {
14-
const { default: openApiTs } = await import('openapi-typescript');
15-
1614
const enums: string[] = [];
1715
const visitedEnums: Record<string, string[]> = {};
1816

@@ -34,7 +32,7 @@ export async function generate(file: string | typeof process.stdin, options: Ope
3432
enums.push(definition);
3533
}
3634

37-
const basicOutput = await openApiTs(file, {
35+
const finalOptions: OpenAPITSOptions = {
3836
...options,
3937
transform(schemaObject, metadata) {
4038
// There are two places where we look for enums. Path handlers, and components.
@@ -73,7 +71,9 @@ export async function generate(file: string | typeof process.stdin, options: Ope
7371
}
7472
return undefined;
7573
},
76-
});
74+
};
7775

76+
const { default: openApiTs } = await import('openapi-typescript');
77+
const basicOutput = await openApiTs(file, finalOptions);
7878
return [basicOutput, ...enums].join('\n');
7979
}

tsconfig.test.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"skipLibCheck": true
5+
}
6+
}

vitest.config.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { configDefaults, defineConfig } from 'vitest/config';
2+
3+
export default defineConfig({
4+
test: {
5+
watch: false,
6+
exclude: ['.trunk', ...configDefaults.exclude],
7+
},
8+
});

0 commit comments

Comments
 (0)