Files
bruno/packages/bruno-converters/tests/insomnia/env-utils.spec.js
Sanjai Kumar e68b2ae3b7 feat: Import Insomnia environments (#5716)
* feat: Implement environment conversion utilities for Insomnia to Bruno migration

fix tests

fix: test

feat: updated `toBrunoEnv` and merging functions to flatten environment data using dot-notation keys. added tests for `buildV5Environments` and `buildV4Environments` to verify flattened key behavior and shallow overrides.

chore: update package-lock.json

refactor: replace `flat` library with custom `flattenObject` utility for improved environment data flattening

chore: remove package-lock.json updates

feat: update `toBrunoEnv` to convert environment values to strings and adjust tests for flattened key behavior in Insomnia environment imports

refactor: update flattening logic to use JavaScript-style square bracket notation for arrays and adjust related tests

feat: enhance insomnia-to-bruno conversion by normalizing variables in requests, and add tests for v4 and v5 environment imports

refactor: improve variable naming and streamline environment building logic in `buildV5Environments` and `buildV4Environments` functions

test: add cleanup step to environment import tests and update expected version for new feature

* revert package-lock.json changes

* test: Add data-testid attributes to environment variable rows in EnvironmentVariables component
2025-10-29 19:04:09 +05:30

101 lines
3.3 KiB
JavaScript

import { describe, it, expect } from '@jest/globals';
import { buildV5Environments, buildV4Environments } from '../../src/insomnia/env-utils';
const getVar = (env, name) => {
return env.variables.find((v) => v.name === name);
};
describe('env-utils', () => {
describe('buildV5Environments', () => {
it('creates base and sub environments with flattened keys and shallow overrides', () => {
const environmentsNode = {
name: 'Base',
data: {
baseurl: 'https://api.example.com',
nested: { name: 'alice', roles: ['admin'] },
numbers: [1, 2]
},
subEnvironments: [
{
name: 'Staging',
data: {
baseurl: 'https://staging.example.com',
nested: { name: 'bob' }
}
},
{ name: 'Dev', data: {} }
]
};
const envs = buildV5Environments(environmentsNode);
expect(envs.length).toBe(3);
const base = envs[0];
const staging = envs[1];
const dev = envs[2];
expect(base.name).toBe('Base');
expect(getVar(base, 'baseurl')?.value).toBe('https://api.example.com');
expect(getVar(base, 'nested.name')?.value).toBe('alice');
expect(getVar(base, 'nested.roles[0]')?.value).toBe('admin');
expect(getVar(base, 'numbers[1]')?.value).toBe('2');
expect(staging.name).toBe('Staging');
// baseurl overridden in sub
expect(getVar(staging, 'baseurl')?.value).toBe('https://staging.example.com');
// nested.name overridden, nested array preserved from base
expect(getVar(staging, 'nested.name')?.value).toBe('bob');
expect(getVar(staging, 'nested.roles[0]')?.value).toBe('admin');
expect(dev.name).toBe('Dev');
// no sub data => inherits base
expect(getVar(dev, 'baseurl')?.value).toBe('https://api.example.com');
expect(getVar(dev, 'nested.name')?.value).toBe('alice');
});
});
describe('buildV4Environments', () => {
it('merges nearest base and sub env data (flattened) into standalone Bruno envs', () => {
const workspaceId = 'wrk_1';
const resources = [
{ _id: workspaceId, _type: 'workspace', name: 'WS' },
{
_id: 'env_base',
_type: 'environment',
parentId: workspaceId,
name: 'Base',
data: {
baseurl: 'https://api.example.com',
user: { name: 'alice' },
arr: [{ id: 1 }]
}
},
{
_id: 'env_sub',
_type: 'environment',
parentId: 'env_base',
name: 'Sub',
data: {
user: { name: 'bob' }
}
}
];
const envs = buildV4Environments(resources, workspaceId);
expect(envs.length).toBe(2);
const base = envs.find((e) => e.name === 'Base');
const sub = envs.find((e) => e.name === 'Sub');
expect(getVar(base, 'baseurl')?.value).toBe('https://api.example.com');
expect(getVar(base, 'user.name')?.value).toBe('alice');
expect(getVar(base, 'arr[0].id')?.value).toBe('1');
// sub should inherit base, override user.name
expect(getVar(sub, 'baseurl')?.value).toBe('https://api.example.com');
expect(getVar(sub, 'user.name')?.value).toBe('bob');
expect(getVar(sub, 'arr[0].id')?.value).toBe('1');
});
});
});