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

toBeInstanceOf does not properly recognise instance when using baseUrl in tsconfig #2686

Closed
6 tasks done
sosnowski opened this issue Jan 16, 2023 · 2 comments · Fixed by #2687
Closed
6 tasks done

Comments

@sosnowski
Copy link

Describe the bug

Hello All,

I've noticed unexpected behaviour that I believe is a bug. I've replicated it on Stackblitz, you can check it here: https://stackblitz.com/edit/node-qasnfa

As you can see, the tests fail on toBeInstanceOf check.

The tested function is very simple, it just creates an instance of the User class

import { User } from './user';

export const createDefaultUser = () => {
  return new User('DefaultName');
};

Now, this test fails, even though it should not:

import { createDefaultUser } from 'src/create';
import { User } from 'src/user';

describe('Test User class', () => {
  it('Should create user instance', () => {
    const u = createDefaultUser();
    expect(u).toBeInstanceOf(User);
  });
});

It fails, because create.ts file is importing user.ts "directly" ./user.ts, while the test suite is referrng to it in relation to tsconfig baseUrl setting.

If I change the import statement in the test suite...

import { createDefaultUser } from '../src/create';
import { User } from '../src/user';

describe('Test User class', () => {
  it('Should create user instance', () => {
    const u = createDefaultUser();
    expect(u).toBeInstanceOf(User);
  });
});

It works perfectly

Reproduction

https://stackblitz.com/edit/node-qasnfa

Just run npm run test

System Info

This is from Stacblitz but the same issue occurs on my MacOS

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    vitest: ^0.27.1 => 0.27.1

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

sheremet-va commented Jan 16, 2023

Vitest doesn't recognise tsconfig. If you want to rely on it, install vite-tsconfig-paths plugin.

Your particular case incorrectly resolves url, but still loads a module, because it's relative to root. If you change baseUrl, it will still be loaded, even if TypeScript will give you the error. Your edge case should be resolved by #2687, but if you rely on tsconfig, you should install the plugin.

@sheremet-va
Copy link
Member

This syntax is not supported without vite-tsconfig-paths. The fact that it "works" sometimes is a coincidence. When resolving paths, Vite thinks that src/user is relative to the current folder.

@sheremet-va sheremet-va reopened this Feb 3, 2023
@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Feb 16, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants