#Unexpected token in testing render method

44 messages · Page 1 of 1 (latest)

rigid flume
molten fiber
#

render expects a function as its first argument.

#

If you used typescript, you would have gotten a type warning.

#

So you need to use render(() => <AdminUserChange />).

rigid flume
#

It did't solve the problem. But what really hellps is the changing of the test file extension from .js to .jsx.

#

Have no idea why ...

#

By the way , I use js, not typescript

#

And now I have this

molten fiber
#

Using the function instead of the component should solve at least one part of the problem. I am the maintainer of our testing-library.

#

Maybe you need to await screen.findByRole('button') inside an async testing function instead.

#

But usually, rendering is pretty instant.

#

Unless you have a Suspense or Show in there somewhere.

#

Can you show me the AdminUserChange component?

#

Or wait, maybe it finds more than one button.

rigid flume
#

Yep it has 2

molten fiber
#

In which case, you should probably use const { getByRole } = render(...) and use that instead of screen.

#

or if they have different text, use screen.getByRole('button', { name: 'Update' }).

#

Exchange Update for whatever text you are expecting.

rigid flume
#
describe('<AdminUserChange />', () => {
  it('renders without crashing', () => {
    render(() => <AdminUserChange />);
    const updateBtn = screen.getByRole('button', { name: 'Update' });
    // const input = screen.getByName('phone_number');
    // expect(input).toBeInTheDocument();
    expect(updateBtn).toBeInTheDocument();
  });
});

not working

#

Stilll throws
dispose() is not a function

molten fiber
#

Then you are using node 20

rigid flume
#

Yep I have just updated because of another problem

molten fiber
#

There are three workarounds for a change in node's module resolution that breaks vitest.

#
  1. Inline all /solid-js/ deps,
  2. resolve.alias-es for all solid imports,
  3. downgrade node to 18+.
rigid flume
molten fiber
#

#1 is recommended.

#

I have already informed the vitest team of the issue, a later version should fix it so that the workaround can be removed.

rigid flume
#

I got this nasty problem so I tried to solve it by upgrading node to 20.

molten fiber
#

In your vite(st) config, you need something like test: { ..., deps: { inline: [/solid-js/] } },.

#

IIRC, at least. I'm currently on my cellphone.

rigid flume
#
//<reference types="vitest" />
/// <reference types="vite/client" />
// :point_up_2: do not forget to add the references above

import { defineConfig } from 'vite';
import solidPlugin from 'vite-plugin-solid';
import devtools from 'solid-devtools/vite';
import eslintPlugin from 'vite-plugin-eslint';

export default defineConfig({
  plugins: [devtools(), solidPlugin(), eslintPlugin()],
  server: {
    port: 3000,
  },
  build: {
    target: 'esnext',
  },
  test: {
    environment: 'jsdom',
    globals: true,
    transformMode: { web: [/\.[jt]sx?$/] },
  },
  envDir: '.envs',
  envPrefix: 'HIN_',
});

That's my config

molten fiber
#

Just add the deps part to test.

rigid flume
#

I worked out! But there is another problem:(

#

Why did it decided I have 2 instaces of solid and how to tell it that my App is actually wrapped by Router?

#
/* @refresh reload */
// eslint-disable-next-line import/named
import { render } from 'solid-js/web';
// import 'material-icons/iconfont/material-icons.css';
import 'solid-devtools';
import { Router } from '@solidjs/router';

import './index.css';
import App from './App';

render(
  () => (
    <Router>
      <App />
    </Router>
  ),
  document.getElementById('root'),
);
molten fiber
#

Ah, you probably need to include /@solidjs/ to the inlined deps as well.

rigid flume
#
///<reference types="vitest" />
/// <reference types="vite/client" />
// 👆 do not forget to add the references above

import { defineConfig } from 'vite';
import solidPlugin from 'vite-plugin-solid';
import devtools from 'solid-devtools/vite';
import eslintPlugin from 'vite-plugin-eslint';

export default defineConfig({
  plugins: [devtools(), solidPlugin(), eslintPlugin()],
  server: {
    port: 3000,
  },
  build: {
    target: 'esnext',
  },
  test: {
    environment: 'jsdom',
    globals: true,
    transformMode: { web: [/\.[jt]sx?$/] },
    deps: {
      inline: [/solid-js/, /@solidjs/],
    },
  },
  envDir: '.envs',
  envPrefix: 'HIN_',
});

Like this? It's not working

#

Wait

#

It solved the problem with multiple instances of solid!

#

But there is still the problem with Wrapping up the App with Router

rigid flume
#

Thank you! It works!

molten fiber
#

Happy to help. I hope your experience testing solid code is now smoother.

rigid flume
#

I'm a beginer in frontend testing so smoother is still not my case 🙂 But I'm keen on TDD and love testing backend in Python. I hope I manage to include TDD approach to my Solid js projects/