Skip to content

Commit

Permalink
fix(window): avoid global.window redefinition (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore committed Apr 28, 2022
1 parent f1b318b commit 505c62f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: build
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 14
- 16
- 18
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Mock `canvas` when run unit test cases with jest. For more browser environment, you can use [jest-electron](https://github.com/hustcc/jest-electron) for real browser runtime.
[![Build Status](https://travis-ci.org/hustcc/jest-canvas-mock.svg?branch=master)](https://travis-ci.org/hustcc/jest-canvas-mock)
[![Build Status](https://github.com/hustcc/jest-canvas-mock/workflows/build/badge.svg)](https://github.com/hustcc/jest-canvas-mock/actions/workflows/build.yml)
[![Coverage Status](https://coveralls.io/repos/github/hustcc/jest-canvas-mock/badge.svg?branch=master)](https://coveralls.io/github/hustcc/jest-canvas-mock)
[![npm](https://img.shields.io/npm/v/jest-canvas-mock.svg)](https://www.npmjs.com/package/jest-canvas-mock)
[![npm](https://img.shields.io/npm/dm/jest-canvas-mock.svg)](https://www.npmjs.com/package/jest-canvas-mock)
Expand Down
12 changes: 12 additions & 0 deletions __mocks__/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
global.window.Worker = class {
constructor(stringUrl) {
this.url = stringUrl;
this.onmessage = () => {};
}

postMessage(msg) {
this.onmessage(msg);
}
};

global.window.URL.createObjectURL = jest.fn();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"src/mock/**/*.js"
],
"setupFiles": [
"./src/index.js"
"./src/index.js",
"./__mocks__/worker.js"
]
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import mockWindow from './window';
// mock global window
// TODO: Force coverage to ignore this branch
if (typeof window !== 'undefined') {
global.window = mockWindow(window);
mockWindow(global.window);
}

export const ver = '__VERSION__';

0 comments on commit 505c62f

Please sign in to comment.