Skip to content

Commit f8a1430

Browse files
authoredApr 22, 2024
Merge pull request #18 from Mermaid-Chart/test/make-e2e-sdk-tests-more-configurable
test(sdk): make e2e tests more configurable
2 parents c3f6a65 + b062b59 commit f8a1430

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed
 

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

+43-10
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+
`Invalid environment variable TEST_MERMAIDCHART_PROJECT_ID. `
50+
+ `Please go to ${new URL('/app/projects', baseURL)} and create one that you have access to.`
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', () => {
@@ -125,17 +155,20 @@ describe('deleteDocument', () => {
125155
});
126156

127157
describe("getDocument", () => {
128-
it("should get publicly shared diagram", async() => {
158+
it("should get diagram", async() => {
159+
const newDocument = await client.createDocument(testProjectId);
160+
161+
documentsToDelete.add(newDocument.documentID);
162+
129163
const latestDocument = await client.getDocument({
130-
// owned by alois@mermaidchart.com
131-
documentID: '8bce727b-69b7-4f6e-a434-d578e2b363ff',
164+
documentID: newDocument.documentID,
165+
// major and minor are optional
132166
});
133167

134168
expect(latestDocument).toStrictEqual(documentMatcher);
135169

136170
const earliestDocument = await client.getDocument({
137-
// owned by alois@mermaidchart.com
138-
documentID: '8bce727b-69b7-4f6e-a434-d578e2b363ff',
171+
documentID: newDocument.documentID,
139172
major: 0,
140173
minor: 1,
141174
});

0 commit comments

Comments
 (0)
Failed to load comments.