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

Prevent "not implemented" error being logged in jsdom and general repository maintenance #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
resetMocks: true,
restoreMocks: true
};
20,153 changes: 17,948 additions & 2,205 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 16 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
"index.d.ts"
],
"scripts": {
"test": "cypress run",
"lint": "tsc --noEmit",
"test": "npm run test:jest && npm run test:cypress",
"test:cypress": "cypress run",
"test:jest": "jest",
"lint": "tsc",
"build": "del dist && rollup -c"
},
"author": "Vincent Thibault <[email protected]>",
Expand All @@ -32,15 +34,19 @@
"joypixel"
],
"devDependencies": {
"@babel/core": "^7.10.2",
"@babel/preset-env": "^7.10.2",
"@babel/preset-typescript": "^7.10.1",
"@rollup/plugin-typescript": "^4.1.2",
"@babel/core": "^7.18.9",
"@babel/preset-env": "^7.18.9",
"@babel/preset-typescript": "^7.18.6",
"@jest/globals": "^28.1.3",
"@rollup/plugin-typescript": "^8.3.3",
"@types/jest": "^28.1.6",
"cypress": "^5.0.0",
"del-cli": "^3.0.1",
"del-cli": "^5.0.0",
"jest": "^28.1.3",
"jest-environment-jsdom": "^28.1.3",
"prettier": "2.0.5",
"rollup": "^2.12.0",
"rollup-plugin-terser": "^6.1.0",
"typescript": "^3.9.3"
"rollup": "^2.77.0",
"rollup-plugin-terser": "^7.0.2",
"typescript": "^4.7.4"
}
}
16 changes: 16 additions & 0 deletions src/is-emoji-supported.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @jest-environment jsdom
*/
import {expect} from '@jest/globals';

it('does not call console.error in jsdom', async () => {
const consoleErrorSpy = jest.spyOn(console, 'error');

// Must use dynamic import because
// `document.createElement('canvas').getContext('2d')` is called when the
// module is imported
const {isEmojiSupported} = await import('./is-emoji-supported');

expect(consoleErrorSpy).not.toHaveBeenCalled();
expect(isEmojiSupported('😂')).toBe(false);
});
17 changes: 16 additions & 1 deletion src/is-emoji-supported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ export function setCacheHandler(store: TCache): void {
cache = store;
}

function isJsdom(): boolean {
try {
if (navigator.userAgent.includes('jsdom')) {
return true;
}
} catch {
// Running in Node so navigator is not defined
}

return false;
}

/**
* Check if the two pixels parts are perfectly the sames
*
Expand All @@ -44,7 +56,10 @@ export function setCacheHandler(store: TCache): void {
const isSupported = (() => {
let ctx = null;
try {
ctx = document.createElement('canvas').getContext('2d');
// getContext will print a "not implemented" error when running in jsdom
if (!isJsdom()) {
ctx = document.createElement('canvas').getContext('2d');
}
} catch {}

// Not in browser env
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"compilerOptions": {
"esModuleInterop": true,
"lib": ["ES2016", "DOM"]
"lib": ["ES2016", "DOM"],
"skipLibCheck": true,
"noEmit": true
}
}
Loading