Socket
Socket
Sign inDemoInstall

riot-jest-transformer

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

riot-jest-transformer - npm Package Compare versions

Comparing version 1.0.8 to 1.0.10

2

package.json
{
"name": "riot-jest-transformer",
"version": "1.0.8",
"version": "1.0.10",
"description": "Jest transformer for testing riot tags",

@@ -5,0 +5,0 @@ "main": "src/main.js",

@@ -29,2 +29,4 @@ ![Build Status](https://travis-ci.org/tompascall/riot-jest-transformer.svg?branch=master) [![Coverage Status](https://coveralls.io/repos/github/tompascall/riot-jest-transformer/badge.svg?branch=master)](https://coveralls.io/github/tompascall/riot-jest-transformer?branch=master)

Let's suppose that you saved the Jest config file to the project root directory as `jest.config`. In this case you should run jest with --config option: `jest --config jest.config`
#### Usage

@@ -57,12 +59,22 @@

The transformer uses *babel-core* module by default as transformer for the compiled tag. You can also use other transformer by configuring it in `.riot-jest-transformer` config file in the root of your project directory. The form of the config file must be like this:
The transformation has two steps:
- it compiles the tag with Riot's compiler
- it transformes the compiled tag in order to be able to run it when imported
The transformer uses *babel-core* module by default as transformer for the compiled tag (and uses the .babelrc for babel configuration). You can also use other transformer by configuring it in the `riot-jest-transformer.json` config file in the root of your project directory. In the latter case the form of the config file must be like this:
```js
{
transformer: <transformer module name or path, it will be required by Jest> : String,
method: <the used method of transformer module> : String,
args: <arguments for method> : Array
"transformer": <transformer module name or path, it will be required by Jest> : String, required
"method": <the used method of transformer module> : String, required
"args": <arguments for method> : Array, optional
}
```
##### Notes:
- If you'd like to use babel-core for transformation, and options provided in .babelrc is enough for you, you do not need to create riot-jest-transformer.json file, the transformer just works out of the box
- if you provide a configuration in `riot-jest-transformer.json` and want to use `babel-core` as transformer (maybe with special options for riot tags), the first argument must be an object in `args`, because transformer method of `babel-core` needs an options object for transformation.
- If you want to use other transformer module than babel-core, you have to give the proper path in `transformer` attribute. If the transformer module is from an npm package, it is enough to give the name of the module, otherwise you have to provide the path of the module from project root directory
#### Demo

@@ -69,0 +81,0 @@

@@ -13,3 +13,3 @@ import { process, transformer } from '../main';

const configPath = `${path.resolve('.riot-jest-transformer')}`;
const configPath = `${path.resolve('riot-jest-transformer.json')}`;

@@ -24,3 +24,3 @@ describe('riot-jest-transformer', function() {

describe('isertRiot', () => {
describe('insertRiot', () => {
it('should insert riot dependency into compiled tag', () => {

@@ -63,3 +63,3 @@ const hello2 = `

it('should use .riot-jest-transformer file as config if it exists', () => {
it('should use riot-jest-transformer.json file as config if it exists', () => {
let config = {

@@ -76,4 +76,90 @@ transformer: "babel-core",

});
it('should throw informative error message if conf file is not in json format', () => {
let config = `{
transformer: "babel-core",
method: 'transform',
args: [{}]
}`;
fs.writeFileSync(configPath, config, {encoding: 'utf8'});
let callGetConfig = () => {
transformer.getConfig({ filename: 'fakeFile' });
}
expect(callGetConfig).toThrow('The content of the config file must be in JSON format');
fs.unlinkSync(configPath);
});
});
describe('validateConfig', function() {
const callValidateConfig = () => {
transformer.validateConfig(config);
};
let config;
it('should check type of config object', () => {
config = [];
expect(callValidateConfig).toThrow('riot-jest-transformer config must provide an object');
});
it('should check existence of compulsory config options', () => {
config = {};
expect(callValidateConfig).toThrow('riot-jest-transformer config must define the name or path of the "transformer" module, the "method" of the transformer to be called, and optionally "args" ie. arguments of the method');
});
it('should check type of transformer and method', () => {
config = {
transformer: {},
method: []
};
expect(callValidateConfig).toThrow('"transformer" and "method" in riot-jest-transformer config must be string');
});
it('should check type of args', () => {
config = {
transformer: 'fakeModule',
method: 'fakeMethod',
args: 'arg1, arg2'
};
expect(callValidateConfig).toThrow('If you define "args" in riot-jest-transformer config, it must be an array of arguments');
});
// {
// transformer: "babel-core",
// method: 'transform',
// args: ["trallala"]
// };
it('should throw informative error message if transformer is babel-core and args[0] in conf is not an object', () => {
let config = {
transformer: "babel-core",
method: 'transform',
args: ["trallala"]
};
let callValidateConfig = () => {
transformer.validateConfig(config);
}
expect(callValidateConfig).toThrow('If you want to use babel-core for transformation, you have to provide an object as first element of your args array');
});
it('should not throw error if transformer is not babel-core and args[0] is not an object', () => {
let config = {
transformer: "abel-babel",
method: 'label',
args: ['kabel']
};
let callValidateConfig = () => {
transformer.validateConfig(config);
}
expect(callValidateConfig).not.toThrow();
});
});
describe('getTransformed', () => {

@@ -109,2 +195,44 @@

});
// **************
it('should throw informative error message if transformer module cannot be required', () => {
let compiled = transformer.getCompiled(hello);
let fakeNodeModule2 = `
exports.fakeFormer = 'not_a_function';
`
fs.writeFileSync(path.resolve('node_modules/fakeNodeModule2.js'), fakeNodeModule2, {encoding: 'utf8'});
let result;
const config = {
compiled,
transformer: 'not_existent_module',
method: 'fakeFormer',
};
const callGetTransformed = () => {
result = transformer.getTransformed(config);
};
expect(callGetTransformed).toThrow(`The ${config.transformer} transformer module in your riot-jest-transformer config cannot be required (it might not have been installed or it has a wrong path in config)`);
fs.unlinkSync(path.resolve('node_modules/fakeNodeModule2.js'));
});
it('should throw informative error message if method is not a function', () => {
let compiled = transformer.getCompiled(hello);
let fakeNodeModule3 = `
exports.fakeFormer = 'not_a_function';
`
fs.writeFileSync(path.resolve('node_modules/fakeNodeModule3.js'), fakeNodeModule3, {encoding: 'utf8'});
let result;
const callGetTransformed = () => {
result = transformer.getTransformed({
compiled,
transformer: 'fakeNodeModule3',
method: 'fakeFormer',
});
};
expect(callGetTransformed).toThrow('You should provide a function of transformer as "method" in your riot-jest-transformer config');
fs.unlinkSync(path.resolve('node_modules/fakeNodeModule3.js'));
});
});

@@ -111,0 +239,0 @@

@@ -5,3 +5,3 @@ const riot = require('riot');

const fs = require('fs');
const CONFIG_PATH = `${path.resolve('.riot-jest-transformer')}`;
const CONFIG_PATH = `${path.resolve('riot-jest-transformer.json')}`;
const RIOT_PROCESSOR = 'riot.tag2';

@@ -38,3 +38,9 @@

if (fs.existsSync(CONFIG_PATH)) {
config = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
try {
config = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
}
catch (e) {
throw Error("The content of the config file must be in JSON format");
}
if (Array.isArray(config.args) &&

@@ -50,6 +56,38 @@ {}.toString.call(config.args[0]) == '[object Object]'

validateConfig (config) {
if ({}.toString.call(config) !== "[object Object]") {
throw Error('riot-jest-transformer config must provide an object');
}
if (!config.hasOwnProperty('transformer') || !config.hasOwnProperty('method')) {
throw Error('riot-jest-transformer config must define the name or path of the "transformer" module, the "method" of the transformer to be called, and optionally "args" ie. arguments of the method');
}
if (typeof config.transformer !== 'string' || typeof config.method !== 'string') {
throw Error('"transformer" and "method" in riot-jest-transformer config must be string');
}
if (config.hasOwnProperty('args') && !Array.isArray(config.args)) {
throw Error('If you define "args" in riot-jest-transformer config, it must be an array of arguments');
}
if (config.transformer === 'babel-core' && {}.toString.call(config.args[0]) !== '[object Object]') {
throw Error("If you want to use babel-core for transformation, you have to provide an object as first element of your args array");
}
},
getTransformed ({ compiled, transformer, method, args = [] } = {}) {
if (transformer && method) {
const transformerByParam = require(transformer);
return transformerByParam[method](compiled, ...args);
let transformerByParam;
try {
transformerByParam = require(transformer);
}
catch (e) {
throw Error(`The ${transformer} transformer module in your riot-jest-transformer config cannot be required (it might not have been installed or it has a wrong path in config)`);
}
const methodFunction = transformerByParam[method];
if (typeof methodFunction !== 'function') {
throw Error('You should provide a function of transformer as "method" in your riot-jest-transformer config');
}
return methodFunction(compiled, ...args);
}

@@ -56,0 +94,0 @@ return transform(compiled, ...args);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc