Simple tests for Postman and Yaak importers

This commit is contained in:
Gregory Schier
2024-03-18 13:40:00 -07:00
parent fa46611d76
commit c49e81cde4
5 changed files with 86 additions and 157 deletions

View File

@@ -1,4 +1,4 @@
export function pluginHookImport(contents) {
export function pluginHookImport(contents: string) {
let parsed;
try {
parsed = JSON.parse(contents);
@@ -10,23 +10,20 @@ export function pluginHookImport(contents) {
return undefined;
}
if (!('yaakSchema' in parsed)) {
const isYaakExport = 'yaakSchema' in parsed;
if (!isYaakExport) {
return;
}
// Migrate v1 to v2 -- changes requests to httpRequests
if (parsed.yaakSchema === 1) {
if ('requests' in parsed.resources) {
parsed.resources.httpRequests = parsed.resources.requests;
parsed.yaakSchema = 2;
delete parsed.resources.requests;
}
if (parsed.yaakSchema === 2) {
return { resources: parsed.resources }; // Should already be in the correct format
}
return undefined;
return { resources: parsed.resources }; // Should already be in the correct format
}
export function isJSObject(obj) {
export function isJSObject(obj: any) {
return Object.prototype.toString.call(obj) === '[object Object]';
}

View File

@@ -0,0 +1,29 @@
import { describe, expect, test } from 'vitest';
import { pluginHookImport } from '../src';
describe('importer-yaak', () => {
test('Skips invalid imports', () => {
expect(pluginHookImport('not JSON')).toBeUndefined();
expect(pluginHookImport('[]')).toBeUndefined();
expect(pluginHookImport(JSON.stringify({ resources: {} }))).toBeUndefined();
});
test('converts schema 1 to 2', () => {
const imported = pluginHookImport(
JSON.stringify({
yaakSchema: 1,
resources: {
requests: [],
},
}),
);
expect(imported).toEqual(
expect.objectContaining({
resources: {
httpRequests: [],
},
}),
);
});
});

View File

@@ -4,7 +4,7 @@ import { defineConfig } from 'vite';
export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, 'src/index.js'),
entry: resolve(__dirname, 'src/index.ts'),
fileName: 'index',
formats: ['es'],
},