Invalid Chai property in Vitest
Asked Answered
H

3

5

I have this Vitest test:

    import React from 'react';
    import { expect, it, vi } from 'vitest';
    import { render, screen } from '@testing-library/react';
    import { StyledNativeTimePicker } from 
       '../timePicker/StyledNativeTimePicker.jsx';

    ...

    it('Shows the time correctly', async () => {
    const time = '12:00';
    render(
        <StyledNativeTimePicker
            time={time}
            timeChanged={() => {}}
        ></StyledNativeTimePicker>
    );
    const testInput = screen.getByRole('input', { type: 'time' });
    expect(testInput).toHaveValue(time);
   });
   ...

Now I get this error Error: Invalid Chai property: toHaveValue. Any Idea why when Chai is not installed?

Hyperemia answered 6/12, 2023 at 9:44 Comment(1)
Chai is installed, by Vitest. "Vitest provides chai assertions by default and also Jest compatible assertions build on top of chai" - neither includes toHaveValue, perhaps you intended to use Jest-DOM.Amateurism
A
12

First you need to create setupTests.js file, then connect it to the config. You may have to install jsdom as a dependency to the project.

setupTests.js

import '@testing-library/jest-dom';

vite.config.js

import { defineConfig } from 'vite';

export default defineConfig({
  test: {
    globals: true,
    environment: "jsdom",
    setupFiles: ["./setupTests.js"],
  },
});

links: Github problem and Configuring Vitest

Australasia answered 14/12, 2023 at 21:48 Comment(0)
D
5

It may be installed as a dependency of vitest

Try adding

import '@testing-library/jest-dom/vitest';
Dun answered 26/2 at 23:43 Comment(0)
L
2

I just installed this

npm i vite-tsconfig-paths

and added to my vitest config : vitest.config.ts

import { defineConfig } from 'vite'
import tsconfigPaths from 'vite-tsconfig-paths'

export default defineConfig({
  plugins: [tsconfigPaths()],
})
import { defineConfig } from 'vite'
import tsconfigPaths from 'vite-tsconfig-paths'

export default defineConfig({
  plugins: [tsconfigPaths()],
})
Ludicrous answered 7/3 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.