Skip to content

Commit a1ccab3

Browse files
committed
test(jest): add Jest to CI matrix
1 parent 3fad5e9 commit a1ccab3

File tree

8 files changed

+63
-5
lines changed

8 files changed

+63
-5
lines changed

.github/workflows/release.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
matrix:
2626
node: ['16', '18', '20']
2727
svelte: ['3', '4']
28-
test-runner: ['vitest:jsdom', 'vitest:happy-dom']
28+
test-runner: ['vitest:jsdom', 'vitest:happy-dom', 'jest']
2929
experimental: [false]
3030
include:
3131
- node: '20'
@@ -36,6 +36,10 @@ jobs:
3636
svelte: 'next'
3737
test-runner: 'vitest:happy-dom'
3838
experimental: true
39+
- node: '20'
40+
svelte: 'next'
41+
test-runner: 'jest'
42+
experimental: true
3943

4044
steps:
4145
- name: ⬇️ Checkout repo

jest.config.cjs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
testMatch: ['<rootDir>/src/__tests__/**/*.test.js'],
3+
transform: {
4+
'^.+\\.svelte$': 'svelte-jester',
5+
},
6+
moduleFileExtensions: ['js', 'svelte'],
7+
extensionsToTreatAsEsm: ['.svelte'],
8+
testEnvironment: 'jsdom',
9+
setupFilesAfterEnv: ['<rootDir>/src/__tests__/_jest-setup.js'],
10+
injectGlobals: false,
11+
moduleNameMapper: {
12+
'^vitest$': '<rootDir>/src/__tests__/_jest-vitest-alias.js',
13+
},
14+
resetMocks: true,
15+
restoreMocks: true,
16+
}

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"test:watch": "vitest",
7272
"test:vitest:jsdom": "vitest run --coverage --environment jsdom",
7373
"test:vitest:happy-dom": "vitest run --coverage --environment happy-dom",
74+
"test:jest": "npx --node-options=\"--experimental-vm-modules --no-warnings\" jest --coverage",
7475
"types": "svelte-check",
7576
"validate": "npm-run-all test:vitest:* types",
7677
"contributors:add": "all-contributors add",
@@ -93,6 +94,7 @@
9394
"@testing-library/dom": "^10.0.0"
9495
},
9596
"devDependencies": {
97+
"@jest/globals": "^29.7.0",
9698
"@sveltejs/vite-plugin-svelte": "^3.0.2",
9799
"@testing-library/jest-dom": "^6.3.0",
98100
"@testing-library/user-event": "^14.5.2",
@@ -113,6 +115,8 @@
113115
"eslint-plugin-vitest-globals": "1.4.0",
114116
"expect-type": "^0.19.0",
115117
"happy-dom": "^14.7.1",
118+
"jest": "^29.7.0",
119+
"jest-environment-jsdom": "^29.7.0",
116120
"jsdom": "^24.0.0",
117121
"npm-run-all": "^4.1.5",
118122
"prettier": "3.2.4",

src/__tests__/_jest-setup.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import '@testing-library/jest-dom/jest-globals'
2+
3+
import { afterEach } from '@jest/globals'
4+
import { act, cleanup } from '@testing-library/svelte'
5+
6+
afterEach(async () => {
7+
await act()
8+
cleanup()
9+
})

src/__tests__/_jest-vitest-alias.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, jest, test } from '@jest/globals'
2+
3+
export {
4+
afterAll,
5+
afterEach,
6+
beforeAll,
7+
beforeEach,
8+
describe,
9+
expect,
10+
test,
11+
jest as vi,
12+
} from '@jest/globals'
13+
14+
// Add support for describe.skipIf and test.skipIf
15+
describe.skipIf = (condition) => (condition ? describe.skip : describe)
16+
test.skipIf = (condition) => (condition ? test.skip : test)
17+
18+
// Add support for `stubGlobal`
19+
jest.stubGlobal = (property, stub) => {
20+
if (typeof stub === 'function') {
21+
jest.spyOn(globalThis, property).mockImplementation(stub)
22+
} else {
23+
jest.replaceProperty(globalThis, property, stub)
24+
}
25+
}

src/__tests__/cleanup.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('cleanup', () => {
1919
renderSubject()
2020
cleanup()
2121

22-
expect(onDestroyed).toHaveBeenCalledOnce()
22+
expect(onDestroyed).toHaveBeenCalledTimes(1)
2323
})
2424

2525
test('cleanup handles unexpected errors during mount', () => {

src/__tests__/mount.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('mount and destroy', () => {
1515

1616
expect(content).toBeInTheDocument()
1717
await act()
18-
expect(onMounted).toHaveBeenCalledOnce()
18+
expect(onMounted).toHaveBeenCalledTimes(1)
1919
})
2020

2121
test('component is destroyed', async () => {
@@ -28,6 +28,6 @@ describe('mount and destroy', () => {
2828

2929
expect(content).not.toBeInTheDocument()
3030
await act()
31-
expect(onDestroyed).toHaveBeenCalledOnce()
31+
expect(onDestroyed).toHaveBeenCalledTimes(1)
3232
})
3333
})

src/__tests__/rerender.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('rerender', () => {
2323
await rerender({ props: { name: 'Dolly' } })
2424

2525
expect(element).toHaveTextContent('Hello Dolly!')
26-
expect(console.warn).toHaveBeenCalledOnce()
26+
expect(console.warn).toHaveBeenCalledTimes(1)
2727
expect(console.warn).toHaveBeenCalledWith(
2828
expect.stringMatching(/deprecated/iu)
2929
)

0 commit comments

Comments
 (0)