0.1.3 • Published 3 years ago

esm-world v0.1.3

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

This is an ES module loader for BDD-style testing.

/* index.js */

let instance = null;

export class MySingleton {
  constructor() {
    if (instance !== null)
      throw new Error('Only a single instance is supported');

    instance = this;
  }

  sayFoo() {
    console.log('Foo!');
  }

  sayBar() {
    console.log('Bar!');
  }
}
/* test.js */

import { strict as assert } from 'assert';

import { createWorld } from 'esm-world';

let mockConsole = new (class MockConsole {
  log(message) {
    this.message = message;
  }
})();

describe('MySingleton', () => {
  let instance = null;

  beforeEach(async () => {
    // Load module in a new world.
    let { MySingleton } = await createWorld('./index.js', { globals: { console: mockConsole } });

    instance = new MySingleton();
  });

  describe('#sayFoo()', () => {
    it('should say foo', () => {
      instance.sayFoo();

      assert.equal(mockConsole.message, 'Foo!');
    });
  });

  describe('#sayBar()', () => {
    it('should say bar', () => {
      instance.sayBar();

      assert.equal(mockConsole.message, 'Bar!');
    });
  });
});

It needs the --experimental-vm-modules option to Node.js and is meant for testing with the Mocha framework only for now.

Under development.

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago