js: fix timers mocking in QRCode tests (#87365)
gitea/passerelle/pipeline/head There was a failure building this commit Details

This commit is contained in:
Corentin Sechet 2024-02-23 09:46:24 +01:00 committed by Corentin Sechet
parent 668ddf08e5
commit 3f584e13a7
3 changed files with 9 additions and 7 deletions

View File

@ -1,5 +1,5 @@
import 'qrcode/qrcode-reader.js'
import { expect, test, vi} from 'vitest'
import { expect, test, vi } from 'vitest'
import ZXingBrowser from 'qrcode/zxing-browser.min.js'
import nacl from 'qrcode/nacl.min.js'
@ -26,7 +26,6 @@ const certificateWithoutValidity =
'B3X3PP0TH9CN2R3IYEDB$DKWEOED0%EIEC ED3.DQ34%R83:6NW6XM8ME1.$E8UCE44QIC+96M.C9%6I' +
'M6E467W5LA76L6%47G%64W5ZJCYX6J*64EC6:6J$6'
const qrcodeReaderTest = test.extend({
mock: async ({ task }, use) => {
let resolveResult = undefined
@ -66,6 +65,7 @@ const qrcodeReaderTest = test.extend({
const fetchBackup = global.fetch
global.fetch = vi.fn()
vi.useFakeTimers({toFake: ['Date']})
await use({
reader,
scan: async (text) => {
@ -74,14 +74,12 @@ const qrcodeReaderTest = test.extend({
await resultHandled
}
})
vi.useFakeTimers()
vi.useRealTimers()
global.fetch = fetchBackup
reader.remove()
vi.useRealTimers ()
resolveResult(terminate)
window.ZXingBrowser = savedZXingBrowser
navigator.mediaDevices = undefined
@ -112,7 +110,7 @@ qrcodeReaderTest('qrcode reader shows valid qrcode informations', async ({mock})
const validity = popup.querySelector('.qrcode-reader--validity')
expect(validity.innerText).toMatch(/from :\s*31\/10\/2023 23:00:00\s*to :\s*01\/12\/2023 22:59:59/)
expect(validity.innerText).toMatch(/from :\s*10\/31\/2023, 11:00:00 PM\s*to :\s*12\/1\/2023, 10:59:59 PM/)
const labels = popup.querySelectorAll('.qrcode-reader--data-item-label')

View File

@ -96,4 +96,5 @@ allowlist_externals =
install_command = bash setup-vitest.sh {packages}
setenv =
NODE_PATH={envdir}/lib/node_modules
LC_ALL=en_US.UTF-8
commands = npx vitest --run

View File

@ -9,7 +9,10 @@ export default defineConfig({
qrcode: fileURLToPath(new URL('./passerelle/apps/qrcode/static/qrcode/js', import.meta.url)),
vitest: process.env.NODE_PATH + '/vitest'
},
environment: 'happy-dom'
environment: 'happy-dom',
chaiConfig: {
truncateThreshold: Infinity,
},
}
})