question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Vite + React + TS + Jest] is it any intention to make vite testable?

See original GitHub issue

Clear and concise description of the problem

I started a new project with Vite. Everything worked fine till i had to implement environment variables. I made it with documentation using import.meta and I noticed that all my tests associated with place where I used this syntax were broken.

I have stack:

  • React 17
  • TypeScript 4.5.2
  • jest 27
  • RTL
  • msw
  • Cypress

I tried a lot of solution from SO (like implement ts-jest, transformers in jest etc.) and even closed(why?) issues in Vite but it didn’t work. I found a temporary workaround doing this:

To be able run application i had to define process.env in config. Without that i had blank screen. I also had to implement dotenv to see envs from files. Without that i saw only envs passed directly in package.json.

// vite.config.ts
import dotenv from 'dotenv';

dotenv.config();

export default defineConfig({
	define: {
		'process.env': JSON.stringify(process.env)
	}
});

After that i had to read variable in old way to be able run tests. Without that jest cried about import.meta syntax.

export const baseURL = process.env.VITE_API_URL

Finally I had to pass envs directly to Cypress configuration in plugins using process.env and dotenv.

require('dotenv').config();

module.exports = (on, config) => ({
	...config,
	env: {
		...config.env,
		CYPRESS_USER_NAME: process.env.CYPRESS_USER_NAME,
		CYPRESS_USER_PASSWORD: process.env.CYPRESS_USER_PASSWORD,
		CYPRESS_BASE_URL: process.env.CYPRESS_BASE_URL
	}
});

For now it works but I don’t know what is the cost of this solution.

Suggested solution

Maybe building some preset for jest will be solution. Are there any plans for this?

Alternative

Currently there is no place in documention describing this problem or workaround. Perhaps, while there is no solution, it is worth describing the best workaround for this case.

Additional context

No response

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Niputicommented, Feb 18, 2022

then leave a issue at the repo or/and try out vitest which have much better integration with vite

1reaction
bluwycommented, Feb 18, 2022

See #1955. Or you can use Vitest, a great alternative to Jest.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Quick Jest Setup With ViteJS, React, & TypeScript | by Manny
This is a walkthrough, so we'll cover adding different libraries, why do certain configurations, and extend Jest a bit further to make ......
Read more >
Adding Jest with TypeScript Support to a Vite Application
In the root of your application, create a new file called jest-setup.ts. Inside of this file, type import @testing-library/jest-dom. This library is going...
Read more >
Changelog - Cypress Documentation
Users working in React Component Testing projects can now generate a basic spec file from the components that exist in their project. Addresses...
Read more >
Preview Your HTML Code While using Jest With React - Morioh
When writing tests using Jest, we usually debug by reading the HTML code. Sometimes the HTML is too complex and it's hard to...
Read more >
Accidentally testable with Jessica Sachs (JS Party #233)
Retool – The low-code platform for developers to build internal tools ... Basically, we wanted to make a Vite-based Storybook alternative ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found