emoji-picker-element/config/jest.setup.js

53 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-10-07 21:55:20 +02:00
import '@testing-library/jest-dom/jest-globals'
2023-11-11 17:12:20 +01:00
import { jest } from '@jest/globals'
2023-06-19 01:08:12 +02:00
import * as FakeIndexedDB from 'fake-indexeddb'
2020-06-05 16:50:40 +02:00
import { Crypto } from '@peculiar/webcrypto'
import { ResizeObserver } from 'd2l-resize-aware/resize-observer-module.js'
import { deleteDatabase } from '../src/database/databaseLifecycle'
import styles from '../node_modules/.cache/emoji-picker-element/styles.js'
2023-11-11 17:12:20 +01:00
import * as fetchMockJest from 'fetch-mock-jest'
2023-06-19 01:08:12 +02:00
const { IDBFactory, IDBKeyRange } = FakeIndexedDB
2023-01-22 19:05:56 +01:00
// See https://github.com/jsdom/jsdom/issues/3455#issuecomment-1333567714
2023-11-11 17:12:20 +01:00
globalThis.crypto.subtle = new Crypto().subtle
2023-01-22 19:05:56 +01:00
2023-11-11 17:12:20 +01:00
if (!globalThis.performance) {
globalThis.performance = {}
}
2023-11-11 17:12:20 +01:00
if (!globalThis.performance.mark) {
globalThis.performance.mark = () => {}
}
2023-11-11 17:12:20 +01:00
if (!globalThis.performance.measure) {
globalThis.performance.measure = () => {}
}
jest.setTimeout(60000)
2020-06-04 07:11:26 +02:00
2023-11-11 17:12:20 +01:00
globalThis.ResizeObserver = ResizeObserver
process.env.NODE_ENV = 'test'
process.env.STYLES = styles
2023-11-11 17:12:20 +01:00
globalThis.IDBKeyRange = IDBKeyRange
globalThis.indexedDB = new IDBFactory()
2023-11-11 18:06:31 +01:00
// Hack to work around an issue with jest-environment-jsdom https://github.com/jsdom/jsdom/issues/3363
globalThis.structuredClone = globalThis.structuredClone ?? (_ => JSON.parse(JSON.stringify(_)))
beforeAll(() => {
2023-11-11 17:12:20 +01:00
jest.spyOn(globalThis.console, 'log').mockImplementation()
jest.spyOn(globalThis.console, 'warn').mockImplementation()
const fetch = fetchMockJest.default.sandbox()
globalThis.fetch = fetch
globalThis.Response = fetch.Response
})
afterEach(async () => {
// fresh indexedDB for every test
2023-11-11 17:12:20 +01:00
const dbs = await globalThis.indexedDB.databases()
await Promise.all(dbs.map(({ name }) => deleteDatabase(name)))
})