mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-03 07:36:41 +02:00
Merge vitest
This commit is contained in:
44
src/tests/util.ts
Normal file
44
src/tests/util.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
Used to convert jest's Tagged Template literals to object arrays as required by vitest.
|
||||
|
||||
Example:
|
||||
|
||||
Jest code
|
||||
```ts
|
||||
it.each`
|
||||
str | expected
|
||||
${'1d'} | ${moment.duration(1, 'd')}
|
||||
${'2w'} | ${moment.duration(2, 'w')}
|
||||
`('should parse $str to $expected duration', ({ str, expected }) => {
|
||||
expect(yourFunction(str)).toEqual(expected);
|
||||
});
|
||||
```
|
||||
|
||||
Vitest code
|
||||
```ts
|
||||
it.each(convert`
|
||||
str | expected
|
||||
${'1d'} | ${moment.duration(1, 'd')}
|
||||
${'2w'} | ${moment.duration(2, 'w')}
|
||||
`)('should parse $str to $expected duration', ({ str, expected }) => {
|
||||
expect(yourFunction(str)).toEqual(expected);
|
||||
});
|
||||
```
|
||||
*/
|
||||
|
||||
export const convert = (template: TemplateStringsArray, ...params: any[]) => {
|
||||
const header = template[0]
|
||||
.trim()
|
||||
.split('|')
|
||||
.map((s) => s.trim());
|
||||
if (header.length === 0 || params.length % header.length !== 0) {
|
||||
throw new Error('Table column count mismatch');
|
||||
}
|
||||
const chunkSize = header.length;
|
||||
const out = [];
|
||||
for (let i = 0; i < params.length; i += chunkSize) {
|
||||
const chunk = params.slice(i, i + chunkSize);
|
||||
out.push(Object.fromEntries(chunk.map((v, i) => [header[i], v])));
|
||||
}
|
||||
return out;
|
||||
};
|
Reference in New Issue
Block a user