Skip to content

Commit

Permalink
(test): add initial test harness and one simple test
Browse files Browse the repository at this point in the history
- 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
      - jestjs/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
  • Loading branch information
agilgur5 committed Dec 1, 2019
1 parent a497b63 commit a5690f6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ npm-debug.log*
node_modules/

build/
coverage/
*.tgz

.DS_Store
Expand Down
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -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')
}
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@
"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"
},
"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"
}
}
17 changes: 17 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit a5690f6

Please sign in to comment.