Skip to content

Commit

Permalink
Upgrade to babel version 7 and ava version 3 (#111)
Browse files Browse the repository at this point in the history
* Upgrade to babel version 7 and ava version 3

* Improve branch coverage; handle all other Vinyl options like isNull()

* Add latest Node.js to Travis tests

* Have tests fail with less than 100% coverage
  • Loading branch information
JohnAlbin committed Jun 11, 2021
1 parent 2067c1a commit 2eb7a68
Show file tree
Hide file tree
Showing 9 changed files with 5,179 additions and 7,703 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["env"]
"presets": [["@babel/preset-env", {"targets": {"node": "10"}}]]
}
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ language: node_js
node_js:
- 10
- 12
- 14
- node
script: "npm test"
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
12,599 changes: 5,025 additions & 7,574 deletions package-lock.json

Large diffs are not rendered by default.

30 changes: 19 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
},
"main": "dist/index.js",
"scripts": {
"pretest": "xo src",
"prepublishOnly": "del-cli dist && cross-env BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/",
"test": "nyc ava src/__tests__"
"pretest": "xo src tests",
"prepublishOnly": "del-cli dist && cross-env BABEL_ENV=publish babel src --out-dir dist",
"test": "nyc ava"
},
"files": [
"LICENSE-MIT",
Expand All @@ -31,12 +31,11 @@
"svgo": "^1.3.2"
},
"devDependencies": {
"ava": "^0.17.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"babel-register": "^6.26.0",
"chai": "^4.2.0",
"@babel/cli": "^7.13.14",
"@babel/core": "^7.13.14",
"@babel/preset-env": "^7.13.12",
"@babel/register": "^7.13.14",
"ava": "^3.15.0",
"coveralls": "^3.1.0",
"cross-env": "^7.0.2",
"del-cli": "^3.0.1",
Expand All @@ -55,12 +54,21 @@
"prettier": true
},
"ava": {
"require": "babel-register"
"require": "@babel/register",
"verbose": true
},
"nyc": {
"exclude": [
"node_modules",
"**/__tests__"
"tests"
],
"check-coverage": true,
"functions": 100,
"lines": 100,
"branches": 100,
"reporter": [
"text",
"html"
]
}
}
113 changes: 0 additions & 113 deletions src/__tests__/test.js

This file was deleted.

9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ module.exports = function (options) {
}

stream._transform = function (file, encoding, cb) {
if (file.isNull()) {
return cb(null, file);
}

if (file.isStream()) {
return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
Expand All @@ -35,7 +31,12 @@ module.exports = function (options) {
cb(new PluginError(PLUGIN_NAME, error));
}
);

return;
}

// Handle all other cases, like file.isNull(), file.isDirectory().
return cb(null, file);
};

return stream;
Expand Down
File renamed without changes
File renamed without changes
127 changes: 127 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import test from 'ava';
import {readFileSync} from 'fs';
import path from 'path';
import PluginError from 'plugin-error';
import {Readable} from 'stream';
import Vinyl from 'vinyl';
import svgmin from '../src/index.js';

function readFixture(fileName) {
return readFileSync(path.resolve(__dirname, fileName), 'utf8').replace(
/\n$/,
''
);
}

const inputSVG = readFixture('input.svg');
const outputSVG = readFixture('output.svg');

function makeTest(options, file, additionalSetup = () => {}) {
return new Promise((resolve, reject) => {
try {
const transform = svgmin(options);
let result;

// Resolve the promise on the transform's finish or end
// events.
transform.on('finish', () => resolve(result));
transform.on('error', (error) => reject(error));

// Save the transform's data to the Promise's result.
transform.on('data', (data) => {
result = data.contents ? String(data.contents) : data.contents;
});

// Write the test's file to the transform.
const vinylFile = Vinyl.isVinyl(file)
? file
: new Vinyl({contents: Buffer.from(file)});
transform.write(vinylFile);

// Allow individual tests to run additional code before the
// transform 'end' event and to modify the transform or the Vinyl
// file being written.
additionalSetup(transform, vinylFile);

transform.end();
} catch (error) {
reject(error);
}
});
}

test('should let null files pass through', async (t) => {
const result = await makeTest(
null,
new Vinyl({
path: 'null.md',
contents: null,
})
);

t.is(result, null);
});

test('should minify svg with svgo', async (t) => {
const result = await makeTest(null, inputSVG);

t.is(result, outputSVG);
});

test('should honor disabling plugins, such as keeping the doctype', async (t) => {
const result = await makeTest(
{plugins: [{removeDoctype: false}]},
inputSVG
);

t.regex(result, /DOCTYPE/);
});

test('should allow disabling multiple plugins', async (t) => {
const result = await makeTest(
{plugins: [{removeDoctype: false}, {removeComments: false}]},
inputSVG
);

t.regex(result, /DOCTYPE/);
t.regex(result, /test comment/);
});

test('should allow per file options, such as keeping the doctype', async (t) => {
const vinylFile = new Vinyl({contents: Buffer.from(inputSVG)});

const result = await makeTest((file) => {
t.is(file, vinylFile);
return {plugins: [{removeDoctype: false}]};
}, vinylFile);

t.regex(result, /DOCTYPE/);
});

test('in stream mode must emit error', async (t) => {
await t.throwsAsync(
makeTest(
null,
// Create a stream-mode Vinyl object.
new Vinyl({
contents: new Readable(),
}),
(transform, vinylFile) => {
// Write the SVG file to the stream.
vinylFile.contents.write(inputSVG);
vinylFile.contents.end();
}
),
{
instanceOf: PluginError,
message: /Streaming not supported/,
}
);
});

test('stream should emit an error when svgo errors', async (t) => {
await t.throwsAsync(makeTest(null, 'file does not contain an SVG'), {
instanceOf: PluginError,
message: /Error in parsing SVG/,
});
});

0 comments on commit 2eb7a68

Please sign in to comment.