diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..c13c5f6 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} diff --git a/.gitignore b/.gitignore index df9001d..a7fb6a2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ npm-debug.log* node_modules/ build/ +coverage/ *.tgz .DS_Store diff --git a/.travis.yml b/.travis.yml index 79b0ee6..e837c80 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,4 +2,5 @@ language: node_js # default is apparently 0.10.48 node_js: '10.16.0' -script: npm run test:pub +script: npm test +after_script: npm run test:pub diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..2267c25 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,6 @@ +module.exports = { + transform: { + // use babel-jest@23 for babel@6 support (https://github.com/facebook/jest/issues/8230#issuecomment-479470547) + '\\.js$': require.resolve('babel-jest') + } +} diff --git a/package.json b/package.json index 5b82df3..8625e51 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "lint": "standard index.js", "build:watch": "webpack --watch -d", "build:prod": "webpack -p", - "test": "echo \"Error: no test specified\" && exit 1", + "test": "jest", "test:pub": "npm run build:prod && npm pack", "pub": "npm run build:prod && npm publish", "changelog": "changelog-maker" @@ -37,8 +37,11 @@ "devDependencies": { "@agilgur5/changelog-maker": "^3.0.0", "babel-core": "^6.0.14", + "babel-jest": "^23.6.0", "babel-loader": "^6.0.0", "babel-preset-es2015": "^6.6.0", + "canvas-prebuilt": "^1.6.11", + "jest": "^24.9.0", "webpack": "^1.12.2" } } diff --git a/test/index.spec.js b/test/index.spec.js new file mode 100644 index 0000000..d0e7582 --- /dev/null +++ b/test/index.spec.js @@ -0,0 +1,17 @@ +import trimCanvas from '../index.js' + +describe('trimCanvas', () => { + it('should trim whitespace', () => { + const canvas = document.createElement('canvas') + const ctx = canvas.getContext('2d') + canvas.width = 1000 + canvas.height = 1000 + + ctx.fillStyle = 'purple' + ctx.fillRect(450, 450, 100, 100) // 100x100 purple box in center + + trimCanvas(canvas) + expect(canvas.width).toBe(100) + expect(canvas.height).toBe(100) + }) +})