JSlang.dev

JavaScript Deep Dives with Wolfram. Since 2015
Inclusive, Test-Driven, Spec-Focused, Collaborative.

March 07, 2019
TestsSource
import() as a function

Source Code

The source code we wrote during the event.

import assert from 'assert';

it('import throws when no parameter given', () => {
  assert.throws(() => { eval('import();') });
});
it('import throws when empty string is given', async () => {
  try {
    await import("");
    assert(false);
  } catch(e) {
  }
});
it('import throws when empty string is given', async () => {
  try {
    await import("");
    assert(false);
  } catch(e) {
    // assert.equal("TypeError: Failed to resolve module specifier ''", e.toString()); runs only in Chrome, and we dont know when the string changes, since it is not specified
      
    // assert(e instanceof TypeError); // in the browser we get this
    assert(e instanceof Error); // on node we get this 
  }
});

// use a variable

xit('dynamic import should not throw for an existing valid JS file', () => {
  import('./file.js');
});