rogue94
rogue94

Reputation: 11

jest environment jsdom and jose Uint8Array type mismatch

When running jest that calls the below function in jest unit test, I get multiple errors related to Uint8Array. I suspect what is happening is the implementation of the test environment's Uint8Array is different than the implementation within jose. When I use visual studio code to go to definition of Uint8Array within jest.setup.ts, it is different than the implementation in my source files, and also within jose package. Note that is error seems to only happen in jest-environment-jsdom test environment and works fine in node test environment. However, most of my other tests require the dom so I would like to work around/fix this if possible.

import { SignJWT, jwtVerify, JWTVerifyOptions, JWTPayload } from 'jose'; //used for jwt
export async function get_jwt_token() {
    const sig = (new TextEncoder().encode('MYSECRETKEY543534')) as Uint8Array;
    const token = await new SignJWT()
        .sign(sig); //causes TypeError: payload must be an instance of Uint8Array in jest

    return token;
}

I also get a related error when calling jwtVerify Key for the HS256 algorithm must be one of type KeyObject or Uint8Array. Received an instance of Uint8Array

 try {
        const result = await jwtVerify(
            token,
            new TextEncoder().encode('MYSECRETKEY543534'),
            {
                algorithms: ['HS256'],
                maxTokenAge: '10 m',
            } as JWTVerifyOptions
        ); // Causes Key for the HS256 algorithm must be one of type KeyObject or Uint8Array. Received an instance of Uint8Array
        return result.payload as unknown as UserJwtPayload;
    } catch (err) {
        throw new Error(getErrorMessage(err));
    }
    it('get_jwt_token should not cause an error', async () => {
        const token = await get_jwt_token('admin', 'my-password');
        console.log(token);
    });

    it('verify_jwt should not cause an error', async () => {
        const token = await verify_jwt('my-token');
        console.log(token);
    });

Here is my jest.setup.ts

import { TextEncoder, TextDecoder } from 'util';
Object.assign(global, { TextDecoder, TextEncoder });

jest.config.js

const nextJest = require("next/jest");
const createJestConfig = nextJest({
    dir: "./",
});
const customJestConfig = {
    preset: "ts-jest",
    roots: ["<rootDir>"],
    modulePaths: ["<rootDir>", "<rootDir>/src"],
    moduleDirectories: ["node_modules"],
    setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
    testEnvironment: "jest-environment-jsdom",
    moduleNameMapper: {
        "^@src/(.*)$": "<rootDir>/src/$1",
    },
    testEnvironmentOptions: { 
        customExportConditions: [''],
    },
};
module.exports = createJestConfig(customJestConfig);

I am using the latest versions of jest and jose but also tried older versions. I expect it to work normally without error. I even tried console.log(sig) inside of get_jwt_token and it shows the type as Uint8Array during jest test so I am not understanding the problem.

Am I missing something? Any help would be appreciated, thank you.

Upvotes: 0

Views: 328

Answers (0)

Related Questions