vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Test run starts server

Open tmkn opened this issue 3 years ago • 5 comments

Describe the bug

I upgraded to [email protected] and [email protected]. My test command is defined like this: "test": "vitest --run" Now, whenever I run the tests they also start the built in server. However it fails with:

Uncaught SyntaxError: The requested module '/node_modules/react-redux/lib/index.js?v=fe7be4c4' does not provide an export named 'useDispatch' (at hooks.ts:1:10)

But really, the server shouldn't be started at all.

My vite.config.ts looks like this:

import * as child_process from "child_process";

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

const commitHash = child_process.execSync("git rev-parse --short HEAD").toString();

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [react()],
    define: {
        GIT_COMMIT_HASH: JSON.stringify(commitHash)
    },
    server: {
        open: true
    }
});

However if I change defineConfig to a callback it won't start the server:

import * as child_process from "child_process";

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

const commitHash = child_process.execSync("git rev-parse --short HEAD").toString();

// https://vitejs.dev/config/
export default defineConfig(() => ({ // <-- notice callback style
    plugins: [react()],
    define: {
        GIT_COMMIT_HASH: JSON.stringify(commitHash)
    },
    server: {
        open: true
    }
}));

With this little change the server now won't be started during a test run. (the behaviour is the same for single test runs and code coverage runs)

vitest.config.ts looks like this:

import { defineConfig, mergeConfig } from "vitest/config";
import viteConfig from "./vite.config";

export default mergeConfig(
    viteConfig,
    defineConfig({
        test: {
            environment: "jsdom",
            setupFiles: "./tests/setup.ts"
        }
    })
);

Unless I'm doing something wrong, it shouldn't behave like this. At least I couldn't find anything in the documentation

Reproduction

See above

System Info

System:
    OS: macOS 13.1
    CPU: (8) arm64 Apple M1
    Memory: 64.86 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 8.5.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 116.0.5845.110
    Safari: 16.2
  npmPackages:
    @vitejs/plugin-react: ^4.0.4 => 4.0.4 
    @vitest/coverage-v8: ^0.34.3 => 0.34.3 
    vite: ^4.4.9 => 4.4.9 
    vitest: ^0.34.3 => 0.34.3 


### Used Package Manager

yarn

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitest-dev/vitest/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitest-dev/vitest/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitest.dev/guide/).
- [X] Check that there isn't [already an issue](https://github.com/vitest-dev/vitest/issues) that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitest-dev/vitest/discussions) or join our [Discord Chat Server](https://chat.vitest.dev).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

tmkn avatar Aug 25 '23 20:08 tmkn

I am experiencing exactly the same issue, vite serve gets started as well during the vitest process, downgrading to 0.34.2 solved the issue

apocaliss92 avatar Aug 26 '23 08:08 apocaliss92

To avoid this you can put this in your vite.config.ts :

export default defineConfig(({command, mode, ssrBuild}) => {
 return {
   ...
   server: {
     open: mode !== 'test',
   }
 }
});

samsaggace avatar Aug 28 '23 12:08 samsaggace

I created #4044 for the same reason; some similar workarounds are mentioned there.

It still does seem to me that this is not great behavior to change on a patch release, especially since the symptoms / cause are not completely obvious at first glance, nor do they seem to line up with changes in the release notes.

wyardley avatar Aug 30 '23 04:08 wyardley

I have also encountered a different issue in vitest 0.34.3 with spawning browser, it may be related.

My project has a dependency that internally imports a css file.

If I use server: { open: mode !== 'test' } option with --experimentalVmThreads option, vitest throws Error: Failed to load url Does the file exist? If I use server: { open: true } option with --experimentalVmThreads option, a browser window will pop up, the test can only pass in my local environment, and failed on CI because we can't open the a browser window in CI.

Is there a better way to solve the issue?

smeng9 avatar Sep 03 '23 21:09 smeng9

This looks like a duplicate of my issue #4052 and should be fixed in v0.34.4

rvollmer-eleven avatar Sep 11 '23 18:09 rvollmer-eleven