index.test.js
1.99 KB
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
const path = require(`path`);
let PnpWebpackPlugin = require(`./index`);
function makeResolver(resolverPlugins, options = {}) {
const {
NodeJsInputFileSystem,
CachedInputFileSystem,
ResolverFactory
} = require('enhanced-resolve');
const resolver = ResolverFactory.createResolver({
fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
extensions: ['.js', '.json'],
... options,
});
for (const {apply} of resolverPlugins)
apply(resolver);
return resolver;
}
function makeRequest(resolver, request, issuer) {
return new Promise((resolve, reject) => {
resolver.resolve({}, issuer, request, {}, (err, filepath) => {
if (err) {
reject(err);
} else {
resolve(filepath);
}
});
});
}
describe(`Regular Plugin`, () => {
it(`should correctly resolve a relative require`, async () => {
const resolver = makeResolver([PnpWebpackPlugin]);
const resolution = await makeRequest(resolver, `./index.js`, __dirname);
expect(resolution).toEqual(path.normalize(`${__dirname}/index.js`));
});
it(`shouldn't prevent the 'extensions' option from working`, async () => {
const resolver = makeResolver([PnpWebpackPlugin]);
const resolution = await makeRequest(resolver, `./index`, __dirname);
expect(resolution).toEqual(path.normalize(`${__dirname}/index.js`));
});
it(`shouldn't prevent the 'alias' option from working`, async () => {
const resolver = makeResolver([PnpWebpackPlugin], {alias: {[`foo`]: `./fixtures/index.js`}});
const resolution = await makeRequest(resolver, `foo`, __dirname);
expect(resolution).toEqual(path.normalize(`${__dirname}/fixtures/index.js`));
});
it(`shouldn't prevent the 'modules' option from working`, async () => {
const resolver = makeResolver([PnpWebpackPlugin], {modules: [`./fixtures`]});
const resolution = await makeRequest(resolver, `file`, __dirname);
expect(resolution).toEqual(path.normalize(`${__dirname}/fixtures/file.js`));
});
});