#Unable to use Vitest due to import problems

5 messages · Page 1 of 1 (latest)

urban glade
#

Has anyone been successful in using Vitest? We are having extremely weird issues that I think are due to import or export problems, but I'm not sure what.
Our entire test suite runs in Jest without issues. On Vitest, around 80% of our tests run successfully, including some GraphQL tests.

We have a test for a class called SignUpUseCase, but when I run this test it fails like so:

 FAIL  test/use-cases/SignUpUseCase.test.ts > SignUpUseCase > handle > should throw when email is already registered
Error: Undefined type error. Make sure you are providing an explicit type for the "finishGather" (parameter at index [0]) of the "GatherResolver" class.

This mutation in GatherResolver is apparently the culprit, but it looks just like any other in this file. I commented out all other mutations in this file, so this is the only one active:

  @Mutation(() => FinishGatherOutput)
  async finishGather(
    @Args('input') input: FinishGatherInput,
  ): Promise<FinishGatherOutput> {
      ...
   }

This FinishGatherInput is defined in a file finish-gather.ts, and is just these 2 simple classes, not much else, it doesn't even import other InputTypes or ObjectTypes from other files.

import { Field, InputType, ObjectType } from '@nestjs/graphql';
import { IsNotEmpty, IsUUID } from 'class-validator';

@InputType()
export class FinishGatherInput {
  ...
}

@ObjectType()
export class FinishGatherOutput {
  ...
}

The definitions from this file are exported in the index.ts on the same folder:

export * from ... [a bazillion of these]
export * from './finish-gather';
export * from ... [more bazillions of them]

But here's the thing: if I move the finish-gather export to the top of the file, the SignUpUseCase test runs fine, no graphql issues.... but then if I start uncommenting other mutations, then eventually I find some that fail, where this trick above doesn't work.

Can someone help?

#

Please ask for more details, at this point I don't know what is revelant or not and there's so many characters I can put in the post

#

At some point during my investigation where I move imports up/down, change the import statement to something else (using path alias, include the specific class in the path, etc) I got this:

#

Vite config:


import swc from 'unplugin-swc';
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    globals: true,
    root: './',
    alias: {
      '@src': './src',
      '@test': './test',
    }
  },
  plugins: [
    swc.vite({
      module: { type: 'es6' },
    }),
  ],
});

urban glade
#

If anyone is struggling:

https://docs.nestjs.com/fundamentals/circular-dependency

It's due to the barrel import files, nothing will warn you about them, things will be exported as "undefined" and crash deep into Nestjs or Graphql. The "finishGather" thing I posted was a red herring.