Skip to content

Commit 5815f19

Browse files
committedMar 5, 2024
test(sdk): make e2e tests more configurable
Check the `TEST_MERMAIDCHART_BASE_URL` and `TEST_MERMAIDCHART_PROJECT_ID` environment variables, so that different mermaidchart accounts can be used.
1 parent 766066b commit 5815f19

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed
 

‎packages/sdk/src/index.e2e.test.ts

+35-5
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,57 @@ import process from 'node:process';
88
import { AxiosError } from 'axios';
99
import { MCDocument } from './types.js';
1010

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

1514
let client: MermaidChart;
1615

1716
beforeAll(async() => {
17+
if (process.env.TEST_MERMAIDCHART_BASE_URL) {
18+
try {
19+
baseURL = new URL(process.env.TEST_MERMAIDCHART_BASE_URL);
20+
} catch (err) {
21+
throw new Error("Invalid URL in environment variable TEST_MERMAIDCHART_BASE_URL", { cause: err});
22+
}
23+
} else {
24+
process.emitWarning(`Missing environment variable TEST_MERMAIDCHART_BASE_URL. Defaulting to ${baseURL.href}.`);
25+
}
26+
1827
if (!process.env.TEST_MERMAIDCHART_API_TOKEN) {
1928
throw new Error(
2029
"Missing required environment variable TEST_MERMAIDCHART_API_TOKEN. "
21-
+ "Please go to https://test.mermaidchart.com/app/user/settings and create one."
30+
+ `Please go to ${new URL('/app/user/settings', baseURL)} and create one.`
2231
);
2332
}
2433

2534
client = new MermaidChart({
2635
clientID: '00000000-0000-0000-0000-000000git000test',
27-
baseURL: 'https://test.mermaidchart.com',
36+
baseURL: baseURL.href,
2837
redirectURI: 'https://localhost.invalid',
2938
});
3039

3140
await client.setAccessToken(process.env.TEST_MERMAIDCHART_API_TOKEN);
41+
42+
const projects = await client.getProjects();
43+
44+
// confirm that testProjectId is valid
45+
if (process.env.TEST_MERMAIDCHART_PROJECT_ID) {
46+
testProjectId = process.env.TEST_MERMAIDCHART_PROJECT_ID;
47+
if (!projects.find((project) => project.id === testProjectId)) {
48+
throw new Error(
49+
`Missing environment variable TEST_MERMAIDCHART_PROJECT_ID. `
50+
+ `Please go to ${new URL('/app/projects', baseURL)} and create one.`
51+
);
52+
}
53+
} else {
54+
if (!projects.find((project) => project.id === testProjectId)) {
55+
throw new Error(
56+
`Missing environment variable TEST_MERMAIDCHART_PROJECT_ID. `
57+
+ `Please go to ${new URL('/app/projects', baseURL)} and create one.`
58+
);
59+
}
60+
process.emitWarning(`Missing optional environment variable TEST_MERMAIDCHART_PROJECT_ID. Defaulting to ${testProjectId}`);
61+
}
3262
});
3363

3464
describe('getUser', () => {

0 commit comments

Comments
 (0)
Failed to load comments.