Skip to content

Commit

Permalink
test(sdk): make e2e tests more configurable
Browse files Browse the repository at this point in the history
Check the `TEST_MERMAIDCHART_BASE_URL` and
`TEST_MERMAIDCHART_PROJECT_ID` environment variables, so that different
mermaidchart accounts can be used.
  • Loading branch information
aloisklink committed Mar 5, 2024
1 parent 766066b commit 5815f19
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions packages/sdk/src/index.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,57 @@ import process from 'node:process';
import { AxiosError } from 'axios';
import { MCDocument } from './types.js';

// hard-coded, replace with your own project,
// or ask alois is you want this to be shared with your account
const testProjectId = '316557b3-cb6f-47ed-acf7-fcfb7ce188d5';
let testProjectId = '316557b3-cb6f-47ed-acf7-fcfb7ce188d5';
let baseURL = new URL('https://test.mermaidchart.com');

let client: MermaidChart;

beforeAll(async() => {
if (process.env.TEST_MERMAIDCHART_BASE_URL) {
try {
baseURL = new URL(process.env.TEST_MERMAIDCHART_BASE_URL);
} catch (err) {
throw new Error("Invalid URL in environment variable TEST_MERMAIDCHART_BASE_URL", { cause: err});
}
} else {
process.emitWarning(`Missing environment variable TEST_MERMAIDCHART_BASE_URL. Defaulting to ${baseURL.href}.`);
}

if (!process.env.TEST_MERMAIDCHART_API_TOKEN) {
throw new Error(
"Missing required environment variable TEST_MERMAIDCHART_API_TOKEN. "
+ "Please go to https://test.mermaidchart.com/app/user/settings and create one."
+ `Please go to ${new URL('/app/user/settings', baseURL)} and create one.`
);
}

client = new MermaidChart({
clientID: '00000000-0000-0000-0000-000000git000test',
baseURL: 'https://test.mermaidchart.com',
baseURL: baseURL.href,
redirectURI: 'https://localhost.invalid',
});

await client.setAccessToken(process.env.TEST_MERMAIDCHART_API_TOKEN);

const projects = await client.getProjects();

// confirm that testProjectId is valid
if (process.env.TEST_MERMAIDCHART_PROJECT_ID) {
testProjectId = process.env.TEST_MERMAIDCHART_PROJECT_ID;
if (!projects.find((project) => project.id === testProjectId)) {
throw new Error(
`Missing environment variable TEST_MERMAIDCHART_PROJECT_ID. `
+ `Please go to ${new URL('/app/projects', baseURL)} and create one.`
);
}
} else {
if (!projects.find((project) => project.id === testProjectId)) {
throw new Error(
`Missing environment variable TEST_MERMAIDCHART_PROJECT_ID. `
+ `Please go to ${new URL('/app/projects', baseURL)} and create one.`
);
}
process.emitWarning(`Missing optional environment variable TEST_MERMAIDCHART_PROJECT_ID. Defaulting to ${testProjectId}`);
}
});

describe('getUser', () => {
Expand Down

0 comments on commit 5815f19

Please sign in to comment.