Files
bun.sh/test/js/bun/resolve/yaml/yaml.test.js
Dylan Conway 8fad98ffdb Add Bun.YAML.parse and YAML imports (#22073)
### What does this PR do?
This PR adds builtin YAML parsing with `Bun.YAML.parse`
```js
import { YAML } from "bun";
const items = YAML.parse("- item1");
console.log(items); // [ "item1" ]
```

Also YAML imports work just like JSON and TOML imports
```js
import pkg from "./package.yaml"
console.log({ pkg }); // { pkg: { name: "pkg", version: "1.1.1" } }
```
### How did you verify your code works?
Added some tests for YAML imports and parsed values.

---------

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
2025-08-23 06:55:30 -07:00

70 lines
1.5 KiB
JavaScript

import { expect, it } from "bun:test";
import emptyYaml from "./yaml-empty.yaml";
import yamlFromCustomTypeAttribute from "./yaml-fixture.yaml.txt" with { type: "yaml" };
const expectedYamlFixture = {
framework: "next",
bundle: {
packages: {
"@emotion/react": true,
},
},
array: [
{
entry_one: "one",
entry_two: "two",
},
{
entry_one: "three",
nested: [
{
entry_one: "four",
},
],
},
],
dev: {
one: {
two: {
three: 4,
},
},
foo: 123,
"foo.bar": "baz",
},
};
const expectedYmlFixture = {
framework: "next",
bundle: {
packages: {
"@emotion/react": true,
},
},
};
it("via dynamic import", async () => {
const yaml = (await import("./yaml-fixture.yaml")).default;
expect(yaml).toEqual(expectedYamlFixture);
});
it("via import type yaml", async () => {
expect(yamlFromCustomTypeAttribute).toEqual(expectedYmlFixture);
});
it("via dynamic import with type attribute", async () => {
delete require.cache[require.resolve("./yaml-fixture.yaml.txt")];
const yaml = (await import("./yaml-fixture.yaml.txt", { with: { type: "yaml" } })).default;
expect(yaml).toEqual(expectedYmlFixture);
});
it("empty via import statement", () => {
// Empty YAML file with just a comment should return null
expect(emptyYaml).toBe(null);
});
it("yml extension works", async () => {
const yaml = (await import("./yaml-fixture.yml")).default;
expect(yaml).toEqual(expectedYmlFixture);
});