js: configure vitest + happy-dom test environment (#81105) #666

Open
csechet wants to merge 1 commits from wip/81105-tests-unitaires-js into main
5 changed files with 56 additions and 1 deletions

6
setup-vitest.sh Normal file
View File

@ -0,0 +1,6 @@
#!/bin/bash
pip install $*
nodeenv --prebuilt --python-virtualenv
source $VIRTUAL_ENV/bin/activate # source again to activate npm from env
npm install -g vite vitest@0.30.0 happy-dom

18
tests/js/dummy.test.js Normal file
View File

@ -0,0 +1,18 @@
import { expect, test, vi } from 'vitest'
import { initLiveUpdate } from 'wcs/dummy.js'
test('for the record', async () => {
document.body.innerHTML = `
<form>
<input />
</div>`
initLiveUpdate()
const handlerMock = vi.fn()
const input = document.querySelector('input')
const form = document.querySelector('form').addEventListener('wcs:change', handlerMock)
input.dispatchEvent(new Event('change'))
expect(handlerMock).toHaveBeenCalled()
})

12
tox.ini
View File

@ -1,6 +1,6 @@
[tox]
toxworkdir = {env:TMPDIR:/tmp}/tox-{env:USER}/wcs/{env:BRANCH_NAME:main}/{env:EXECUTOR_NUMBER:}
envlist = py3-django32-codestyle-coverage, pylint
envlist = py3-django32-codestyle-coverage, pylint, vitest
[testenv]
setenv =
@ -80,3 +80,13 @@ commands =
[pytest]
filterwarnings =
error::_pytest.warning_types.PytestCollectionWarning
[testenv:vitest]
deps = nodeenv
allowlist_externals =
bash
npx
install_command = bash setup-vitest.sh {packages}
setenv =
NODE_PATH={envdir}/lib/node_modules
commands = npx vitest --run

15
vitest.config.js Normal file
View File

@ -0,0 +1,15 @@
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
include: 'tests/js/**/*.test.js',
watchExclude: ['**'],
alias: {
wcs: fileURLToPath(new URL('./wcs/qommon/static/js', import.meta.url)),
vitest: process.env.NODE_PATH + '/vitest'
},
environment: 'happy-dom'
}
})

View File

@ -0,0 +1,6 @@
export function initLiveUpdate() {
document.querySelector('input').addEventListener('change', (event) => {
const wcsChangeEvent = new Event('wcs:change')
event.target.closest('form').dispatchEvent(wcsChangeEvent);
})
}