Module Resolver Spec - SMotaal/specs GitHub Wiki

Save this file in make-importer/src/resolve.spec.js

import { makeRootedResolver } from '.';

export const testRootedResolve = ({ test, thrownFrom }) => {
  const resolve = makeRootedResolver('file:///some/where/over');
  const referrer = 'file:///some/where/over/rainbow/place.js';
    
  const dotFoo = resolve('./foo', referrer);
  await test(() => dotFoo === 'file:///some/where/over/rainbow/foo');
 
  const dotDotRoot = resolve('../root.js', referrer);
  await test(() => dotDotRoot === 'file:///some/where/over/root.js');

  const superRootThrows = thrownFrom(() => resolve('./foo/../../../superroot.js', referrer));
  await test(() => superRootThrows instanceof TypeError);

  const dotWithSlash = resolve('.', 'file:///some/where/over/');
  await test(() => dotWithSlash === 'file:///some/where/over/');

  const dotWithReferrer = resolve('.', referrer);
  await test(() => dotWithReferrer === 'file:///some/where/over/rainbow/');

  const explicitDirectory = resolve('./foo/', referrer);
  await test(() => explicitDirectory === 'file:///some/where/over/rainbow/foo/');

  const trailingDot = resolve('./foo/.', referrer);
  await test(() => trailingDot === 'file:///some/where/over/rainbow/foo/');

  const twoDots = resolve('..', `${referrer}/bar`);
  await test(() => twoDots === 'file:///some/where/over/rainbow/');

  const trailingTwoDots = resolve('./foo/..', referrer);
  await test(() => trailingTwoDots === 'file:///some/where/over/rainbow/');
};

Alternative without await:

import { makeRootedResolver } from '.';

export const testRootedResolve = ({ test, thrownFrom }) => {
  const resolve = makeRootedResolver('file:///some/where/over');
  const referrer = 'file:///some/where/over/rainbow/place.js';
    
  return Promise.resolve()
    .then(_ => {
      const dotFoo = resolve('./foo', referrer);
      return test(() => dotFoo === 'file:///some/where/over/rainbow/foo');
    }).then(_ => {
      const dotDotRoot = resolve('../root.js', referrer);
      return test(() => dotDotRoot === 'file:///some/where/over/root.js');
    }).then(_ => {
      const superRootThrows = thrownFrom(() => resolve('./foo/../../../superroot.js', referrer));
      return test(() => superRootThrows instanceof TypeError);
    }).then(_ => {
      const dotWithSlash = resolve('.', 'file:///some/where/over/');
      return test(() => dotWithSlash === 'file:///some/where/over/');
    }).then(_ => {
      const dotWithReferrer = resolve('.', referrer);
      return test(() => dotWithReferrer === 'file:///some/where/over/rainbow/');
    }).then(_ => {
      const explicitDirectory = resolve('./foo/', referrer);
      return test(() => explicitDirectory === 'file:///some/where/over/rainbow/foo/');
    }).then(_ => {
      const trailingDot = resolve('./foo/.', referrer);
      return test(() => trailingDot === 'file:///some/where/over/rainbow/foo/');
    }).then(_ => {
      const twoDots = resolve('..', `${referrer}/bar`);
      return test(() => twoDots === 'file:///some/where/over/rainbow/');
    }).then(_ => {
      const trailingTwoDots = resolve('./foo/..', referrer);
      return test(() => trailingTwoDots === 'file:///some/where/over/rainbow/');
    });
  };