From 5545cd0166fb06803fdd0cc76f07d5abc929e392 Mon Sep 17 00:00:00 2001 From: David Barrat Date: Mon, 1 Jan 2024 04:37:14 +0100 Subject: [PATCH] chore: fix jest setup --- jest.config.js | 2 +- jest.setup.js | 7 +++++++ src/vendors/jwks/jwks.test.ts | 9 --------- src/vendors/jwt/jwt-verify.test.ts | 9 --------- 4 files changed, 8 insertions(+), 19 deletions(-) create mode 100644 jest.setup.js diff --git a/jest.config.js b/jest.config.js index f97692d..09f75d1 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,4 @@ module.exports = { - moduleFileExtensions: ["ts", "js"], transform: { "^.+\\.(ts)$": "ts-jest", @@ -19,4 +18,5 @@ module.exports = { "/lib/**/*.ts", "/lib/**/**/*.ts" ], + setupFilesAfterEnv: ['/jest.setup.js'], } diff --git a/jest.setup.js b/jest.setup.js new file mode 100644 index 0000000..e3b0697 --- /dev/null +++ b/jest.setup.js @@ -0,0 +1,7 @@ +const fetch = require('node-fetch'); +const { Headers } = fetch; + +if (!global.fetch) { + global.fetch = fetch; + global.Headers = Headers; +} \ No newline at end of file diff --git a/src/vendors/jwks/jwks.test.ts b/src/vendors/jwks/jwks.test.ts index 551a784..b0bbb0a 100644 --- a/src/vendors/jwks/jwks.test.ts +++ b/src/vendors/jwks/jwks.test.ts @@ -8,15 +8,6 @@ import { import { JwtAlgorithmsEnum as Algs, JwtKeyTypes as Kty } from "../../enums"; import { default as nock } from "nock"; -// TODO: move this to jest config -import fetch, { Headers } from "node-fetch"; - -// https://stackoverflow.com/a/75956506/8483084 -if (!globalThis.fetch) { - globalThis.fetch = fetch; - globalThis.Headers = Headers; -} - import * as c from "../../constants"; import { SignJWT, jwtVerify } from "jose"; const AUTHDOG_API_ROOT = "https://api.authdog.xyz"; diff --git a/src/vendors/jwt/jwt-verify.test.ts b/src/vendors/jwt/jwt-verify.test.ts index dacb8f5..22c2036 100644 --- a/src/vendors/jwt/jwt-verify.test.ts +++ b/src/vendors/jwt/jwt-verify.test.ts @@ -15,15 +15,6 @@ import * as c from "../../constants"; import { getKeyPair, signJwtWithPrivateKey } from "./jwt-sign"; import nock from "nock"; -// TODO: move this to jest config -import fetch, { Headers } from "node-fetch"; - -// https://stackoverflow.com/a/75956506/8483084 -if (!globalThis.fetch) { - globalThis.fetch = fetch; - globalThis.Headers = Headers; -} - const DUMMY_HS256_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; const DUMMY_NON_JWT_TOKEN = "hello-i-am-not-a-jwt";