From a5690f678647bddf47b9ad39ecf2716b01d99f2f Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sat, 30 Nov 2019 12:19:37 -0500 Subject: [PATCH] (test): add initial test harness and one simple test - ensure that width/height of canvas are trimmed down after drawing a purple rectangle in the center - add test script that uses jest - (ci): change CI to run test and test:pub - configure jest and babel-jest - add coverage/ directory to gitignore - add babel-jest@23 for Babel 6 support - and configure it for .js files due to a jest bug - add .babelrc to configure babel-jest - can't use .babelrc.js as babel-jest@23 doesn't support it - facebook/jest#5324 - can't use .babelrc for babel-loader (have to duplicate config) because babel-loader@6 has some bugs with it and babel-loader@7 doesn't support webpack@1 - babel/babel-loader#552 (deps): add jest, babel-jest, and canvas-prebuilt to devDeps - add canvas-prebuilt@1 to support jest's jsdom v11 - canvas is used by jsdom for, well, canvas interactions --- .babelrc | 3 +++ .gitignore | 1 + .travis.yml | 3 ++- jest.config.js | 6 ++++++ package.json | 5 ++++- test/index.spec.js | 17 +++++++++++++++++ 6 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 .babelrc create mode 100644 jest.config.js create mode 100644 test/index.spec.js 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) + }) +})