10

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?

1

5 Answers 5

26

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

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

1 Comment

works for me, thank you!
13

It may be installed as a dependency of vitest

Try adding

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

Comments

3

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()],
})

Comments

1

I was getting the error PrettyFormatPluginError: Invalid Chai property: $$typeof, and in my case it was due to a "naked" assertion:

expect(fn).toHaveBeenCalledTimes(1);
expect(fn) // <-- missing something afterwards
...

It's an old project, they may have changed the error message to a more meaningful one

1 Comment

That could be a potential issue that is hard to find. I've searched my codebase with a Regex like: expect(([^()]|([^()]*))*)(?!\.) It finds every expect without a following Dot.
0

I had this issue due to syntax errors in my tests.

In my case I deleted a value for a variable in my tests but not the var variable = part (had assumed i was copying but instead performed a cut) prior to the expect() calls at the end of the test, so one of the expect calls was getting assigned to that variable, which was then being passed into another expect() call.

Comments

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.