mhassan1 / redis-memory-server

Redis Server for testing. The server will allow you to connect your favorite client library to the Redis Server and run parallel integration tests isolated from each other.
MIT License
76 stars 12 forks source link

Testing with Vitest opens a window in VSCode (Windows 10) #39

Closed matheuslanduci closed 3 weeks ago

matheuslanduci commented 3 weeks ago

Hello,

I'm using redis-memory-server as an instance for my tests with vitest on Windows 10, and every time a test runs (and the server is killed), a script called redis_killer.js opens in my VSCode.

The tests run successfully; the only issue is that this file keeps opening.

I don't know if this could be related, but I'm using turbo and a monorepo setup.

// vitest.config.ts
import dotenv from 'dotenv'
import { defineConfig } from 'vitest/config'
import tsconfigPaths from 'vite-tsconfig-paths'

const env = dotenv.config({
    path: './.env.test'
})

if (env.error) {
    throw env.error
}

export default defineConfig({
    plugins: [tsconfigPaths()],
    test: {
        coverage: {
            provider: 'v8',
            exclude: [
                'drizzle.config.ts',
                'vitest.config.ts',
                './src/main.ts',
                './src/db/**/*.ts'
            ]
        },
        environment: 'node',
        env: {
            ...env.parsed
        },
        globals: true,
        include: ['./src/**/*.spec.ts'],
        setupFiles: ['./src/setup-tests.ts']
    }
})
// setup-tests.ts
import { RedisMemoryServer } from 'redis-memory-server'
import Redis from 'ioredis'

const redisServer = new RedisMemoryServer()

// #resources/redis is a map to ./src/resources/redis
vi.mock('#resources/redis', async () => {
    return {
        redis: new Redis({
            port: await redisServer.getPort(),
            host: await redisServer.getHost()
        })
    }
})

image

mhassan1 commented 3 weeks ago

My guess is that it has something to do with this line: https://github.com/mhassan1/redis-memory-server/blob/686899040c3080bb9e7bfa55b8a9b7541cbda34c/src/util/RedisInstance.ts#L220

I am looking into it.

mhassan1 commented 3 weeks ago

I'm not able to replicate this locally. What command are you running to execute the tests?

Please add the following to the top of setup-tests.ts and let me know the result:

console.log(process.env.NODE)
console.log(process.argv)
console.log(process.execPath)
matheuslanduci commented 3 weeks ago

I'm running the command pnpm test that goes to vitest image

This is the output of the logs:

# process.env.NODE
C:\Users\Matheus Landuci\AppData\Local\pnpm\pnpm.exe

# process.argv
[
  'C:\\Program Files\\nodejs\\node.exe',
  'E:\\Workspaces\\Projects\\Project\\node_modules\\.pnpm\\tinypool@1.0.1\\node_modules\\tinypool\\dist\\entry\\process.js'
]

# process.execPath
C:\Program Files\nodejs\node.exe

Using Node.js 22.3.0

If I try to run in a terminal (no VSCode running) it opens a new VSCode window with the file.

Things I tried:

matheuslanduci commented 3 weeks ago

Fixed it :)

Since the process.env.NODE is different than the process.argv, I put a NODE env in my .env.test and the value is where my Node executable is that is 'C:\Program Files\nodejs\node.exe'

image

Thanks.

mhassan1 commented 3 weeks ago

Glad you found a workaround. Out of curiosity, what versions of pnpm and vitest are you using?

matheuslanduci commented 3 weeks ago

Using pnpm 9.9.0 and vitest 2.1.3

mhassan1 commented 3 weeks ago

The only way I can replicate this is by manually setting process.env.NODE = 'C:\\path\\to\\pnpm', but that should never be set to something other than a node binary. If you can figure out where that is being set in your environment, that would be helpful. I can see where pnpm sets it to process.execPath (if it's not already set to something else): https://github.com/pnpm/npm-lifecycle/blob/1e0f149176877f906968a01087cf63290652ccbc/index.js#L81.