1import { resolveWithTsConfigPaths } from '../resolveWithTsConfigPaths';
2
3describe(resolveWithTsConfigPaths, () => {
4  it('resolves a simple alias', () => {
5    const resolver = jest.fn(() => ({} as any));
6    expect(
7      resolveWithTsConfigPaths(
8        {
9          paths: {
10            '@foo/*': ['foo/*'],
11          },
12          baseUrl: '.',
13        },
14        {
15          moduleName: '@foo/bar',
16          originModulePath: './foo/bar',
17        },
18        resolver
19      )
20    ).not.toEqual(null);
21
22    expect(resolver).toHaveBeenCalledWith('foo/bar');
23  });
24  it('resolves an alias with baseUrl', () => {
25    const resolver = jest.fn(() => ({} as any));
26    expect(
27      resolveWithTsConfigPaths(
28        {
29          paths: {
30            '@foo/*': ['foo/*'],
31          },
32          baseUrl: './src',
33        },
34        {
35          moduleName: '@foo/bar',
36          originModulePath: './foo/bar',
37        },
38        resolver
39      )
40    ).not.toEqual(null);
41
42    expect(resolver).toHaveBeenCalledWith('src/foo/bar');
43  });
44  it(`skips resolving if the origin module is inside the node_modules directory`, () => {
45    const resolver = jest.fn(() => ({} as any));
46    expect(
47      resolveWithTsConfigPaths(
48        {
49          paths: {
50            '@foo/*': ['foo/*'],
51          },
52          baseUrl: '.',
53        },
54        {
55          moduleName: '@foo/bar',
56          originModulePath: './node_modules/foo/bar',
57        },
58        resolver
59      )
60    ).toEqual(null);
61
62    expect(resolver).not.toHaveBeenCalled();
63  });
64  it(`skips resolving if the module name is absolute or relative`, () => {
65    ['/foo/bar', './foo/bar'].forEach((moduleName) => {
66      const resolver = jest.fn(() => ({} as any));
67      expect(
68        resolveWithTsConfigPaths(
69          {
70            paths: {
71              '@foo/*': ['foo/*'],
72            },
73            baseUrl: '.',
74          },
75          {
76            moduleName,
77            originModulePath: '/foo/bar',
78          },
79          resolver
80        )
81      ).toEqual(null);
82
83      expect(resolver).not.toHaveBeenCalled();
84    });
85  });
86});
87