How to use getMarkdown method in Jest

Best JavaScript code snippet using jest

Run Jest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

routes.js

Source: routes.js Github

copy
1const parallelPromise = require('./lib/parallelPromise')
2const getMarkdown = require('./lib/getMarkdown')
3const getArticles = require('./lib/getArticles')
4
5module.exports = {
6  '/': (params) => [
7    'start',
8    () => new Promise((resolve, reject) => {
9      parallelPromise([
10        getMarkdown('start'),
11        getMarkdown('skills'),
12        getMarkdown('references')
13      ])
14        .then((mdArr) => {
15          resolve({ sections: mdArr.map((md) => md[0].html) })
16        })
17        .catch(reject)
18    })
19  ],
20  '/projects': (params) => [
21    'projects',
22    () => new Promise((resolve, reject) => {
23      getMarkdown('projects/*')
24        .then((mdArr) => resolve({ projects: mdArr.map((md) => md.html) }))
25        .catch(reject)
26    })
27  ],
28  '/anotherblog': (params) => [
29    'blog',
30    () => new Promise((resolve, reject) => {
31      getArticles('blog')
32        .then((articles) => resolve({ articles }))
33        .catch(reject)
34    })
35  ],
36  '/anotherblog/:article': (params) => [
37    'blogArticle',
38    () => new Promise((resolve, reject) => {
39      getArticles('blog')
40        .then((articles) =>
41          resolve(articles.filter((el) => el.name === params.article)[0])
42        )
43        .catch(reject)
44    }),
45    `blogArticle/${params.article}`
46  ],
47  '/ipa': (params) => [
48    'generic',
49    () => new Promise((resolve, reject) => {
50      getMarkdown('dispo_interaktiver_news_artikel')
51        .then((md) => resolve({
52          title: 'Interaktiver News-Artikel',
53          generic: md[0].html
54        }))
55        .catch(reject)
56    }),
57    'ipa'
58  ],
59  '/peopleidliketoworkwith': (params) => [
60    'generic',
61    () => new Promise((resolve, reject) => {
62      getMarkdown('peopleidliketoworkwith')
63        .then((md) => resolve({
64          title: 'peopleidliketoworkwith',
65          generic: md[0].html
66        }))
67        .catch(reject)
68    })
69  ],
70  '/blacklist': (params) => [
71    'generic',
72    () => new Promise((resolve, reject) => {
73      resolve({
74        title: 'blacklist',
75        generic: 'YOLO'
76      })
77    })
78  ]
79}
80
81
Full Screen

buildMarkdown.spec.js

Source: buildMarkdown.spec.js Github

copy
1import documentation from 'documentation';
2import buildMarkdown from './';
3
4function getMarkdown(documentationSource, args) {
5  return documentation
6    .build([{ source: documentationSource }], { shallow: true })
7    .then(comments => buildMarkdown(comments, args));
8}
9
10describe('buildMarkdown', () => {
11  describe('for class', () => {
12    it('should render markdown for class with constructor', async () => {
13      const documentationSource = `
14      /**
15      * Component description
16      * 
17      * @example
18      * new Component({visible: true})
19      */
20      class Component {
21        /**
22        * creates new Component
23        */ 
24        constructor(config: any) {}
25      
26        /**
27        * show
28        */
29        show() {}
30      }
31    `;
32      const markdown = await getMarkdown(documentationSource);
33
34      expect(markdown).toMatchSnapshot();
35    });
36  });
37
38  describe('for API class', () => {
39    const title = 'API Methods';
40
41    it('should render markdown for getters/setters', async () => {
42      const documentationSource = `
43      /** Example class jsdoc */
44      class API {
45        /**
46        * \`isVisible\` getter comment
47        */
48        get isVisible(): boolean {
49            return true;
50        }
51        
52        /**
53        * \`width\` setter comment
54        */
55        set width(value: number) {
56            console.log(value)
57        }
58      }
59    `;
60      const markdown = await getMarkdown(documentationSource, { title });
61
62      expect(markdown).toMatchSnapshot();
63    });
64
65    it('should render markdown for methods default value', async () => {
66      const documentationSource = `
67      /** Example class jsdoc */
68      class API {
69        /**
70        * myMethod comment
71        * @param [x=''] - \`x\` description 
72        * 
73        */
74        myMethod(x: string) {}
75      }
76    `;
77      const markdown = await getMarkdown(documentationSource, { title });
78
79      expect(markdown).toMatchSnapshot();
80    });
81
82    it('should render markdown for methods with @returns comment', async () => {
83      const documentationSource = `
84      /** Example class jsdoc */
85      class API {
86        /**
87        * myMethod comment
88        * @param x - \`x\` description 
89        * 
90        * @returns returns comment
91        */
92        myMethod(x: string): string {
93          return x;
94        }
95      }
96    `;
97      const markdown = await getMarkdown(documentationSource, { title });
98
99      expect(markdown).toMatchSnapshot();
100    });
101
102    it('should render markdown for methods with UnionType with literals as @param type', async () => {
103      const documentationSource = `
104      /** Example class jsdoc */
105      class API {
106        /**
107        * myMethod comment
108        * @param y - \`y\` comment
109        */
110        myMethod(y: 'bla' | 'not bla' | 7 | null) {
111          return y;
112        }
113      }
114    `;
115      const markdown = await getMarkdown(documentationSource, { title });
116
117      expect(markdown).toMatchSnapshot();
118    });
119
120    it('should render markdown for methods with UnionType with NOT literal as @param type', async () => {
121      const documentationSource = `
122      /** Example class jsdoc */
123      class API {
124        /**
125        * myMethod comment
126        * @param y - \`y\` comment
127        */
128        myMethod(y: number | null) {
129          return y;
130        }
131      }
132    `;
133      const markdown = await getMarkdown(documentationSource, { title });
134
135      expect(markdown).toMatchSnapshot();
136    });
137
138    it('should render markdown for methods with @example end @note', async () => {
139      const documentationSource = `
140      /** Example class jsdoc */
141      class API {
142        /**
143        * myMethod comment
144        * @param x - \`x\` description. see http://example.com
145        * 
146        * @note
147        * \`myMethod\` note 1
148        *
149        * @example <caption>test example caption</caption>
150        * console.log('example')
151        *
152        * @example
153        * console.log('example')
154        *
155        * @note
156        * \`myMethod\` note 2
157        * Read more about [myMethod](/my-method)
158        *
159        */
160        myMethod(x: string): string {
161          return x;
162        }
163      }
164    `;
165      const markdown = await getMarkdown(documentationSource, { title });
166
167      expect(markdown).toMatchSnapshot();
168    });
169
170    it('should render markdown for methods with @deprecated', async () => {
171      const documentationSource = `
172      /** Example class jsdoc */
173      class API {
174        /**
175        * show component 
176        * @deprecated use method \`show()\`
177        */
178        doShow() {}
179        
180        /**
181        * show component
182        */
183        show() {}
184      }
185    `;
186      const markdown = await getMarkdown(documentationSource, { title });
187
188      expect(markdown).toMatchSnapshot();
189    });
190
191    it('should render markdown for methods with `...rest` as param', async () => {
192      const documentationSource = `
193      /** Example class jsdoc */
194      class API {
195        /**
196        * myMethod comment
197        */
198        myMethod(...arr: string[]): string[] {
199          return arr;
200        }
201      }
202    `;
203      const markdown = await getMarkdown(documentationSource, { title });
204
205      expect(markdown).toMatchSnapshot();
206    });
207
208    it('should render markdown for methods with `optional` as param', async () => {
209      const documentationSource = `
210      /** Example class jsdoc */
211      class API {
212        /**
213        * myMethod comment
214        */
215        myMethod(x: ?number): number {
216          return number;
217        }
218      }
219    `;
220      const markdown = await getMarkdown(documentationSource, { title });
221
222      expect(markdown).toMatchSnapshot();
223    });
224
225    it('should render markdown for methods interface as return type', async () => {
226      const documentationSource = `
227     /**
228      * MyResult interface
229      * @property a - \`a\` description
230      * @property b - \`b\` description
231      */
232      interface MyResult {
233        a: string;
234        b?: number;
235      }
236
237      /** Example class jsdoc */
238      class API {
239        /**
240        * myMethod comment
241        * @param y - \`y\` comment
242        */
243        myMethod(y: 'bla' | 'not bla'): MyResult {
244          return y;
245        }
246      }
247    `;
248      const markdown = await getMarkdown(documentationSource, { title });
249
250      expect(markdown).toMatchSnapshot();
251    });
252
253    it('should render markdown sections in valid order', async () => {
254      const documentationSource = `
255      /** Example class jsdoc */
256      class API {
257        /**
258        * myMethod1 comment
259        * @param x - \`x\` description
260        * 
261        * @example <caption>test example caption</caption>
262        * console.log('example')
263        * @note
264        * \`myMethod1\` note 
265        * Read more about [myMethod](/my-method)
266        *
267        * @returns returns comment
268        */
269        myMethod1(x: string): string {
270          return x;
271        }
272        
273        /**
274        * myMethod2 comment
275        * @param y - y comment
276        * 
277        * @example
278        * console.log('example')
279        */
280        myMethod2(y: 'bla' | 'not bla'): string {
281          return y;
282        }
283      }
284    `;
285      const markdown = await getMarkdown(documentationSource, { title });
286
287      expect(markdown).toMatchSnapshot();
288    });
289  });
290
291  describe('for functions', async () => {
292    const title = 'Functions';
293
294    it('should render valid markdown', async () => {
295      const documentationSource = `
296      /**
297      * MyFuncResult interface
298      * @property a - \`a\` description
299      * @property b - \`b\` description
300      */
301      interface MyFuncResult {
302        a: string;
303        b: number;
304      }
305
306      /**
307      * myFunc1 description
308      * @param x - \`x\` description 
309      */
310      function myFunc1(x: number): number {}
311
312      /**
313      * myFunc2 description
314      * @param x - \`x\` description 
315      * @param y - \`y\` description
316      *
317      * @example <caption>MyFunc example</caption>
318      * 
319      * console.log(myFunc(3, 2))
320      * // {a: '3', b: 2}
321      */
322      function myFunc2(x: number, y:number): MyFuncResult {}
323    `;
324      const markdown = await getMarkdown(documentationSource, { title });
325
326      expect(markdown).toMatchSnapshot();
327    });
328
329    it('should render valid markdown with function as argument', async () => {
330      const documentationSource = `
331        /**
332        * @param name - event name 
333        * @param fn - event handler 
334        */
335        function myFunc(name: string, fn: (name: string, data: Object) => any) {}
336      `;
337      const markdown = await getMarkdown(documentationSource, { title });
338
339      expect(markdown).toMatchSnapshot();
340    });
341
342    it('should render valid markdown with UnionType in TypeApplication as argument', async () => {
343      const documentationSource = `
344        /**
345        * @param fn - event handler 
346        */
347        function myFunc(fn: (value: string | null) => void) {}
348      `;
349      const markdown = await getMarkdown(documentationSource, { title });
350
351      expect(markdown).toMatchSnapshot();
352    });
353
354    it('should render valid markdown with function as return type', async () => {
355      const documentationSource = `
356        /**
357        * @returns myFunc result function
358        */
359        function myFunc(): (flag: boolean) => void {}
360      `;
361      const markdown = await getMarkdown(documentationSource, { title });
362
363      expect(markdown).toMatchSnapshot();
364    });
365
366    it('should render valid markdown with function in return type interface', async () => {
367      const documentationSource = `
368        /**
369        * @property update - update entity
370        * @property destroy - destroy entity
371        */
372        interface MyEntity {
373          id: string;
374          title: string;
375          updateTitle(id: string, title: string): Promise<any>;
376          destroy(): void;
377        }
378      
379        /**
380        * create new entity
381        * @param title - entity title
382        * @returns new entity
383        */
384        function create(title: string): MyEntity {}
385      `;
386      const markdown = await getMarkdown(documentationSource, { title });
387
388      expect(markdown).toMatchSnapshot();
389    });
390  });
391
392  describe('with ApplicationType as return type', () => {
393    const title = 'API Methods';
394
395    it('should render markdown for methods with Promise as @returns type', async () => {
396      const documentationSource = `
397      /**
398      * @returns returns comment
399      */
400      function myFn(): Promise<string> {
401        return x;
402      }
403      `;
404      const markdown = await getMarkdown(documentationSource, { title });
405
406      expect(markdown).toMatchSnapshot();
407    });
408
409    it('should render markdown for methods with Promise & interface as @returns type', async () => {
410      const documentationSource = `
411      /** */
412      interface IResult {
413        x: number;
414        y: number;
415      }
416
417      /**
418      * @returns returns comment
419      */
420      function myFn(): Promise<IResult> {
421        return {x: 1, y: 1};
422      }
423      `;
424      const markdown = await getMarkdown(documentationSource, { title });
425
426      expect(markdown).toMatchSnapshot();
427    });
428
429    it('should render markdown for methods with Array as @returns type', async () => {
430      const documentationSource = `
431      /**
432      * @returns returns comment
433      */
434      function myFn(): string[] {
435        return [''];
436      }
437      `;
438      const markdown = await getMarkdown(documentationSource, { title });
439
440      expect(markdown).toMatchSnapshot();
441    });
442
443    it('should render markdown for methods with Array & interface as @returns type', async () => {
444      const documentationSource = `
445      /** */
446      interface IResult {
447        x: number;
448        y: number;
449      }
450
451      /**
452      * @returns returns comment
453      */
454      function myFn(): IResult[] {
455        return [{x: 1, y: 1}];
456      }
457      `;
458      const markdown = await getMarkdown(documentationSource, { title });
459
460      expect(markdown).toMatchSnapshot();
461    });
462  });
463});
464
Full Screen

JsDoc.spec.js

Source: JsDoc.spec.js Github

copy
1import React from 'react';
2import JsDoc, { getMarkdown } from './JsDoc';
3
4const tags = {
5	deprecated: [
6		{
7			title: 'description',
8			description: 'Use *another* method',
9		},
10	],
11	version: [
12		{
13			title: 'version',
14			description: '2.0.0',
15		},
16	],
17	since: [
18		{
19			title: 'since',
20			description: '1.0.0',
21		},
22	],
23	author: [
24		{
25			title: 'author',
26			description: '[Author 1](#TestLink)',
27		},
28		{
29			title: 'author',
30			description: '[Author 2](#TestLink2)',
31		},
32	],
33	see: [
34		{
35			title: 'see',
36			description: '[See 1](#TestLink)',
37		},
38		{
39			title: 'see',
40			description: '[See 2](#TestLink2)',
41		},
42	],
43	link: [
44		{
45			title: 'link',
46			description: '[Link 1](#TestLink)',
47		},
48	],
49};
50
51describe('getMarkdown', () => {
52	it('should return Markdown for all tags', () => {
53		const result = getMarkdown(tags);
54		expect(result).toMatchSnapshot();
55	});
56
57	it('should return Markdown for one author', () => {
58		const result = getMarkdown({
59			author: [tags.author[0]],
60		});
61		expect(result).toMatchSnapshot();
62	});
63
64	it('should return Markdown for multiple authors', () => {
65		const result = getMarkdown({
66			author: tags.author,
67		});
68		expect(result).toMatchSnapshot();
69	});
70});
71
72describe('JsDoc', () => {
73	it('should render Markdown', () => {
74		const actual = shallow(<JsDoc {...tags} />);
75
76		expect(actual).toMatchSnapshot();
77	});
78
79	it('should render null for empty tags', () => {
80		const actual = shallow(<JsDoc />);
81
82		expect(actual.getElement()).toBe(null);
83	});
84});
85
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Jest on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)