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.

Can't run vitest tests in CI

See original GitHub issue

I’ve run into an issue where laravel-vite-plugin is preventing vitest tests from running in a CI environment (BitBucket Pipelines in my case, but anything that sets the CI variable would do it).

  • Laravel Vite Plugin Version: 0.7.0 (0.6.1 or later)
  • Laravel Version: 9.36.4
  • Node Version: 16.14.0
  • NPM Version: 8.3.1
  • Host operating system: Linux
  • Web Browser & Version: N/A
  • Running in Sail / Docker: Docker (CI environment)

Description:

I’m working on a pretty standard Jetstream based, Inertia/Vue application which has been set up to use Vite according to the docs in this repository. I needed some unit tests for my code, and followed https://vuejs.org/guide/scaling-up/testing.html#recipes to add vitest. That all worked fine locally, including hot reloading, and running npm test run to run tests once. When I added either npm test or npm test run to my CI server setup, my builds started failing with the following stack trace:

⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯
Error: You should not run the Vite HMR server in CI environments. You should build your assets for production instead.
    at ensureCommandShouldRunInEnvironment (/opt/atlassian/pipelines/agent/build/node_modules/laravel-vite-plugin/dist/index.js:169:15)
    at config (/opt/atlassian/pipelines/agent/build/node_modules/laravel-vite-plugin/dist/index.js:54:13)
    at runConfigHook (file:///opt/atlassian/pipelines/agent/build/node_modules/vite/dist/node/chunks/dep-4da11a5e.js:63556:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async resolveConfig (file:///opt/atlassian/pipelines/agent/build/node_modules/vite/dist/node/chunks/dep-4da11a5e.js:63076:14)
    at async createServer (file:///opt/atlassian/pipelines/agent/build/node_modules/vite/dist/node/chunks/dep-4da11a5e.js:62092:20)
    at async createVitest (file:///opt/atlassian/pipelines/agent/build/node_modules/vitest/dist/chunk-vite-node-externalize.72a4d20b.js:11390:18)
    at async startVitest (file:///opt/atlassian/pipelines/agent/build/node_modules/vitest/dist/chunk-vite-node-externalize.72a4d20b.js:11501:15)
    at async start (file:///opt/atlassian/pipelines/agent/build/node_modules/vitest/dist/cli.js:697:17)

Looking at the code in ensureCommandShouldRunInEnvironment it’s looking to see if a production build is being run, or a bypass variable is set before it throws that error. I think it should also be checking to make sure it’s not a vitest test session as well.

Steps To Reproduce:

  1. Take any Laravel application with Vite and laravel-vite-plugin set up.
  2. Install vitest as per https://vuejs.org/guide/scaling-up/testing.html#recipes
  3. Create a simple smoke test in resources/js/__tests__/smoke.test.js:
describe('smoke test', () => {
  it('is true', () => {
    expect(true).toBeTruthy()
  })
})
  1. Execute npm test run to make sure everything works. This will run the tests once and exit without starting HMR.
  2. You can fake a CI environment and see the error by running CI=true npm test run.

I would expect the output from steps 5 and 5 to be the same, but 6 actually throws the stack track above as it thinks HMR is being invoked in CI. If the CI env variable is set, and you don’t include “run” on the command line vitest will run the tests once and not go into watch mode, so the fix isn’t quite as simple as adding “run” to the commands checked at line 156.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
timacdonaldcommented, Nov 15, 2022

You can set the env LARAVEL_BYPASS_ENV_CHECK=1 to bypass the env check.

0reactions
timacdonaldcommented, Dec 19, 2022

no problem. thank you for raising the issue and the feedback ❤️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Usual pytest workflow - can't run tests in a file - Stack Overflow
I'm trying to setup a project (Python 3.4.4, pytest 3.3.1) to use pytest, but pytest is failing to run tests in a file...
Read more >
How to invoke pytest — pytest documentation
Pytest supports several ways to run and select tests from the command-line. ... This will run tests which contain names that match the...
Read more >
Can't run tests with py.test · Issue #4878 · pypa/pip - GitHub
Yes, pytest doesn't work out of the box. This is a consequence of the fact that the project is structured with an src...
Read more >
Testing Python Applications with Pytest - Semaphore Tutorial
Pytest stands out among Python testing tools due to its ease of use. ... To run the test, execute the pytest command: $...
Read more >
Testing with pytest - Scikit-HEP
Your tests should run against an installed version of your code. Testing against the local version might work while the installed version does...
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