Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pixi explodes on imports in jest #4769

Closed
thomasuster opened this issue Mar 18, 2018 · 17 comments
Closed

Pixi explodes on imports in jest #4769

thomasuster opened this issue Mar 18, 2018 · 17 comments

Comments

@thomasuster
Copy link

thomasuster commented Mar 18, 2018

TypeError: Cannot set property 'fillStyle' of null

screen shot 2018-03-18 at 10 55 13 am

@thomasuster
Copy link
Author

yarn add canvas-prebuilt fixes this, this is an issue with jsdom not auto configured to use canvas.

@dortamiguel
Copy link

dortamiguel commented Jun 2, 2018

works for me 🙂

yarn add -D canvas-prebuilt

@seethroughdev
Copy link

In case anyone else searches for this, here's a little more info:

You have to install the 1.x version of canvas-prebuilt until jsdom/jsdom#1964 is fixed.

yarn add -D canvas-prebuilt@1

after that, this shouldn't be an issue.

sfentress added a commit to concord-consortium/connected-bio-spaces that referenced this issue Nov 13, 2018
sfentress added a commit to concord-consortium/connected-bio-spaces that referenced this issue Nov 13, 2018
sfentress added a commit to concord-consortium/connected-bio-spaces that referenced this issue Nov 14, 2018
@andrevenancio
Copy link
Contributor

andrevenancio commented Nov 16, 2018

you dont need to install the canvas-prebuilt to be honest. Simply create a mock api on your tests setup.

Here's mine, with mocha, enzyme, jsdom.

import { JSDOM } from 'jsdom';
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

const exposedProperties = ['window', 'navigator', 'document'];
const { document } = (new JSDOM('', { url: 'https://your-website-mock.com/' })).window;
global.document = document;
global.window = document.defaultView;
global.requestAnimationFrame = (callback) => {
    setTimeout(callback, 0);
};

Object.keys(document.defaultView).forEach((property) => {
    if (typeof global[property] === 'undefined') {
        exposedProperties.push(property);
        global[property] = document.defaultView[property];
    }
});

global.window.HTMLCanvasElement.prototype.getContext = () => {
    return {
        fillRect: () => {},
        clearRect: () => {},
        getImageData: (x, y, w, h) => {
            return {
                data: new Array(w * h * 4),
            };
        },
        putImageData: () => {},
        createImageData: () => {
            return [];
        },
        setTransform: () => {},
        drawImage: () => {},
        save: () => {},
        fillText: () => {},
        restore: () => {},
        beginPath: () => {},
        moveTo: () => {},
        lineTo: () => {},
        closePath: () => {},
        stroke: () => {},
        translate: () => {},
        scale: () => {},
        rotate: () => {},
        arc: () => {},
        fill: () => {},
        measureText: () => {
            return {
                width: 0,
            };
        },
        transform: () => {},
        rect: () => {},
        clip: () => {},
    };
};

global.window.HTMLCanvasElement.prototype.toDataURL = () => {
    return '';
};


global.navigator = {
    userAgent: 'node.js',
};

@sgb-io
Copy link

sgb-io commented Nov 30, 2018

nice @andrevenancio 😄

@sgb-io
Copy link

sgb-io commented Nov 30, 2018

For anyone googling, this can also affect react-lottie

@andrevenancio
Copy link
Contributor

That's was exactly why I found this issue @sgb-io ! I was trying to use react-lottie 😆

@StephanBijzitter
Copy link

StephanBijzitter commented Dec 21, 2018

I already had canvas-prebuilt installed and used it successfully before, but today it suddenly started throwing this error.

If you already have canvas-prebuilt (and you want to keep it): yarn install --force did the trick for me.

@lk-jeffpeck
Copy link

This worked for me:
https://www.npmjs.com/package/jest-canvas-mock

@NoriSte
Copy link

NoriSte commented Feb 7, 2019

The @lk-jeffpeck solution works like a charm for me!
The previous solutions worked but just for some days, every now and then the problem reappears...

@ZufengW
Copy link

ZufengW commented Jun 6, 2019

This worked for me:
https://www.npmjs.com/package/jest-canvas-mock

Thanks. Worked for me as well.

In case future readers get this problem:
Blindly following their Setup section did not work. I had to add "setupFiles": ["jest-canvas-mock"] to my jest.config.js instead of package.json.

See: configuration

@knenkne
Copy link

knenkne commented Nov 15, 2019

For anyone googling, this can also affect react-lottie

Any solution?

@alanaugustodias
Copy link

For anyone googling, this can also affect react-lottie

having the same here...

@knenkne
Copy link

knenkne commented Nov 20, 2019

For anyone googling, this can also affect react-lottie

having the same here...

Some of canvas, canvas-prebuilt, jest-canvas-mock did the work for me. Can't tell exactly, i've been messing with jsdom-quokka-plugin also.

@alanaugustodias
Copy link

alanaugustodias commented Nov 21, 2019

For anyone googling, this can also affect react-lottie

having the same here...

Some of canvas, canvas-prebuilt, jest-canvas-mock did the work for me. Can't tell exactly, i've been messing with jsdom-quokka-plugin also.

Nice one! I've got that also from an open issue on lottie-react-web repo

@SiGnaLz314
Copy link

Hope this helps the next seeker.

I was able to get a clean pass on jest after running into this exact issue by following this:
#iov-one/ponferrada#133
More specifically (@alexlouden with jsom fourteen):
#iov-one/ponferrada#133 (comment)

Note: does appear window.* will fail.

athuyaoo added a commit to Source-Academy-Game/cadet-frontend that referenced this issue Apr 15, 2020
Tried pixijs/pixijs#4769 after encountering TypeError: Cannot set property 'fillStyle' of null
It solved the problem with the game-dev component.
athuyaoo added a commit to Source-Academy-Game/cadet-frontend that referenced this issue Apr 21, 2020
Tried pixijs/pixijs#4769 after encountering TypeError: Cannot set property 'fillStyle' of null
It solved the problem with the game-dev component.
athuyaoo added a commit to Source-Academy-Game/cadet-frontend that referenced this issue Apr 21, 2020
Tried pixijs/pixijs#4769 after encountering TypeError: Cannot set property 'fillStyle' of null
It solved the problem with the game-dev component.
wltan pushed a commit to Source-Academy-Game/cadet-frontend that referenced this issue May 2, 2020
Tried pixijs/pixijs#4769 after encountering TypeError: Cannot set property 'fillStyle' of null
It solved the problem with the game-dev component.
@stansey
Copy link

stansey commented May 4, 2020

Sorta maybe worked for me too. I added canvas and jest-canvas-mock.

canvas-prebuilt seems to be unavailable now.

jetkan-yk added a commit to source-academy/frontend that referenced this issue May 20, 2020
* Refactor constants (#2)

- Move ASSETS_HOST to within constants.js
- Add a backend directory to facilitate future communication with the backend
- Add ability to toggle asset hosts

* Setup game state fetcher

Includes implementation to manage active stories

* Refactor save and load data

* Create game testing page

* Create telescope/night filter

* Add test for Game Dev link

* Add override functions

* Retrieve data from backend

* Add more tests for game-dev page and material page

* Deleted unnecessary page

* To use test bucket

* Changed test page to automatically find folder

* Prettified and Updated Snapshots

* Change the story retrieval workflow

Dev users will first attempt to pull from the test bucket. If it fails, then try the live bucket.

Student users will always pull from the live bucket.

* Fixed location of stories

* Json Upload

* Modify test for StoryTable

* Read input JSON file and set override

* Enable the JSON file to simulate customized mission pointer and current date as well

* Fixed bug where tests on PixiJS will fail due to canvas error

Tried pixijs/pixijs#4769 after encountering TypeError: Cannot set property 'fillStyle' of null
It solved the problem with the game-dev component.

* Fully moved completed-quests and collectibles away from localStorage.

Saving and loading of quest and collectibles will come through the user data or from overriding data.

* Save Manager now saves story action sequence and start location to localStorage

* Added new actions and changed state for the addition of game state.

- Made new actiontypes to update game state in the backend and in session.
- Updated tests to accomodate for game state.

* Updated game and gamedev components to accomodate the addition of game state and changes in save manager.

* Deleted non-essential mission pointer override

* Fixed Typo 'hasColletible'.

* Merged GroundControl with gameStateUpdate

* Using SessionStorage for Game State instead of JS

* Minor Cosmetic Changes

* GamePage to load only when all information is gotten

* Fixed some errors and allow for reset json files

* Updated Game Components to use GroundControl

Use Ground Control's assessment overview instead of master.xml file. Less things to keep track of this way.

* Updated Material Table Snapshots

* Minor bugfixes and documentation

- Fixed bug with regards to missing stories, testers now use default story if they have not overriden the game.

- Potential bug fixed With no missions available,  will return leaving a black screen, instead of crashing.

- Fixed bug that stops the game from compiling when the last mission has closed, will now only show last opened mission.

- Added minor documentation

* Point hosts back to default

* Removed console.log function

* Fix bugs in backend.ts

* Prettify code

* Fix bugs in backend.ts

Co-authored-by: Asthenosphere <e0271247@u.nus.edu>
Co-authored-by: wltan <wltan@users.noreply.github.com>
Co-authored-by: travisryte <thuyaoo200@gmail.com>
Co-authored-by: jetkan-yk <jetkan.yk@gmail.com>
dvdvdmt added a commit to dvdvdmt/flying-circus that referenced this issue Jun 7, 2021
Shubhi66302 pushed a commit to Shubhi66302/mapcreator that referenced this issue Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests