File size: 5,020 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { transformAsync } from '@babel/core';

import plugin from '../extension-resolver';
import fs from 'fs';
jest.mock('fs');

describe('babel-plugin-extension-resolver', () => {
  const pluginOptions = {
    modulesToResolve: ['instantsearch.js'],
  };
  const options = {
    filename: '/path/to/src/file.js',
    configFile: false,
    babelrc: false,
    plugins: [[plugin, pluginOptions]],
  };

  afterEach(() => {
    jest.resetAllMocks();
  });

  it('name=babel-plugin-extension-resolver', () =>
    expect(plugin({ types: {} }, {}).name).toStrictEqual(
      'babel-plugin-extension-resolver'
    ));

  it('ignores empty code', async () => {
    expect(await transformAsync('', options)).toHaveProperty('code', '');
  });

  it('ignores module imports', async () => {
    expect(
      await transformAsync('import path from "path";', options)
    ).toHaveProperty('code', 'import path from "path";');
  });

  it('finds .js files', async () => {
    fs.__setMockFiles([
      '/path/to/src/other.js',
      '/path/to/src/other.ts',
      '/path/to/src/other.tsx',
    ]);

    expect(
      await transformAsync('import other from "./other";', options)
    ).toHaveProperty('code', 'import other from "./other.js";');
  });

  it('finds .ts files', async () => {
    fs.__setMockFiles(['/path/to/src/other.ts', '/path/to/src/other.tsx']);

    expect(
      await transformAsync('import other from "./other";', options)
    ).toHaveProperty('code', 'import other from "./other.js";');
  });

  it('finds .tsx files', async () => {
    fs.__setMockFiles(['/path/to/src/other.tsx']);

    expect(
      await transformAsync('import other from "./other";', options)
    ).toHaveProperty('code', 'import other from "./other.js";');
  });

  it('finds files in parent directory', async () => {
    fs.__setMockFiles(['/path/to/other.js', '/path/to/src/other.js']);

    expect(
      await transformAsync('import other from "../other";', options)
    ).toHaveProperty('code', 'import other from "../other.js";');
  });

  it('finds files in child directory', async () => {
    fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/child/other.js']);

    expect(
      await transformAsync('import other from "./child/other";', options)
    ).toHaveProperty('code', 'import other from "./child/other.js";');
  });

  it('uses index file', async () => {
    fs.__setMockFiles(['/path/to/src/other/index.js']);

    expect(
      await transformAsync('import other from "./other";', options)
    ).toHaveProperty('code', 'import other from "./other/index.js";');
  });

  it('uses modulesToResolve', async () => {
    fs.__setMockFiles(['/path/to/src/other/index.js']);

    expect(
      await transformAsync(
        'import index from "instantsearch.js/es/widgets/index/index";\n' +
          'import root from "instantsearch.js";\n' +
          'import other from "different-module/other";',
        options
      )
    ).toHaveProperty(
      'code',
      'import index from "instantsearch.js/es/widgets/index/index.js";\n' +
        'import root from "instantsearch.js";\n' +
        'import other from "different-module/other";'
    );
  });

  it('works with multiple imports', async () => {
    fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/another.js']);

    expect(
      await transformAsync(
        'import other from "./other";\nimport another from "./another";',
        options
      )
    ).toHaveProperty(
      'code',
      'import other from "./other.js";\nimport another from "./another.js";'
    );
  });

  it('works with export from', async () => {
    fs.__setMockFiles(['/path/to/src/other.js']);

    expect(
      await transformAsync('export * from "./other"', options)
    ).toHaveProperty('code', 'export * from "./other.js";');
  });

  it('ignores require()', async () => {
    fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/another.js']);

    expect(await transformAsync('require("./other");', options)).toHaveProperty(
      'code',
      'require("./other");'
    );
  });

  it('ignores other function calls', async () => {
    fs.__setMockFiles(['/path/to/src/other.js']);

    expect(
      await transformAsync('requireOOPS("./other");', options)
    ).toHaveProperty('code', 'requireOOPS("./other");');
  });

  it('errors if file not found', async () => {
    fs.__setMockFiles([]);

    await expect(() =>
      transformAsync('import other from "./other";', options)
    ).rejects.toThrow(
      '/path/to/src/file.js: import for "./other" could not be resolved'
    );
  });

  it('errors if module file not found', async () => {
    fs.__setMockFiles([]);

    await expect(() =>
      transformAsync(
        'import other from "instantsearch.js/non-existing-folder-this-can-never-exist/qsdf/gh/jklm";',
        options
      )
    ).rejects.toThrow(
      "/path/to/src/file.js: Cannot find module 'instantsearch.js/non-existing-folder-this-can-never-exist/qsdf/gh/jklm' from 'scripts/babel/extension-resolver.js'"
    );
  });
});