-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutils.ts
27 lines (23 loc) · 1007 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { randomBytes } from 'crypto';
import { Project } from 'ts-morph';
import { migrateFile } from '../migrator';
export const project = new Project({ useInMemoryFileSystem: true });
export const createSourceFile = (content = undefined as string | undefined, ext = 'ts') => {
const randomString = randomBytes(5).toString('hex');
return project.createSourceFile(`./${randomString}.${ext}`, content);
};
export const expectMigration = async (sourceCode: string, targetCode: string) : Promise<void> => {
const sourceFile = createSourceFile(sourceCode.replaceAll(' ', ''));
const migratedFile = await migrateFile(project, sourceFile);
expect(migratedFile.getText().replaceAll(' ', ''))
.toBe(targetCode.replaceAll(' ', ''));
};
export const expectMigrationToThrow = async (
sourceCode: string,
errorMessage: string,
) : Promise<void> => {
const sourceFile = createSourceFile(sourceCode);
await expect(migrateFile(project, sourceFile))
.rejects
.toThrow(errorMessage);
};