Ajv can’t resolve reference #/components/schemas/other from id test

I am getting the error when using the Ajv to load a schema that uses $ref imported from a yaml file.

This is my ymal file:

openapi: 3.0.3
info:
  title: Demo
  version: 1.0.0
paths:
  /carousel:
    get:
      responses:
        "200":
          description: Successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/test"
components:
  schemas:
    other:
      type: object
      properties:
        component:
          type: object
    test:
      type: object
      properties:
        slides:
          type: array
          items:
            type: object
            properties:
              contents:
                type: array
                items:
                  anyOf:
                    - $ref: "#/components/schemas/other"

This is my code (JS + Vite)

import Ajv from 'ajv'
import YamlContent from '../Config/API.yaml'; //vite.config.js with package @modyfi/vite-plugin-yaml
const validate =
  new Ajv({
    schemas: YamlContent.components.schemas
  }).
  getSchema(YamlContent.components.schemas.test);

I also tried:

const validate =
  new Ajv().
  addSchema(YamlContent.components.schemas.other).
  compile(YamlContent.components.schemas.test);

But it always gives the same error.
What am i missing here? Thanks.