Search code examples
typescriptsystemjs

Import 'global' modules with TypeScript


I'm writing tests using Mocha and TypeScript ... here's a quick example:

import {assert} from 'chai';
import Greeting from '../../../src/component/greeting/greeting';

describe('Greeting component', function () {
    it('should greet correctly', function () {
        let greeting = new Greeting();
        assert(greeting.greeting === 'Hello World', 'should express the correct greeting');
    });
});

I can see these being compiled correctly. I am outputting them as common-js modules and using system-js as an in-browser loader.

System.import('component/greeting/greetingSpec.js')
    .catch(console.log.bind(console))
    .then(function() {
        mocha.run();
    })

I'd like to create a file which lists all of the 'spec' files:

import * as greeting from './greeting/greetingSpec';
import * as foo from './greeting/fooSpec';

However the TypeScript compiler can see that these imports are unused, so doesn't include them in the JS output.

How can I define a single 'entry point' to my tests which I can load via system-js?


Solution

  • Try using the side-effect-only import:

    import './greeting/greetingSpec';
    import './greeting/fooSpec';
    

    This will not be elided. See this discussion for more information.