-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.helpers.js
30 lines (22 loc) · 1.34 KB
/
test.helpers.js
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
28
29
30
/* eslint-disable max-params */
import { resolve } from 'node:path';
export const testImportFromLocal = async (t, importFrom, dir, file, ext, expected = 'unicorn') => {
const extString = ext ? `.${ext}` : '';
const relativeModuleId = `./${file}${extString}`;
const absoluteModuleId = resolve(dir, `${file}${extString}`);
const nonExistentModuleId = `./nonexistent${extString}`;
t.deepEqual(await importFrom.silent(dir, relativeModuleId), expected);
t.is(await importFrom.silent(dir, absoluteModuleId), expected);
const moduleNotFoundError = await t.throwsAsync(importFrom(dir, nonExistentModuleId));
t.is(moduleNotFoundError.code, 'MODULE_NOT_FOUND');
t.regex(moduleNotFoundError.message, new RegExp(`^Cannot find module '${nonExistentModuleId}'`));
t.is(await importFrom.silent(dir, nonExistentModuleId), undefined);
};
export const testImportFromPackage = async (t, importFrom, dir, packageName, expected) => {
const nonExistentPackageName = 'nonexistent-package';
t.deepEqual(await importFrom.silent(dir, packageName), expected);
const moduleNotFoundError = await t.throwsAsync(importFrom(dir, nonExistentPackageName));
t.is(moduleNotFoundError.code, 'MODULE_NOT_FOUND');
t.regex(moduleNotFoundError.message, new RegExp(`^Cannot find module '${nonExistentPackageName}'`));
t.is(await importFrom.silent(dir, nonExistentPackageName), undefined);
};