index.test.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const path = require(`path`);
  2. let PnpWebpackPlugin = require(`./index`);
  3. function makeResolver(resolverPlugins, options = {}) {
  4. const {
  5. NodeJsInputFileSystem,
  6. CachedInputFileSystem,
  7. ResolverFactory
  8. } = require('enhanced-resolve');
  9. const resolver = ResolverFactory.createResolver({
  10. fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
  11. extensions: ['.js', '.json'],
  12. ... options,
  13. });
  14. for (const {apply} of resolverPlugins)
  15. apply(resolver);
  16. return resolver;
  17. }
  18. function makeRequest(resolver, request, issuer) {
  19. return new Promise((resolve, reject) => {
  20. resolver.resolve({}, issuer, request, {}, (err, filepath) => {
  21. if (err) {
  22. reject(err);
  23. } else {
  24. resolve(filepath);
  25. }
  26. });
  27. });
  28. }
  29. describe(`Regular Plugin`, () => {
  30. it(`should correctly resolve a relative require`, async () => {
  31. const resolver = makeResolver([PnpWebpackPlugin]);
  32. const resolution = await makeRequest(resolver, `./index.js`, __dirname);
  33. expect(resolution).toEqual(path.normalize(`${__dirname}/index.js`));
  34. });
  35. it(`shouldn't prevent the 'extensions' option from working`, async () => {
  36. const resolver = makeResolver([PnpWebpackPlugin]);
  37. const resolution = await makeRequest(resolver, `./index`, __dirname);
  38. expect(resolution).toEqual(path.normalize(`${__dirname}/index.js`));
  39. });
  40. it(`shouldn't prevent the 'alias' option from working`, async () => {
  41. const resolver = makeResolver([PnpWebpackPlugin], {alias: {[`foo`]: `./fixtures/index.js`}});
  42. const resolution = await makeRequest(resolver, `foo`, __dirname);
  43. expect(resolution).toEqual(path.normalize(`${__dirname}/fixtures/index.js`));
  44. });
  45. it(`shouldn't prevent the 'modules' option from working`, async () => {
  46. const resolver = makeResolver([PnpWebpackPlugin], {modules: [`./fixtures`]});
  47. const resolution = await makeRequest(resolver, `file`, __dirname);
  48. expect(resolution).toEqual(path.normalize(`${__dirname}/fixtures/file.js`));
  49. });
  50. });