Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-tester

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-tester - npm Package Compare versions

Comparing version 1.1.1 to 2.0.0

153

dist/index.js

@@ -17,2 +17,6 @@ 'use strict';

var _pathExists = require('path-exists');
var _pathExists2 = _interopRequireDefault(_pathExists);
var _lodash = require('lodash.merge');

@@ -49,5 +53,7 @@

var fullDefaultConfig = {
parserOpts: { parser: recast.parse },
generatorOpts: { generator: recast.print, lineTerminator: '\n' },
babelrc: false
babelOptions: {
parserOpts: { parser: recast.parse },
generatorOpts: { generator: recast.print, lineTerminator: '\n' },
babelrc: false
}
};

@@ -66,4 +72,14 @@

fixtures = _ref.fixtures,
rest = _objectWithoutProperties(_ref, ['plugin', 'pluginName', 'title', 'tests', 'fixtures']);
filename = _ref.filename,
rest = _objectWithoutProperties(_ref, ['plugin', 'pluginName', 'title', 'tests', 'fixtures', 'filename']);
if (fixtures) {
testFixtures(_extends({
plugin,
pluginName,
title: describeBlockTitle,
fixtures,
filename
}, rest));
}
var testAsArray = toTestArray(tests);

@@ -81,4 +97,5 @@ if (!testAsArray.length) {

var _merge = (0, _lodash2.default)({}, testerConfig, toTestConfig({ testConfig, index, plugin, pluginName, fixtures })),
modifier = _merge.modifier,
var _merge = (0, _lodash2.default)({}, testerConfig, toTestConfig({ testConfig, index, plugin, pluginName, filename })),
skip = _merge.skip,
only = _merge.only,
title = _merge.title,

@@ -88,6 +105,13 @@ code = _merge.code,

output = _merge.output,
snapshot = _merge.snapshot;
snapshot = _merge.snapshot,
error = _merge.error;
if (modifier) {
it[modifier](title, tester);
(0, _assert2.default)(!skip && !only || skip !== only, 'Cannot enable both skip and only on a test');
if (skip) {
// eslint-disable-next-line jest/no-disabled-tests
it.skip(title, tester);
} else if (only) {
// eslint-disable-next-line jest/no-focused-tests
it.only(title, tester);
} else {

@@ -97,2 +121,3 @@ it(title, tester);

// eslint-disable-next-line complexity
function tester() {

@@ -103,6 +128,22 @@ (0, _invariant2.default)(code, _commonTags.oneLine`

`);
if (snapshot) {
(0, _invariant2.default)(!output, '`output` cannot be provided with `snapshot: true`');
(0, _invariant2.default)(!babelOptions.babelrc || babelOptions.filename, 'babelrc set to true, but no filename specified in babelOptions');
(0, _invariant2.default)(!snapshot || !output, '`output` cannot be provided with `snapshot: true`');
var result = void 0;
var errored = false;
try {
result = babel.transform(code, babelOptions).code.trim();
} catch (err) {
if (error) {
errored = true;
result = err;
} else {
throw err;
}
}
var result = babel.transform(code, babelOptions).code.trim();
var expectedToThrowButDidNot = error && !errored;
(0, _assert2.default)(!expectedToThrowButDidNot, 'Expected to throw error, but it did not.');
if (snapshot) {

@@ -116,2 +157,4 @@ (0, _invariant2.default)(result !== code, _commonTags.oneLine`

expect(`\n${formattedOutput}\n`).toMatchSnapshot(title);
} else if (error) {
assertError(error, result);
} else if (output) {

@@ -127,2 +170,37 @@ _assert2.default.equal(result, output, 'Output is incorrect.');

function testFixtures(_ref2) {
var plugin = _ref2.plugin,
describeBlockTitle = _ref2.title,
fixtures = _ref2.fixtures,
filename = _ref2.filename,
rest = _objectWithoutProperties(_ref2, ['plugin', 'title', 'fixtures', 'filename']);
describe(`${describeBlockTitle} fixtures`, function () {
var fixturesDir = getPath(filename, fixtures);
_fs2.default.readdirSync(fixturesDir).forEach(function (caseName) {
it(caseName.split('-').join(' '), function () {
var fixtureDir = _path2.default.join(fixturesDir, caseName);
var codePath = _path2.default.join(fixtureDir, 'code.js');
var babelRcPath = _path2.default.join(fixtureDir, '.babelrc');
var _merge2 = (0, _lodash2.default)({}, fullDefaultConfig, {
babelOptions: {
plugins: [plugin],
// if they have a babelrc, then we'll let them use that
// otherwise, we'll just use our simple config
babelrc: _pathExists2.default.sync(babelRcPath)
}
}, rest),
babelOptions = _merge2.babelOptions;
var actual = babel.transformFileSync(codePath, babelOptions).code.trim();
var output = _fs2.default.readFileSync(_path2.default.join(fixtureDir, 'output.js'), 'utf8').trim();
_assert2.default.equal(output, actual, 'actual output does not match output.js');
});
});
});
}
function toTestArray(tests) {

@@ -145,8 +223,8 @@ tests = tests || []; // null/0/false are ok, so no default param

function toTestConfig(_ref2) {
var testConfig = _ref2.testConfig,
index = _ref2.index,
plugin = _ref2.plugin,
pluginName = _ref2.pluginName,
fixtures = _ref2.fixtures;
function toTestConfig(_ref3) {
var testConfig = _ref3.testConfig,
index = _ref3.index,
plugin = _ref3.plugin,
pluginName = _ref3.pluginName,
filename = _ref3.filename;

@@ -160,9 +238,11 @@ if (typeof testConfig === 'string') {

_testConfig$code = _testConfig.code,
code = _testConfig$code === undefined ? getCode(fixtures, fixture) : _testConfig$code,
code = _testConfig$code === undefined ? getCode(filename, fixture) : _testConfig$code,
_testConfig$fullTitle = _testConfig.fullTitle,
fullTitle = _testConfig$fullTitle === undefined ? `${index + 1}. ${title || pluginName}` : _testConfig$fullTitle,
_testConfig$output = _testConfig.output,
output = _testConfig$output === undefined ? getCode(fixtures, testConfig.outputFixture) : _testConfig$output;
output = _testConfig$output === undefined ? getCode(filename, testConfig.outputFixture) : _testConfig$output;
return (0, _lodash2.default)({}, testConfig, {
return (0, _lodash2.default)({
babelOptions: { filename: getPath(filename, fixture) }
}, testConfig, {
babelOptions: { plugins: [plugin] },

@@ -175,13 +255,34 @@ title: fullTitle,

function getCode(fixtures, fixture) {
function getCode(filename, fixture) {
if (!fixture) {
return '';
}
var fullPath = fixture;
if (!_path2.default.isAbsolute(fixture)) {
fullPath = _path2.default.join(fixtures, fixture);
return _fs2.default.readFileSync(getPath(filename, fixture), 'utf8');
}
function getPath(filename, basename) {
if (!basename) {
return undefined;
}
return _fs2.default.readFileSync(fullPath, 'utf8');
if (_path2.default.isAbsolute(basename)) {
return basename;
}
return _path2.default.join(_path2.default.dirname(filename), basename);
}
// eslint-disable-next-line complexity
function assertError(error, result) {
if (typeof error === 'function') {
if (!(result instanceof error || error(result) === true)) {
throw result;
}
} else if (typeof error === 'string') {
_assert2.default.equal(result.message, error, 'Error message is incorrect');
} else if (error instanceof RegExp) {
(0, _assert2.default)(error.test(result.message), `Expected ${result.message} to match ${error}`);
} else {
(0, _invariant2.default)(typeof error === 'boolean', 'The given `error` must be a function, string, boolean, or RegExp');
}
}
function requiredParam(name) {

@@ -188,0 +289,0 @@ throw new Error(`${name} is a required parameter.`);

6

package.json
{
"name": "babel-plugin-tester",
"version": "1.1.1",
"version": "2.0.0",
"description": "Utilities for testing babel plugins",

@@ -23,2 +23,3 @@ "main": "dist/index.js",

"lodash.merge": "^4.6.0",
"path-exists": "^3.0.0",
"recast": "^0.12.3",

@@ -69,3 +70,4 @@ "strip-indent": "^2.0.0"

"/node_modules/",
"__fixtures__"
"__fixtures__",
"__helpers__"
],

@@ -72,0 +74,0 @@ "coverageThreshold": {

@@ -103,11 +103,37 @@ # babel-plugin-tester

#### filename
Relative paths from the other options will be relative to this. Normally you'll
provide this as `filename: __filename`. The only `options` property affected by
this value is `fixtures`. Test Object properties affected by this value are:
`fixture` and `outputFixture`. If those properties are not
absolute paths, then they will be `path.join`ed with `path.dirname` of the
`filename`.
#### fixtures
This is used in combination with the test object's `fixture` and `outputFixture`
options. This is used as the base directory with which to resolve relative
paths for those options.
This is a path to a directory with this format:
Note: you really only need to specify this option if one of your test objects
uses `fixture` or `outputFixture` without absolute paths.
```
__fixtures__
├── first-test # test title will be: "first test"
│   ├── code.js # required
│   └── output.js # required
└── second-test
├── .babelrc # optional
├── code.js
└── output.js
```
With this you could make your test config like so:
```javascript
pluginTester({
plugin,
fixtures: path.join(__dirname, '__fixtures__'),
})
```
And it would run two tests. One for each directory in `__fixtures__`.
#### tests

@@ -163,3 +189,3 @@

here. If it's an absolute path, that's the file that will be loaded, otherwise,
this will be `path.join`ed with the `fixtures` path.
this will be `path.join`ed with the `filename` path.

@@ -171,2 +197,12 @@ #### outputFixture

#### only
To run only this test. Useful while developing to help focus on a single test.
Can be used on multiple tests.
#### skip
To skip running this test. Useful for when you're working on a feature that is
not yet supported.
#### snapshot

@@ -179,2 +215,22 @@

#### error
If a particular test case should be throwing an error, you can that using one
of the following:
```javascript
{
// ...
error: true,
error: 'should have this exact message',
error: /should pass this regex/,
error: SyntaxError, // should be instance of this constructor
error: err => {
if (err instanceof SyntaxError && /message/.test(err.message)) {
return true; // test will fail if function doesn't return `true`
}
},
}
```
## Examples

@@ -199,3 +255,3 @@

// only necessary if you use fixture or outputFixture in your tests
fixtures: path.join(__dirname, '__fixtures__'),
filename: __filename,

@@ -249,7 +305,7 @@ // these will be `lodash.merge`d with the test objects

// `fixtures` path provided above
fixture: 'changed.js',
fixture: '__fixtures__/changed.js',
// because outputFixture is provided, the assertion will be that the
// plugin will change the contents of `changed.js` to the contents of
// `changed-output.js`
outputFixture: 'changed-output.js',
outputFixture: '__fixtures__/changed-output.js',
},

@@ -256,0 +312,0 @@ {

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