From ee1b1f791792bfc2a1598867a076bc516c180524 Mon Sep 17 00:00:00 2001 From: Michael Cousins Date: Sun, 28 Jan 2024 07:11:41 +1100 Subject: [PATCH] feat(vitest): add vitest cleanup export (#294) feat(vitest): add vitest cleanup import --- package.json | 13 ++++++++----- src/__tests__/_vitest-setup.js | 2 ++ src/test-setup.js | 11 ----------- src/vitest.js | 8 ++++++++ vite.config.js | 12 ++++++++---- 5 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 src/__tests__/_vitest-setup.js delete mode 100644 src/test-setup.js create mode 100644 src/vitest.js diff --git a/package.json b/package.json index b842ef7..afe5e54 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,9 @@ ".": { "types": "./types/index.d.ts", "default": "./src/index.js" + }, + "./vitest": { + "default": "./src/vitest.js" } }, "type": "module", @@ -43,9 +46,9 @@ "toc": "doctoc README.md", "lint": "(prettier . --check || true) && eslint .", "format": "prettier . --write && eslint . --fix", - "test": "vitest run src", - "test:watch": "npm run test -- --watch", - "test:update": "npm run test -- --updateSnapshot --coverage", + "test": "vitest run --coverage", + "test:watch": "vitest", + "test:update": "vitest run --update", "setup": "npm install && npm run validate", "validate": "npm-run-all lint test", "contributors:add": "all-contributors add", @@ -61,10 +64,10 @@ "@commitlint/cli": "^17.6.6", "@commitlint/config-conventional": "^17.6.6", "@sveltejs/vite-plugin-svelte": "^2.4.2", - "@testing-library/jest-dom": "^5.16.5", + "@testing-library/jest-dom": "^6.3.0", "@typescript-eslint/eslint-plugin": "^6.19.1", "@typescript-eslint/parser": "^6.19.1", - "@vitest/coverage-c8": "^0.33.0", + "@vitest/coverage-v8": "^0.33.0", "all-contributors-cli": "^6.26.0", "doctoc": "^2.2.1", "eslint": "^8.43.0", diff --git a/src/__tests__/_vitest-setup.js b/src/__tests__/_vitest-setup.js new file mode 100644 index 0000000..13ed819 --- /dev/null +++ b/src/__tests__/_vitest-setup.js @@ -0,0 +1,2 @@ +import '@testing-library/jest-dom/vitest' +import '../vitest' diff --git a/src/test-setup.js b/src/test-setup.js deleted file mode 100644 index fe15b57..0000000 --- a/src/test-setup.js +++ /dev/null @@ -1,11 +0,0 @@ -import * as matchers from '@testing-library/jest-dom/dist/matchers' -import { afterEach, expect } from 'vitest' - -import { act, cleanup } from './pure.js' - -expect.extend(matchers) - -afterEach(async () => { - await act() - cleanup() -}) diff --git a/src/vitest.js b/src/vitest.js new file mode 100644 index 0000000..135ddbe --- /dev/null +++ b/src/vitest.js @@ -0,0 +1,8 @@ +import { afterEach } from 'vitest' + +import { act, cleanup } from './pure.js' + +afterEach(async () => { + await act() + cleanup() +}) diff --git a/vite.config.js b/vite.config.js index a32b963..f261029 100644 --- a/vite.config.js +++ b/vite.config.js @@ -3,9 +3,13 @@ import { defineConfig } from 'vite' // https://vitejs.dev/config/ export default defineConfig({ - plugins: [svelte()], - test: { - environment: 'jsdom', - setupFiles: ['./src/test-setup.js'], + plugins: [svelte()], + test: { + environment: 'jsdom', + setupFiles: ['./src/__tests__/_vitest-setup.js'], + coverage: { + provider: 'v8', + include: ['src'], }, + }, })