0.3.0 • Published 4 years ago

karmeneo v0.3.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Karmeneo

Karmeneo creates a RequireJS configuration of the Akeneo requirejs.yml files for running tests with Karma.

This is a WIP / PoC project. Use it on your own responsibility. It was tested on Akeneo 3.2.

Why using this?

Creating RequireJS tests for Akeneo with karma are a mess when it comes to add all files to the RequireJS config. Akeneo is using a bundle that enables the user to add new RequireJS files and templates with YAML. To prevent changes in Akeneo AND your test config, Karmeneo will read the config files and create a new RequireJS config with the new changes of the packages automatically and reduces error sources and impediments during development.

Installation

yarn add --dev karmeneo

How does it work?

Currently the Akeneo requirejs.yml configs are going to be created when you run your tests with karma. A file will be created that contains the RequireJS config for your tests.

const requireJsWriter = require('karmeneo');

requireJsWriter.createRequireJsConfig('./tests/requirejs.config.js');

// Karma configuration
module.exports = function(config) {
  config.set({

    basePath: '',

    frameworks: ['jasmine', 'requirejs'],

    files: [
      { pattern: 'web/bundles/**/*.js', included: false },
      { pattern: 'tests/lib/**/*.js', included: false },

      // Add the created RequireJS config to Karma
      'tests/requirejs.config.js',
    ],

Use require to load Karmeneo and run the function createRequireJsConfig at the top of your karma.conf.js. The argument of the function is the name and relative path where the RequireJS config has to be saved. A new config file won't be generated if a file with that name exists.

Be sure to have added web/bundles to the pattern like in the example, because Karmeneo is using the path to the files there.

Additional packages

Not every RequireJS package is configured with Akeneo. But every other package can be added in the configuration with additionalLibs:

const requireJsWriter = require('karmeneo');

const config = {
  additionalLibs: {
    'underscore': '../node_modules/underscore/underscore',
    'backbone': '../node_modules/backbone/backbone',
    'jquery': '../node_modules/jquery/dist/jquery',
  }
};

requireJsWriter.createRequireJsConfig('./tests/requirejs.config.js', config);

// Karma configuration
module.exports = function(config) {
  config.set({

    basePath: '',

    frameworks: ['jasmine', 'requirejs'],

    files: [
      { pattern: 'web/bundles/**/*.js', included: false },
      { pattern: 'web/bundles/**/*.html', included: false },
      { pattern: 'tests/lib/**/*.js', included: false },
      { pattern: 'node_modules/*/*.js', included: false },
      { pattern: 'node_modules/jquery/dist/jquery.js', included: false },

      // Add the created RequireJS config to Karma
      'tests/requirejs.config.js',
    ],

Use in Akeneo 4

Akeneo 4 introduces the changed directory structure of Symfony where the web-directory became public. You can use the configuration key isAkeneo4 and set it to true in that case:

const requireJsWriter = require('karmeneo');

const config = {
  isAkeneo4: true
};

requireJsWriter.createRequireJsConfig('./tests/requirejs.config.js', config);

// Karma configuration
module.exports = function(config) {
  config.set({

Custom RequireJS config template

If the default config template file config.temp doesn't fit to your needs, you can create your own config template and add the relative path to the configuration with template:

const requireJsWriter = require('karmeneo');

const config = {
  template: './tests/my-template.js'
};

requireJsWriter.createRequireJsConfig('./tests/requirejs.config.js', config);

// Karma configuration
module.exports = function(config) {
  config.set({

    basePath: '',

    frameworks: ['jasmine', 'requirejs'],

    files: [
      { pattern: 'web/bundles/**/*.js', included: false },
      { pattern: 'web/bundles/**/*.html', included: false },
      { pattern: 'tests/lib/**/*.js', included: false },
      { pattern: 'node_modules/*/*.js', included: false },
      { pattern: 'node_modules/jquery/dist/jquery.js', included: false },
    
      // Add the created RequireJS config to Karma
      'tests/requirejs.config.js',
    ],

In the example the custom template's file name is ./tests/my-template.js that is relative to the project where you're executing the tests. lodash is used for the templating and has available variables for the packages that you'll have to use in your template:

requirejsFiles

The fetched file paths and ids of Akeneo and your custom ones as the AMD of RequireJS needs.

requirejs.config({

    paths: <%= requirejsFiles %>,

htmlAliasForText

Aliases used in map to handle Akeneo's HTML templates with the text! plugin.

    map: {
        '*': <%= htmlAliasForText %>,
    },