-2

I have a simple test case:

import {describe, it, test} from "vitest";
import {decodeBase64, hexlify} from "ethers";

describe('zapModule', () => {

    it('smoke test vitest', async () => {
        // this one fails  originally
       const bin = hexlify(decodeBase64("YIBgQFI0gBVhAA9XX4D9W1BgQFFhBGE4A4BhBGGDOYEBYECBkFJhAC6RYQIzVltfgVFgAWABYEAbA4ERFWEASFdhAEhhAaVWW2BAUZCAglKAYCACYCABggFgQFKAFWEAjVeBYCABW2BAgFGAggGQkVJfgVJgYGAgggFSgVJgIAGQYAGQA5CBYQBmV5BQW1CQUF9bglGBEBVhATpXYQDkg4KBUYEQYQCwV2EAsGEDlFZbYCACYCABAVFfAVGEg4FRgRBhAM1XYQDNYQOUVltgIAJgIAEBUWAgAVFhAWhgIBtgIBxWW4ODgVGBEGEA9ldhAPZhA5RWW2AgAmAgAQFRXwGEhIFRgRBhARJXYQESYQOUVltgIJCBApGQkQGBAVEBkZCRUpAVFZBSgGEBMoFhA6hWW5FQUGEAklZbUF9DgmBAUWAgAWEBT5KRkGEDzFZbYEBRYCCBgwMDgVKQYEBSkFCAUWAgggHzW19gYGBAUZBQX4FSYCCBAWBAUl+AhFFgIIYBh1r6YD89AWAfGRaCAWBAUj2CUpFQPV9gIIMBPpJQkpBQVltjTkh7cWDgG19SYEFgBFJgJF/9W2BAgFGQgQFgAWABYEAbA4ERgoIQFxVhAdtXYQHbYQGlVltgQFKQVltgQFFgH4IBYB8ZFoEBYAFgAWBAGwOBEYKCEBcVYQIJV2ECCWEBpVZbYEBSkZBQVltfW4OBEBVhAitXgYEBUYOCAVJgIAFhAhNWW1BQX5EBUlZbX2AggIOFAxIVYQJEV1+A/VuCUWABYAFgQBsDgIIRFWECWldfgP1bgYUBkVCFYB+DARJhAm1XX4D9W4FRgYERFWECf1dhAn9hAaVWW4BgBRthAo6FggFhAeFWW5GCUoOBAYUBkYWBAZCJhBEVYQKnV1+A/VuGhgGSUFuDgxAVYQOHV4JRhYERFWECxFdfgIH9W4YBYEBgHxmCjQOBAYITFWEC3FdfgIH9W2EC5GEBuVZbg4sBUWABYAFgoBsDgRaBFGEC/VdfgIH9W4FSg4MBUYmBERVhAxBXX4CB/VuAhQGUUFCNYD+FARJhAyVXX4CB/VuKhAFRiYERFWEDOVdhAzlhAaVWW2EDSYyEYB+EARYBYQHhVluSUICDUo6EgocBAREVYQNfV1+Agf1bYQNugY2FAYaIAWECEVZbUICLAZGQkVKEUlBQkYYBkZCGAZBhAq1WW5mYUFBQUFBQUFBQVltjTkh7cWDgG19SYDJgBFJgJF/9W19gAYIBYQPFV2NOSHtxYOAbX1JgEWAEUmAkX/1bUGABAZBWW19gQICDAYWEUmAggoGGAVKBhlGAhFJgYJNQg4cBkVCDgWAFG4gBAYOJAV9bg4EQFWEEUFeJgwNgXxkBhVKBUYBRFRWEUoYBUYaEAYmQUoBRiYUBgZBSYQQxgYqHAYSLAWECEVZblYcBlWAfAWAfGRaTkJMBhwGSUJCFAZBgAQFhA/hWW1CQmplQUFBQUFBQUFBQVv4="));
    })

})

This line of code runs perfectly well when inside of a React component, but fails in Vitest:

TypeError: invalid BytesLike value (argument="value", value={ "data": [ 96, 128, 96, 64, 82, 52, 128, 21, 97, 0, 15, 87, 95, 128, 253, 91, 80, 96, 64, 81, 97, 4, 97, 56, 3, 128, 97, 4, 97, 131, 57, 129, 1, 96, 64, 129, 144, 82, 97, 0, 46, 145, 97, 2, 51, 86, 91, 95, 129, 81, 96, 1, 96, 1, 96, 64, 27, 3, 129, 17, 21, 97, 0, 72, 87, 97, 0, 72, 97, 1, 165, 86, 91, 96, 64, 81, 144, 128, 130, 82, 128, 96, 32, 2, 96, 32, 1, 130, 1, 96, 64, 82, 128, 21, 97, 0, 141, 87, 129, 96, 32, 1, 91, 96, 64, 128, 81, 128, 130, 1, 144, 145, 82, 95, 129, 82, 96, 96, 96, 32, 130, 1, 82, 129, 82, 96, 32, 1, 144, 96, 1, 144, 3, 144, 129, 97, 0, 102, 87, 144, 80, 91, 80, 144, 80, 95, 91, 130, 81, 129, 16, 21, 97, 1, 58, 87, 97, 0, 228, 131, 130, 129, 81, 129, 16, 97, 0, 176, 87, 97, 0, 176, 97, 3, 148, 86, 91, 96, 32, 2, 96, 32, 1, 1, 81, 95, 1, 81, 132, 131, 129, 81, 129, 16, 97, 0, 205, 87, 97, 0, 205, 97, 3, 148, 86, 91, 96, 32, 2, 96, 32, 1, 1, 81, 96, 32, 1, 81, 97, 1, 104, 96, 32, 27, 96, 32, 28, 86, 91, 131, 131, 129, 81, 129, 16, 97, 0, 246, 87, 97, 0, 246, 97, 3, 148, 86, 91, 96, 32, 2, 96, 32, 1, 1, 81, 95, 1, 132, 132, 129, 81, 129, 16, 97, 1, 18, 87, 97, 1, 18, 97, 3, 148, 86, 91, 96, 32, 144, 129, 2, 145, 144, 145, 1, 129, 1, 81, 1, 145, 144, 145, 82, 144, 21, 21, 144, 82, 128, 97, 1, 50, 129, 97, 3, 168, 86, 91, 145, 80, 80, 97, 0, 146, 86, 91, 80, 95, 67, 130, 96, 64, 81, 96, 32, 1, 97, 1, 79, 146, 145, 144, 97, 3, 204, 86, 91, 96, 64, 81, 96, 32, 129, 131, 3, 3, 129, 82, 144, 96, 64, 82, 144, 80, 128, 81, 96, 32, 130, 1, 243, 91, 95, 96, 96, 96, 64, 81, 144, 80, 95, 129, 82, 96, 32, 129, 1, 96, 64, 82, 95, 128, 132, 81, 96, 32, 134, 1, 135, 90, 250, 96, 63, 61, 1, 96, 31, 25, 22, 130, 1, 96, 64, 82, 61, 130, 82, 145, 80, 61, 95, 96, 32, 131, 1, 62, 146, 80, 146, 144, 80, 86, 91, 99, 78, 72, 123, 113, 96, 224, 27, 95, 82, 96, 65, 96, 4, 82, 96, 36, 95, 253, 91, 96, 64, 128, 81, 144, 129, 1, 96, 1, 96, 1, 96, 64, 27, 3, 129, 17, 130, 130, 16, 23, 21, 97, 1, 219, 87, 97, 1, 219, 97, 1, 165, 86, 91, 96, 64, 82, 144, 86, 91, 96, 64, 81, 96, 31, 130, 1, 96, 31, 25, 22, 129, 1, 96, 1, 96, 1, 96, 64, 27, 3, 129, 17, 130, 130, 16, 23, 21, 97, 2, 9, 87, 97, 2, 9, 97, 1, 165, 86, 91, 96, 64, 82, 145, 144, 80, 86, 91, 95, 91, 131, 129, 16, 21, 97, 2, 43, 87, 129, 129, 1, 81, 131, 130, 1, 82, 96, 32, 1, 97, 2, 19, 86, 91, 80, 80, 95, 145, 1, 82, 86, 91, 95, 96, 32, 128, 131, 133, 3, 18, 21, 97, 2, 68, 87, 95, 128, 253, 91, 130, 81, 96, 1, 96, 1, 96, 64, 27, 3, 128, 130, 17, 21, 97, 2, 90, 87, 95, 128, 253, 91, 129, 133, 1, 145, 80, 133, 96, 31, 131, 1, 18, 97, 2, 109, 87, 95, 128, 253, 91, 129, 81, 129, 129, 17, 21, 97, 2, 127, 87, 97, 2, 127, 97, 1, 165, 86, 91, 128, 96, 5, 27, 97, 2, 142, 133, 130, 1, 97, 1, 225, 86, 91, 145, 130, 82, 131, 129, 1, 133, 1, 145, 133, 129, 1, 144, 137, 132, 17, 21, 97, 2, 167, 87, 95, 128, 253, 91, 134, 134, 1, 146, 80, 91, 131, 131, 16, 21, 97, 3, 135, 87, 130, 81, 133, 129, 17, 21, 97, 2, 196, 87, 95, 128, 129, 253, 91, 134, 1, 96, 64, 96, 31, 25, 130, 141, 3, 129, 1, 130, 19, 21, 97, 2, 220, 87, 95, 128, 129, 253, 91, 97, 2, 228, 97, 1, 185, 86, 91, 131, 139, 1, 81, 96, 1, 96, 1, 96, 160, 27, 3, 129, 22, 129, 20, 97, 2, 253, 87, 95, 128, 129, 253, 91, 129, 82, 131, 131, 1, 81, 137, 129, 17, 21, 97, 3, 16, 87, 95, 128, 129, 253, 91, 128, 133, 1, 148, 80, 80, 141, 96, 63, 133, 1, 18, 97, 3, 37, 87, 95, 128, 129, 253, 91, 138, 132, 1, 81, 137, 129, 17, 21, 97, 3, 57, 87, 97, 3, 57, 97, 1, 165, 86, 91, 97, 3, 73, 140, 132, 96, 31, 132, 1, 22, 1, 97, 1, 225, 86, 91, 146, 80, 128, 131, 82, 142, 132, 130, 135, 1, 1, 17, 21, 97, 3, 95, 87, 95, 128, 129, 253, 91, 97, 3, 110, 129, 141, 133, 1, 134, 136, 1, 97, 2, 17, 86, 91, 80, 128, 139, 1, 145, 144, 145, 82, 132, 82, 80, 80, 145, 134, 1, 145, 144, 134, 1, 144, 97, 2, 173, 86, 91, 153, 152, 80, 80, 80, 80, 80, 80, 80, 80, 80, 86, 91, 99, 78, 72, 123, 113, 96, 224, 27, 95, 82, 96, 50, 96, 4, 82, 96, 36, 95, 253, 91, 95, 96, 1, 130, 1, 97, 3, 197, 87, 99, 78, 72, 123, 113, 96, 224, 27, 95, 82, 96, 17, 96, 4, 82, 96, 36, 95, 253, 91, 80, 96, 1, 1, 144, 86, 91, 95, 96, 64, 128, 131, 1, 133, 132, 82, 96, 32, 130, 129, 134, 1, 82, 129, 134, 81, 128, 132, 82, 96, 96, 147, 80, 131, 135, 1, 145, 80, 131, 129, 96, 5, 27, 136, 1, 1, 131, 137, 1, 95, 91, 131, 129, 16, 21, 97, 4, 80, 87, 137, 131, 3, 96, 95, 25, 1, 133, 82, 129, 81, 128, 81, 21, 21, 132, 82, 134, 1, 81, 134, 132, 1, 137, 144, 82, 128, 81, 137, 133, 1, 129, 144, 82, 97, 4, 49, 129, 138, 135, 1, 132, 139, 1, 97, 2, 17, 86, 91, 149, 135, 1, 149, 96, 31, 1, 96, 31, 25, 22, 147, 144, 147, 1, 135, 1, 146, 80, 144, 133, 1, 144, 96, 1, 1, 97, 3, 248, 86, 91, 80, 144, 154, 153, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 86, 254 ], "type": "Buffer" }, code=INVALID_ARGUMENT, version=6.13.4) at makeError (file:///home/ko5tik/customer/node_modules/ethers/src.ts/utils/errors.ts:687:21) at assert (file:///home/ko5tik/customer/node_modules/ethers/src.ts/utils/errors.ts:715:25) at assertArgument (file:///home/ko5tik/customer/node_modules/ethers/src.ts/utils/errors.ts:727:5) at _getBytes (file:///home/ko5tik/customer/node_modules/ethers/src.ts/utils/data.ts:44:5) at getBytesCopy (file:///home/ko5tik/customer/node_modules/ethers/src.ts/utils/data.ts:66:12) at decodeBase64 (file:///home/ko5tik/customer/node_modules/ethers/src.ts/utils/base64.ts:31:12) at /home/ko5tik/customer/src/components/zapModule.test.tsx:10:28 at file:///home/ko5tik/customer/node_modules/@vitest/runner/dist/index.js:146:14 at file:///home/ko5tik/customer/node_modules/@vitest/runner/dist/index.js:533:11 at runWithTimeout (file:///home/ko5tik/customer/node_modules/@vitest/runner/dist/index.js:39:7)

It seems that it loads different versions of the function.
How do I tackle this problem?

vitest.config:

import {defineConfig} from 'vitest/config'
import tsconfigPaths from "vite-tsconfig-paths";
import react from '@vitejs/plugin-react';

export default defineConfig({
    plugins: [tsconfigPaths(), react()],
    test: {
        coverage: {
            reporter: ['text', 'json', 'html'],
        },
        environment: 'jsdom',
        exclude: [
            '**/.{idea,git,cache,output,temp}/**',
            '**/cypress/**',
            '**/dist/**',
            '**/node_modules/**',
            './test/pages/**',
        ],

        globals: true,
        setupFiles: './test/setup.ts',
    },
})

tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2021", // Update this line
    "lib": [
      "ES2020",
      "DOM",
      "ESNext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "ESNext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@*": [
        "./src/*"
      ],
      "@components": [
        "./src/components"
      ],
      "@app": [
        "./src/app/*"
      ],
      "@app/data": [
        "./src/app/data/*"
      ],
      "@config": [
        "./src/config/*"
      ]
    }
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    ".next/types/**/*.ts",
    "src/components/Navbar.jsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

1 Answer 1

0
+100

As I can see from the source code, hexlify expects data of type BytesLike. And the decodeBase64 produces, Uint8Array. The type defined here is export type BytesLike = DataHexString | Uint8Array;.

Since this is just a type error, you can ignore it with @ts-ignore comment.

For further debugging, please log the inputs and outputs of hexlify function in both react and testing context.

Also, make sure you are importing these libraries from the same place in both of your test and actual react component.

Furthermore, argument="value", value={ "data": [ 96, 128, 96,... does not look like Uint8Array.

Sign up to request clarification or add additional context in comments.

3 Comments

This code fragment is copied from a library I have to live with - and problem is, that it works in react app, but not in unit tests with vitest
In that case you can consider creating mock
Offending code originated from inside of ethers library: // node_modules/@ethers-ext/provider-multicall/lib.esm/_contract.js var bin = hexlify(decodeBase64("YIBgQFI0gBVhAA9XX4D9W1BgQFFhBGE4A4BhBGGDOYEBYECBkFJhAC6RYQIzVltfgVFgAWABYEAbA4ERFWEASFdhAEhhAaVWW2BAUZCAglKAYCACYCABggFg.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.