Skip to content

Commit

Permalink
Re-install MSW to mock tokens fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiodxa committed Nov 25, 2024
1 parent 81cf9be commit 666642a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@total-typescript/tsconfig": "^1.0.4",
"@types/bun": "^1.1.13",
"@types/debug": "^4.1.12",
"msw": "^2.6.6",
"remix-auth": "^4.0.0",
"typedoc": "^0.26.11",
"typedoc-plugin-mdn-links": "^4.0.1",
Expand Down
38 changes: 36 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import { describe, expect, mock, test } from "bun:test";
import {
afterAll,
afterEach,
beforeAll,
describe,
expect,
mock,
test,
} from "bun:test";
import { Cookie, SetCookie } from "@mjackson/headers";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/native";
import { OAuth2Strategy } from ".";
import { catchResponse } from "./test/helpers";

const server = setupServer(
http.post("https://example.app/token", async () => {
return HttpResponse.json({
access_token: "mocked",
expires_in: 3600,
refresh_token: "mocked",
scope: ["user:email", "user:profile"].join(" "),
token_type: "Bearer",
});
}),
);

describe(OAuth2Strategy.name, () => {
let verify = mock();

Expand All @@ -19,6 +41,18 @@ describe(OAuth2Strategy.name, () => {
id: string;
}

beforeAll(() => {
server.listen();
});

afterEach(() => {
server.resetHandlers();
});

afterAll(() => {
server.close();
});

test("should have the name `oauth2`", () => {
let strategy = new OAuth2Strategy<User>(options, verify);
expect(strategy.name).toBe("oauth2");
Expand Down Expand Up @@ -101,7 +135,7 @@ describe(OAuth2Strategy.name, () => {
{ headers: { cookie: cookie.toString() } },
);

await strategy.authenticate(request).catch((error) => error);
await strategy.authenticate(request);

expect(verify).toHaveBeenCalled();
});
Expand Down

0 comments on commit 666642a

Please sign in to comment.