Skip to content

Commit 5466f8f

Browse files
committed
Added testing for type declaration using tsd
1 parent 33ea841 commit 5466f8f

File tree

3 files changed

+113
-14
lines changed

3 files changed

+113
-14
lines changed

index.d.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as http from 'http';
44
declare function fastifyOauth2(
55
instance: fastify.FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>,
66
options: fastifyOauth2.FastifyOAuth2Options,
7-
callback: (err?: fastify.FastifyError) => void
7+
callback: (err?: fastify.FastifyError) => void,
88
): void;
99

1010
declare namespace fastifyOauth2 {
@@ -16,33 +16,30 @@ declare namespace fastifyOauth2 {
1616
const SPOTIFY_CONFIGURATION: ProviderConfiguration;
1717
const VKONTAKTE_CONFIGURATION: ProviderConfiguration;
1818

19-
interface OAuth2Token{
19+
interface OAuth2Token {
2020
token_type: 'bearer';
2121
access_token: string;
2222
refresh_token?: string;
23-
expires_in: number
23+
expires_in: number;
2424
}
2525

26-
interface OAuth2Namespace{
26+
interface OAuth2Namespace {
2727
getAccessTokenFromAuthorizationCodeFlow(
2828
request: fastify.FastifyRequest<http.IncomingMessage>,
29-
): Promise<OAuth2Token>
29+
): Promise<OAuth2Token>;
3030

3131
getAccessTokenFromAuthorizationCodeFlow(
3232
request: fastify.FastifyRequest<http.IncomingMessage>,
33-
callback: (token: OAuth2Token) => void
34-
): void
33+
callback: (token: OAuth2Token) => void,
34+
): void;
3535

3636
getNewAccessTokenUsingRefreshToken(
3737
refreshToken: string,
3838
params: Object,
39-
callback: (token: OAuth2Token) => void
40-
): void
39+
callback: (token: OAuth2Token) => void,
40+
): void;
4141

42-
getNewAccessTokenUsingRefreshToken(
43-
refreshToken: string,
44-
params: Object,
45-
): Promise<OAuth2Token>
42+
getNewAccessTokenUsingRefreshToken(refreshToken: string, params: Object): Promise<OAuth2Token>;
4643
}
4744

4845
interface ProviderConfiguration {

index.test-d.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import * as fastify from 'fastify';
2+
import * as fastifyOauth2 from '.';
3+
import { expectType, expectError, expectAssignable } from 'tsd';
4+
5+
/**
6+
* Preparing some data for testing.
7+
*/
8+
const auth = fastifyOauth2.GOOGLE_CONFIGURATION;
9+
const scope = ['r_emailaddress', 'r_basicprofile'];
10+
const credentials = {
11+
client: {
12+
id: 'test_id',
13+
secret: 'test_secret',
14+
},
15+
auth: auth,
16+
};
17+
const OAuth2Options = {
18+
name: 'testOAuthName',
19+
scope: scope,
20+
credentials: credentials,
21+
callbackUri: 'http://localhost/testOauth/callback',
22+
callbackUriParams: {},
23+
generateStateFunction: () => {},
24+
checkStateFunction: () => {},
25+
startRedirectPath: '/login/testOauth',
26+
};
27+
28+
const server = fastify();
29+
30+
server.register(fastifyOauth2, OAuth2Options);
31+
32+
declare module 'fastify' {
33+
// Developers need to define this in their code like they have to do with all decorators.
34+
interface FastifyInstance {
35+
testOAuthName: fastifyOauth2.OAuth2Namespace;
36+
}
37+
}
38+
39+
/**
40+
* Actual testing.
41+
*/
42+
expectType<fastifyOauth2.ProviderConfiguration>(auth);
43+
expectType<string[]>(scope);
44+
expectType<fastifyOauth2.Credentials>(credentials);
45+
expectType<fastifyOauth2.Credentials>(credentials);
46+
47+
expectType<void>(fastifyOauth2(server, OAuth2Options, () => {}));
48+
expectError(fastifyOauth2()); // error because missing required arguments
49+
expectError(fastifyOauth2(server, {}, () => {})); // error because missing required plugin options
50+
51+
expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.FACEBOOK_CONFIGURATION);
52+
expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.GITHUB_CONFIGURATION);
53+
expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.GOOGLE_CONFIGURATION);
54+
expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.LINKEDIN_CONFIGURATION);
55+
expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.MICROSOFT_CONFIGURATION);
56+
expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.SPOTIFY_CONFIGURATION);
57+
expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.VKONTAKTE_CONFIGURATION);
58+
59+
server.get('/testOauth/callback', async request => {
60+
expectType<fastifyOauth2.OAuth2Namespace>(server.testOAuthName);
61+
62+
const token = await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request);
63+
expectType<fastifyOauth2.OAuth2Token>(token);
64+
expectType<void>(
65+
server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, (t: fastifyOauth2.OAuth2Token): void => {}),
66+
);
67+
68+
expectError<void>(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)); // error because Promise should not return void
69+
expectError<fastifyOauth2.OAuth2Token>(
70+
server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, (t: fastifyOauth2.OAuth2Token): void => {}),
71+
); // error because non-Promise function call should return void and have a callback argument
72+
expectError<void>(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)); // error because function call does not pass a callback as second argument.
73+
74+
if (token.refresh_token) {
75+
expectType<fastifyOauth2.OAuth2Token>(
76+
await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.refresh_token, {}),
77+
);
78+
expectType<void>(
79+
server.testOAuthName.getNewAccessTokenUsingRefreshToken(
80+
token.refresh_token,
81+
{},
82+
(t: fastifyOauth2.OAuth2Token): void => {},
83+
),
84+
);
85+
86+
expectError<void>(await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.refresh_token, {})); // error because Promise should not return void
87+
expectError<fastifyOauth2.OAuth2Token>(
88+
server.testOAuthName.getNewAccessTokenUsingRefreshToken(
89+
token.refresh_token,
90+
{},
91+
(t: fastifyOauth2.OAuth2Token): void => {},
92+
),
93+
); // error because non-Promise function call should return void and have a callback argument
94+
expectError<void>(server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.refresh_token, {})); // error because function call does not pass a callback as second argument.
95+
}
96+
97+
return {
98+
access_token: token.access_token,
99+
};
100+
});

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"lint": "standard | snazzy",
88
"unit": "tap test.js",
99
"test": "npm run lint && npm run unit",
10+
"test:typescript": "tsd",
1011
"coverage": "npm run unit -- --cov --coverage-report=html"
1112
},
1213
"repository": {
@@ -30,7 +31,8 @@
3031
"simple-get": "^3.0.2",
3132
"snazzy": "^8.0.0",
3233
"standard": "^14.3.3",
33-
"tap": "^12.0.0"
34+
"tap": "^12.0.0",
35+
"tsd": "^0.11.0"
3436
},
3537
"dependencies": {
3638
"es6-promisify": "^6.0.0",

0 commit comments

Comments
 (0)