-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mocha + sinon + chai. ESM + typescript is hard :-/ Editor TS, build TS and spec TS don't want to agree on module imports. On top of that — Jakefile can not be ESM yet. So src/ and /spec are modules, and project is not. --watch won't work unless ran with --parallel mochajs/mocha#4374 mochajs/mocha-examples#47 https://gist.github.com/jordansexton/2a0c3c360aa700cc9528e89620e82c3d
- Loading branch information
1 parent
91876de
commit df026cf
Showing
12 changed files
with
1,175 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,5 @@ pkg/ | |
pkg-src/ | ||
|
||
# generated files | ||
src/config.js | ||
src/config.ts | ||
manifest.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extension": ["ts"], | ||
"spec": ["spec/**/*.spec.ts"], | ||
"require": ["jsdom-global/register", "spec/setup.ts"], | ||
"node-option": [ | ||
"no-warnings", | ||
"experimental-specifier-resolution=node", | ||
"loader=ts-node/esm" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { SinonStub } from 'sinon' | ||
import { expect } from 'chai' | ||
|
||
import { isLocal, getDomain, storage } from '../src/helpers.js' | ||
|
||
const pickStub = <K extends keyof O, O>(key: K, obj: O) => obj[key] as SinonStub | ||
|
||
describe('helpers.ts', () => { | ||
it('creates 64x64 canvas and 2d context', () => { | ||
const { | ||
OffscreenCanvas, | ||
OffscreenCanvas: { | ||
prototype: { getContext } | ||
} | ||
} = global | ||
|
||
expect(OffscreenCanvas).to.be.calledOnceWith(64, 64) | ||
expect(getContext).to.be.calledOnceWith('2d', { | ||
willReadFrequently: true | ||
}) | ||
}) | ||
|
||
describe('isLocal', () => { | ||
// prettier-ignore | ||
const cases = [ | ||
[true, 'localhost'], | ||
[true, '0.0.0.0'], | ||
[false, '0.0.0.1'], | ||
|
||
[true, '127.0.0.0'], | ||
[true, '127.0.0.1'], | ||
[true, '127.1.1.1'], | ||
|
||
[true, '10.0.0.1'], | ||
[true, '10.1.1.1'], | ||
[true, '10.0.0.0'], | ||
|
||
[false, '172.15.0.0'], | ||
[true, '172.16.0.0'], | ||
[true, '172.16.100.0'], | ||
[true, '172.31.0.0'], | ||
[true, '172.31.0.0'], | ||
[false, '172.32.0.0'], | ||
|
||
[true, '192.168.0.0'], | ||
[false, '192.169.0.0'], | ||
[false, '191.168.0.0'], | ||
|
||
[false, '0.0'], | ||
[false, undefined], | ||
[false, '123'], | ||
[false, '127.0.0.0.boop.com'], | ||
[false, '10.o.0.0.com'], | ||
[false, '8.8.8.8'], | ||
[false, 'geo.furman.im'], | ||
[false, 'battlestation'], | ||
] as const | ||
|
||
cases.forEach(([expected, ip]) => { | ||
it(`${expected ? 'local' : 'global'}\t${ip}`, () => { | ||
expect(isLocal(ip)).to.equal(expected) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('getDomain', () => { | ||
it('returns domain for http, https and ftp schemas', () => { | ||
expect(getDomain('http://boop.doop')).to.eq('boop.doop') | ||
expect( | ||
getDomain('https://127.0.0.0.boop.com/welp?some=come&utm=sucks') | ||
).to.eq('127.0.0.0.boop.com') | ||
expect(getDomain('ftp://scene')).to.eq('scene') | ||
}) | ||
|
||
it('returns undefined for everything else', () => { | ||
expect(getDomain('')).to.be.undefined | ||
expect(getDomain(undefined)).to.be.undefined | ||
expect(getDomain('gopher://old')).to.be.undefined | ||
expect(getDomain('chrome://new-tab')).to.be.undefined | ||
expect(getDomain('magnet://h.a.s.h')).to.be.undefined | ||
}) | ||
}) | ||
|
||
describe('storage', () => { | ||
const setStub = pickStub('set', global.chrome.storage.local), | ||
getStub = pickStub('get', global.chrome.storage.local), | ||
clearStub = pickStub('clear', global.chrome.storage.local) | ||
|
||
it('#set', () => { | ||
storage.set('boop', { woop: 'shmloop' }) | ||
|
||
expect(setStub).to.be.calledOnceWith({ | ||
boop: { woop: 'shmloop' } | ||
}) | ||
}) | ||
|
||
it('#get', async () => { | ||
getStub.resolves({ | ||
'a key': 'valooe', | ||
'another key': 'another valooe' | ||
}) | ||
|
||
expect(await storage.get('but a key')).to.be.undefined | ||
expect(await storage.get('a key')).to.eq('valooe') | ||
expect(await storage.get('another key')).to.eq('another valooe') | ||
}) | ||
|
||
it('clears itself when full', async () => { | ||
setStub.throws('anything') | ||
|
||
await storage.set('smol', 'but fatal') | ||
|
||
expect(clearStub).to.be.calledOnce | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sinon from 'sinon' | ||
import chai from 'chai' | ||
import sinonChai from 'sinon-chai' | ||
|
||
/* Matchers */ | ||
|
||
chai.use(sinonChai) | ||
|
||
/* OffscreenCanvas */ | ||
|
||
const canvasSandbox = sinon.createSandbox({ | ||
properties: ['spy'] | ||
}) | ||
|
||
class OffscreenCanvasMock { | ||
constructor() {} | ||
getContext() {} | ||
} | ||
|
||
canvasSandbox.spy(OffscreenCanvasMock.prototype, 'getContext') | ||
|
||
/* chrome.storage.local */ | ||
|
||
const storageSandbox = sinon.createSandbox({ | ||
properties: ['stub'] | ||
}) | ||
|
||
const local = { | ||
set: chromeSandbox.stub(), | ||
get: chromeSandbox.stub(), | ||
clear: chromeSandbox.stub() | ||
} | ||
|
||
/* Assign to window */ | ||
|
||
Object.assign(global, { | ||
OffscreenCanvas: canvasSandbox.spy(OffscreenCanvasMock), | ||
chrome: { | ||
storage: { local }, | ||
action | ||
} | ||
}) | ||
|
||
/* Reset call counts hook */ | ||
|
||
export const mochaHooks = { | ||
afterEach() { | ||
canvasSandbox.reset() | ||
chromeSandbox.resetHistory() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
|
||
"include": ["spec/**/*"], | ||
|
||
"compilerOptions": { | ||
"noEmit": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
|
||
"include": ["src/**/*"], | ||
|
||
"compilerOptions": { | ||
"outDir": "build/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
{ | ||
"include": ["src/**/*"], | ||
|
||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "build", | ||
|
||
"noErrorTruncation": true, | ||
"noErrorTruncation": true, // no (...) in errors | ||
|
||
/* typechecking */ | ||
"strict": true, | ||
"noUncheckedIndexedAccess": true, | ||
"noImplicitReturns": true, | ||
// do not skipLibCheck, we want our d.ts validated | ||
|
||
// modules | ||
/* modules */ | ||
"moduleResolution": "nodenext", // this makes other peoples d.ts pass | ||
"esModuleInterop": true, | ||
"module": "es2022", | ||
"target": "es2021", | ||
"esModuleInterop": true | ||
"target": "es2021" | ||
} | ||
} |
Oops, something went wrong.