How to add matchers from jest-extended
Asked Answered
B

1

5

I'm porting Jest tests to Vitest.

These tests use matchers from jest-extended like expect(val).toBeString(). I tried to put import "jest-extended" in the test file, but the result is the error: Error: Invalid Chai property: toBeString.

I tried also to put in vite.config.ts:

    test: {
        environment: "happy-dom",
        globals: true,
        reporters: ["default", "html"],
        setupFiles: [
            "node_modules/jest-extended/dist/index.js",
        ]
    }

without change. Even tried to use the vitest-extended package but also no change. Any suggestion?

Bondage answered 8/11, 2023 at 13:2 Comment(0)
B
7

As the documentation says, you need to create setup file.

// vitest.setup.js
import { expect } from 'vitest';
import * as matchers from 'jest-extended';

expect.extend(matchers);

Then you need to specify the path to it in the vitest configuration file

// vitest.config.js
export default defineConfig({
  test: {
    setupFiles: ['./vitest.setup.js'],
  },
});

More information about extending matchers can be found in the vitest documentation.

Belier answered 10/11, 2023 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.