Adding tests for browser and node
JSZip's browser/ESM support is tenuous, but it appears to support browserify.
This commit is contained in:
parent
c8f566c2fe
commit
c0533dd3d6
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@
|
||||
/package-lock.json
|
||||
/shrinkwrap.yaml
|
||||
/yarn.lock
|
||||
test/jszip.js
|
||||
|
18
package.json
18
package.json
@ -24,9 +24,21 @@
|
||||
},
|
||||
"homepage": "https://cndtr.io/do-not-zip/",
|
||||
"devDependencies": {
|
||||
"rollup": "*"
|
||||
"browserify": "16.2.0",
|
||||
"jszip": "3.1.5",
|
||||
"rollup": "*",
|
||||
"rollup-plugin-commonjs": "9.1.0",
|
||||
"rollup-plugin-node-resolve": "3.3.0",
|
||||
"tap-colorize": "1.2.0",
|
||||
"tape-run": "4.0.0",
|
||||
"zora": "2.0.1"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "rollup -c"
|
||||
}
|
||||
"build": "rollup -c",
|
||||
"build:jszip": "browserify -s jszip node_modules/jszip/lib/index.js > test/jszip.js",
|
||||
"test:browser": "npm run build:jszip && rollup -c rollup.config.test.js | tape-run | tap-colorize",
|
||||
"test:node": "node test/test.node.js | tap-colorize",
|
||||
"test": "npm run build && npm run test:node && npm run test:browser"
|
||||
},
|
||||
"dependencies": {}
|
||||
}
|
||||
|
16
rollup.config.test.js
Normal file
16
rollup.config.test.js
Normal file
@ -0,0 +1,16 @@
|
||||
import commonjs from 'rollup-plugin-commonjs'
|
||||
import nodeResolve from 'rollup-plugin-node-resolve'
|
||||
|
||||
export default {
|
||||
input: `./test/test.browser.js`,
|
||||
output: [
|
||||
{
|
||||
format: `iife`,
|
||||
name: `tests`,
|
||||
},
|
||||
],
|
||||
plugins: [
|
||||
nodeResolve(),
|
||||
commonjs(),
|
||||
],
|
||||
}
|
18
test/helper.js
Normal file
18
test/helper.js
Normal file
@ -0,0 +1,18 @@
|
||||
const JSZip = require(`./jszip.js`)
|
||||
|
||||
module.exports = {
|
||||
loadJzip(data) {
|
||||
return new JSZip().loadAsync(data)
|
||||
},
|
||||
jzipToEntries(jzip) {
|
||||
const ary = []
|
||||
jzip.forEach((path, file) => ary.push({ path, file }))
|
||||
return ary
|
||||
},
|
||||
entriesToObject(entries) {
|
||||
return entries.reduce((acc, { path, file }) => {
|
||||
acc[path] = file
|
||||
return acc
|
||||
}, Object.create(null))
|
||||
},
|
||||
}
|
13
test/test.browser.js
Normal file
13
test/test.browser.js
Normal file
@ -0,0 +1,13 @@
|
||||
const test = require(`zora`)
|
||||
const doNotZip = require(`../`)
|
||||
|
||||
test(`Creates a Blob in the browser`, t => {
|
||||
const outputBlob = doNotZip([
|
||||
{ path: `path/to/file1.txt`, data: `Hello` },
|
||||
{ path: `another/file2.txt`, data: `World` },
|
||||
])
|
||||
|
||||
t.ok(outputBlob instanceof Blob, `output is a Blob`)
|
||||
})
|
||||
|
||||
require(`./test.everywhere.js`)
|
20
test/test.everywhere.js
Normal file
20
test/test.everywhere.js
Normal file
@ -0,0 +1,20 @@
|
||||
const test = require(`zora`)
|
||||
const doNotZip = require(`../`)
|
||||
const { loadJzip, jzipToEntries, entriesToObject } = require(`./helper.js`)
|
||||
|
||||
test(`Creates a zip file that jszip can read`, async t => {
|
||||
const outputBlob = doNotZip([
|
||||
{ path: `path/to/file1.txt`, data: `Hello` },
|
||||
{ path: `another/file2.txt`, data: `World` },
|
||||
])
|
||||
const entries = jzipToEntries(await loadJzip(outputBlob))
|
||||
|
||||
const expectedPaths = [ `path/to/file1.txt`, `another/file2.txt` ]
|
||||
|
||||
t.equal(entries.length, expectedPaths.length)
|
||||
|
||||
const jzipMap = entriesToObject(entries)
|
||||
|
||||
expectedPaths.forEach(expectedPath => expectedPath in jzipMap)
|
||||
})
|
||||
|
13
test/test.node.js
Normal file
13
test/test.node.js
Normal file
@ -0,0 +1,13 @@
|
||||
const test = require(`zora`)
|
||||
const doNotZip = require(`../`)
|
||||
|
||||
test(`Creates a Buffer in node`, t => {
|
||||
const outputBlob = doNotZip([
|
||||
{ path: `path/to/file1.txt`, data: `Hello` },
|
||||
{ path: `another/file2.txt`, data: `World` },
|
||||
])
|
||||
|
||||
t.ok(outputBlob instanceof Buffer, `output is a Buffer`)
|
||||
})
|
||||
|
||||
require(`./test.everywhere.js`)
|
Loading…
Reference in New Issue
Block a user