Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whitespace should not have any significance. #2380

Closed
6 tasks done
DavidSabine opened this issue Nov 26, 2022 · 2 comments · Fixed by #2402
Closed
6 tasks done

Whitespace should not have any significance. #2380

DavidSabine opened this issue Nov 26, 2022 · 2 comments · Fixed by #2402

Comments

@DavidSabine
Copy link

DavidSabine commented Nov 26, 2022

Describe the bug

I've observed a strange behaviour with whitespace characters (tab versus spaces). I can't say for sure it's related to vi.mock() but I was using vi.mock() when I noticed the glitch.

When a tab character precedes vi.mock(), the implementation fails.

Reproduction

  1. See this file: https://github.com/DavidSabine/vitest-issue/blob/main/__tests__/weird.test.mjs

  2. Or paste the following code (with NO INDENTATION) into one of your own projects:

import { expect, test, vi } from 'vitest'
import { join as joinPath } from 'node:path'

const helloWorld = () => {
return joinPath('hello', 'world')
}

test('Are you mocking me?', () => {
// note there are NO indents in this file
// except the next line
// test pass with spaces, test fails with tab
	vi.mock('node:path', () => {
return {
join: vi.fn().mockReturnValue('goodbye world')
}
})
expect(helloWorld()).toBe('goodbye world')
})
  1. Add a couple of spaces at the line beginning with vi.mock(
  2. Notice the test passes.
  3. Remove the spaces.
  4. Notice the test passes.
  5. Insert a tab character.
  6. Notice the test FAIL.
  7. Repeat, go crazy.

I reproduce it in this few seconds of screenshare: https://youtu.be/R8EgVU2TrB4

System Info

In VSCode

  WSL System:
    OS: Linux 5.10 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
    Memory: 13.87 GB / 15.60 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.12.0 - ~/.asdf/installs/nodejs/18.12.0/bin/node
    Yarn: 1.22.19 - /mnt/c/Program Files/nodejs/yarn
    npm: 8.19.2 - ~/.asdf/plugins/nodejs/shims/npm

AND 

  System:
    OS: Windows 10 10.0.22621
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
    Memory: 18.45 GB / 31.96 GB
  Binaries:
    Node: 18.9.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.19.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.819.0), Chromium (107.0.1418.56)
    Internet Explorer: 11.0.22621.1

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

sheremet-va commented Nov 26, 2022

This is indeed a bug, but I would recommend you keep vi.mock always in the global scope, since it's moved there by Vitest anyway. It is not executed in a test block, so if you have many vi.mock calls, only one of them is actually executed, which might be confusing.

@DavidSabine
Copy link
Author

✔️

@github-actions github-actions bot locked and limited conversation to collaborators Jun 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants