Skip to content

Commit 3f4afaf

Browse files
build: migrate to vite and vitest
1 parent fb59d7f commit 3f4afaf

19 files changed

Lines changed: 955 additions & 598 deletions

.github/workflows/coverage-diff.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ jobs:
1010
checks: write
1111
pull-requests: write
1212
uses: fingerprintjs/dx-team-toolkit/.github/workflows/coverage-diff.yml@v1
13+
with:
14+
testScript: pnpm test:coverage:diff

__tests__/detect-env.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { detectEnvironment } from '../src/detect-env'
22
import { Env } from '../src/env.types'
3+
import { describe, it, expect } from 'vitest'
34

45
describe('Detect user env', () => {
56
describe('Preact', () => {

__tests__/fpjs-provider.test.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ import { renderHook } from '@testing-library/react'
33
import { FpContext } from '../src'
44
import { createWrapper, getDefaultLoadOptions } from './helpers'
55
import { version } from '../package.json'
6+
import { describe, it, expect, vi } from 'vitest'
7+
import * as agent from '@fingerprint/agent'
68

7-
jest.mock('@fingerprint/agent', () => {
8-
return {
9-
...jest.requireActual<any>('@fingerprint/agent'),
10-
start: jest.fn(),
11-
}
12-
})
9+
vi.mock('@fingerprint/agent', { spy: true })
1310

14-
const mockStart = jest.requireMock('@fingerprint/agent').start as jest.Mock
11+
const mockStart = vi.mocked(agent.start)
1512

1613
describe('FpProvider', () => {
1714
it('should configure an instance of the Fp Agent', async () => {

__tests__/helpers.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PropsWithChildren } from 'react'
22
import { FpProvider, FpProviderOptions } from '../src'
3-
import { act } from 'react-dom/test-utils'
3+
import { act } from '@testing-library/react'
44

55
export const getDefaultLoadOptions = () => ({
66
apiKey: 'test_api_key',

__tests__/use-visitor-data.test.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { useVisitorData, UseVisitorDataReturn } from '../src'
2-
import { render, renderHook } from '@testing-library/react'
2+
import { act, render, renderHook } from '@testing-library/react'
33
import { actWait, createWrapper, wait } from './helpers'
4-
import { act } from 'react-dom/test-utils'
54
import { useEffect, useState } from 'react'
65
import userEvent from '@testing-library/user-event'
6+
import * as agent from '@fingerprint/agent'
77
import { GetResult } from '@fingerprint/agent'
8+
import { beforeEach, describe, expect, it, vi } from 'vitest'
89

910
const mockGetResult = {
1011
visitor_id: 'kOzFgO0kw2Eivvb14mRL',
@@ -14,23 +15,19 @@ const mockGetResult = {
1415
suspect_score: 0.5,
1516
} satisfies GetResult
1617

17-
const mockGet = jest.fn()
18+
const mockGet = vi.fn()
1819
const mockAgent = {
1920
get: mockGet,
21+
collect: vi.fn(),
2022
}
2123

22-
const mockStart = jest.requireMock('@fingerprint/agent').start as jest.Mock
24+
vi.mock('@fingerprint/agent', { spy: true })
2325

24-
jest.mock('@fingerprint/agent', () => {
25-
return {
26-
...jest.requireActual('@fingerprint/agent'),
27-
start: jest.fn(),
28-
}
29-
})
26+
const mockStart = vi.mocked(agent.start)
3027

3128
describe('useVisitorData', () => {
3229
beforeEach(() => {
33-
jest.resetAllMocks()
30+
vi.resetAllMocks()
3431

3532
mockStart.mockReturnValue(mockAgent)
3633
})

__tests__/with-environment.preact.test.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { render as preactRender } from '@testing-library/preact'
22
import { h } from 'preact'
3+
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
4+
import * as compat from 'preact/compat'
35

46
describe('WithEnvironment', () => {
57
describe('when running within Preact', () => {
68
beforeEach(() => {
7-
jest.doMock('react-dom', () => require('preact/compat'))
8-
jest.doMock('react', () => require('preact/compat'))
9+
vi.doMock('react', () => compat)
10+
vi.doMock('react-dom', () => compat)
911
})
1012

1113
afterEach(() => {
12-
jest.resetModules()
14+
vi.resetAllMocks()
1315
})
14-
15-
it('should detect env as "preact"', () => {
16-
const { WithEnvironment } = require('../src/components/with-environment')
16+
it('should detect env as "preact"', async () => {
17+
const { WithEnvironment } = await import('../src/components/with-environment')
1718
const PrintEnv = (props: any) => h('div', null, props?.env?.name)
1819

20+
// @ts-ignore
1921
const { container } = preactRender(h(WithEnvironment, null, h(PrintEnv, null)))
2022

2123
expect(container.innerHTML).toContain('preact')

__tests__/with-environment.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { WithEnvironment } from '../src/components/with-environment'
44
import { Link, MemoryRouter, Route, Routes } from 'react-router-dom'
55
import userEvent from '@testing-library/user-event'
66
import { actWait } from './helpers'
7+
import { describe, it, expect, vi } from 'vitest'
78

89
describe('WithEnvironment', () => {
910
it('enhances provided element with `env` prop', () => {
10-
const Mock = jest.fn(() => <div>foo</div>) as FunctionComponent
11+
const Mock = vi.fn(() => <div>foo</div>) as FunctionComponent
1112

1213
render(
1314
<WithEnvironment>

contributing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pnpm lint:fix
4444

4545
### How to test
4646

47-
Tests are located in `__tests__` folder and run by [jest](https://jestjs.io/) in [jsdom](https://github.com/jsdom/jsdom) environment.
47+
Tests are located in `__tests__` folder and run by [vitest](https://vitest.dev/) in [jsdom](https://github.com/jsdom/jsdom) environment.
4848

4949
To run tests you can use IDE instruments or just run:
5050

jest.config.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

package.json

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
"name": "@fingerprintjs/fingerprintjs-pro-react",
33
"version": "2.7.1",
44
"description": "FingerprintJS Pro React SDK",
5-
"main": "dist/fp-pro-react.cjs.js",
6-
"module": "dist/fp-pro-react.esm.js",
5+
"main": "dist/fp-pro-react.js",
6+
"module": "dist/fp-pro-react.mjs",
77
"types": "dist/fp-pro-react.d.ts",
88
"exports": {
99
".": {
1010
"types": "./dist/fp-pro-react.d.ts",
11-
"import": "./dist/fp-pro-react.esm.js",
12-
"require": "./dist/fp-pro-react.cjs.js",
13-
"node": "./dist/fp-pro-react.esm.js"
11+
"import": "./dist/fp-pro-react.mjs",
12+
"require": "./dist/fp-pro-react.js"
1413
}
1514
},
1615
"keywords": [
@@ -28,13 +27,14 @@
2827
"scripts": {
2928
"postinstall": "node scripts/detect-env.js",
3029
"prepare": "husky install",
31-
"watch": "rollup -c rollup.config.js -w --bundleConfigAsCjs",
32-
"build": "rimraf dist && rollup -c rollup.config.js --bundleConfigAsCjs",
30+
"watch": "vite build --watch",
31+
"build": "vite build",
3332
"start:spa": "pnpm start --prefix=examples/spa",
3433
"lint": "eslint --ext .js,.ts,.tsx --ignore-path .gitignore --max-warnings 0 .",
3534
"lint:fix": "pnpm lint --fix",
36-
"test": "jest",
37-
"test:coverage": "jest --coverage",
35+
"test": "vitest",
36+
"test:coverage": "vitest run --coverage",
37+
"test:coverage:diff": "vitest run --coverage --reporter json --outputFile.json=report.json",
3838
"test:dts": "tsc --noEmit --isolatedModules dist/fp-pro-react.d.ts",
3939
"docs": "typedoc src/index.ts --out docs"
4040
},
@@ -63,15 +63,13 @@
6363
"@fingerprintjs/eslint-config-dx-team": "^0.1.0",
6464
"@fingerprintjs/prettier-config-dx-team": "^0.2.0",
6565
"@fingerprintjs/tsconfig-dx-team": "^0.0.2",
66-
"@rollup/plugin-json": "^6.1.0",
67-
"@rollup/plugin-typescript": "^11.1.6",
68-
"@testing-library/preact": "^3.2.3",
69-
"@testing-library/react": "^14.2.1",
70-
"@testing-library/user-event": "^14.5.2",
71-
"@types/jest": "^29.5.12",
66+
"@testing-library/preact": "^3.2.4",
67+
"@testing-library/react": "^16.3.2",
68+
"@testing-library/user-event": "^14.6.1",
7269
"@types/node": "^20.11.28",
7370
"@types/react": "^18.2.66",
7471
"@types/react-dom": "^18.2.22",
72+
"@vitest/coverage-istanbul": "^4.0.18",
7573
"commitizen": "^4.3.0",
7674
"cz-conventional-changelog": "^3.3.0",
7775
"eslint": "8.57.0",
@@ -80,23 +78,20 @@
8078
"eslint-plugin-react": "7.34.0",
8179
"eslint-plugin-react-hooks": "^4.6.0",
8280
"husky": "^9.0.11",
83-
"jest": "^27.5.1",
84-
"jest-environment-jsdom": "^27.5.1",
8581
"lint-staged": "^15.2.2",
8682
"preact": "^10.19.6",
8783
"react": "^18.2.0",
8884
"react-dom": "^18.2.0",
8985
"react-router-dom": "^6.22.3",
9086
"rimraf": "^5.0.5",
91-
"rollup": "^4.43.0",
9287
"rollup-plugin-banner2": "^1.3.1",
93-
"rollup-plugin-dts": "^6.2.1",
94-
"rollup-plugin-license": "^3.6.0",
95-
"rollup-plugin-peer-deps-external": "^2.2.4",
96-
"ts-jest": "^29.1.2",
9788
"tslib": "^2.6.2",
9889
"typedoc": "^0.25.12",
99-
"typescript": "^5.4.2"
90+
"typescript": "^5.4.2",
91+
"vite": "^7.3.0",
92+
"vite-plugin-banner": "^0.8.1",
93+
"vite-plugin-dts": "^4.5.4",
94+
"vitest": "^4.0.18"
10095
},
10196
"lint-staged": {
10297
"*.ts": "pnpm lint:fix",

0 commit comments

Comments
 (0)