Skip to content

loadYaml

Jason Godesky edited this page Jul 22, 2026 · 2 revisions

loadYaml loads the contents of a YAML file and parses them into an object.

Parameters

src: string | URL

The path or URL to the YAML file you’d like to load.

Returns

The contents of the YAML file, parsed as an object.

Testing

You can mock YAML files in your tests like so:

import { describe, expect, it, vi } from 'vitest'
import { readFileSync } from 'node:fs'
import yourFunctionThatUsesLoadYaml from './func.ts'

vi.mock('node:fs', async importOriginal => ({
  ...await importOriginal<typeof import('node:fs')>(),
  readFileSync: vi.fn()
}))

describe('yourFunctionThatUsesLoadYaml', () => {
  it('loads YAML', () => {
    vi.mocked(readFileSync).mockReturnValue('id: abc123\nname: Test File')
    const { id, name } = yourFunctionThatUsesLoadYaml()
    expect(id).toBe('abc123')
    expect(name).toBe('Test File')
  })
})

Examples

import { loadYaml } from '@revolutionarygamesco/common'

const { id, name } = loadYaml<{ id: string, name: string }>('./path/to/file.yaml')

Clone this wiki locally