babel/karma-babel-preprocessor

Write tests in ES6 without systemjs modules

jonricaurte opened this issue · 11 comments

Hi,

I wrote my app using systemjs and babel, and I am writing my specs in ES6 using the babel preprocessor. Here is my config:

preprocessors: {
'/*.slim': ['slim', 'ng-html2js'],
'
/*.es6': ['babel']
},

babelPreprocessor: {
  options: {
    modules: 'system',
    moduleIds: true
  },
  filename: function(file) {
    return file.originalPath.replace(/.*?javascripts\//, '').replace(/\.es6$/, '.js');
  }
},

My specs are getting added to systemjs modules, which means I have to add a separate application.test.es6 file and separate system.conf.js file where I do:

System.import('unit/application.test');

Then I have to include all my karma tests in the application.test file and import them all like so:

import 'unit/app/home.spec';
import 'unit/app/some_other.spec';

I do not want my specs to be added in system modules, just maybe iife's so I do not have to import all my tests. Is it possible to do this?

Thanks.

Hi, it looks like a question about systemjs stack rather than this module. Please ask it somewhere related to systemjs.

Hi @shuhei,

Sorry for not being clear. What I want to do is use the babel preprocessor to compile both my app and my tests. I want the babel preprocessor to compile my app ES6 files to SystemJS modules like it already does when I run the app. However, I want the babel preprocessor to compile my tests with the "babel --modules ignore" so it loads my tests not in SystemJS modules, but just in normal javascript functions so the tests get run right away without having to import anything. I guess for now I can use both the babel preprocessor for compiling my app to SystemJS es6 modules and the traceur compiler for compiling my tests to normal javascript ES5 functions.

Thanks.

Can we reopen this issue?

Thanks.

So, do you want to specify different modules option to each file? You may still need to import app files from test files though.

Also, karma-systemjs seems to allow us to use systemjs and es6 easily. https://www.npmjs.com/package/karma-systemjs

@shuhei, yes that would be great. For file types that are **/*!(spec).es6 ['babel'] and use a specified module and options. For spec files that are **/*spec.es6 ['babel-ignore'] use normal javascript functions. Since my app is an angular app, I can import the top level application.js so the app loads all the systemjs modules (which in turn load all the angular modules) and their dependencies. Then in my karma test I can run beforeEach(angular.mock.module('anyModuleName');

It seems like it might be a common enough feature to have something like 'babel-ignore'. What are your thoughts?

It would be nice to allow users to specify all options as functions like filename and sourceRoot so that they can craft them file by file.

I think that is a great idea! Then the user can do any kind of matching they want. So I could check to see if the file contains .spec and if it does use ignore for module otherwise use systemjs.

module: function(file) {
return file.originalPath.indexOf('.spec') != -1 ? 'ignore' : 'systemjs';
}

Published 5.1.0. Please close this issue if it works. Thanks!

It works perfectly! Thanks! Now I don't have to have a file where I import all my tests and have a system.conf.test.js file. Can you make a new release and tag?

Thanks!

Ah, I forgot to push the tag. Thanks!