Skip to content

Commit cb66333

Browse files
authored
test(jest): add Jest to CI matrix (#372)
1 parent ac3248d commit cb66333

File tree

8 files changed

+71
-6
lines changed

8 files changed

+71
-6
lines changed

.github/workflows/release.yml

Lines changed: 6 additions & 2 deletions
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
@@ -55,7 +59,7 @@ jobs:
5559
run: npm run test:${{ matrix.test-runner }}
5660

5761
- name: ▶️ Run type-checks
58-
if: ${{ matrix.node == '20' && matrix.svelte == '4' }}
62+
if: ${{ matrix.node == '20' && matrix.svelte == '4' && matrix.test-runner == 'vitest:jsdom' }}
5963
run: npm run types
6064

6165
- name: ⬆️ Upload coverage report

jest.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
2+
3+
const IS_SVELTE_5 = SVELTE_VERSION >= '5'
4+
5+
export default {
6+
testMatch: ['<rootDir>/src/__tests__/**/*.test.js'],
7+
transform: {
8+
'^.+\\.svelte$': 'svelte-jester',
9+
},
10+
moduleFileExtensions: ['js', 'svelte'],
11+
extensionsToTreatAsEsm: ['.svelte'],
12+
testEnvironment: 'jsdom',
13+
setupFilesAfterEnv: ['<rootDir>/src/__tests__/_jest-setup.js'],
14+
injectGlobals: false,
15+
moduleNameMapper: {
16+
'^vitest$': '<rootDir>/src/__tests__/_jest-vitest-alias.js',
17+
'^@testing-library/svelte$': IS_SVELTE_5
18+
? '<rootDir>/src/svelte5-index.js'
19+
: '<rootDir>/src/index.js',
20+
},
21+
resetMocks: true,
22+
restoreMocks: true,
23+
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"test:watch": "vitest",
6868
"test:vitest:jsdom": "vitest run --coverage --environment jsdom",
6969
"test:vitest:happy-dom": "vitest run --coverage --environment happy-dom",
70+
"test:jest": "npx --node-options=\"--experimental-vm-modules --no-warnings\" jest --coverage",
7071
"types": "svelte-check",
7172
"validate": "npm-run-all test:vitest:* types",
7273
"contributors:add": "all-contributors add",
@@ -89,6 +90,7 @@
8990
"@testing-library/dom": "^10.0.0"
9091
},
9192
"devDependencies": {
93+
"@jest/globals": "^29.7.0",
9294
"@sveltejs/vite-plugin-svelte": "^3.0.2",
9395
"@testing-library/jest-dom": "^6.3.0",
9496
"@testing-library/user-event": "^14.5.2",
@@ -109,6 +111,8 @@
109111
"eslint-plugin-vitest-globals": "1.5.0",
110112
"expect-type": "^0.19.0",
111113
"happy-dom": "^14.7.1",
114+
"jest": "^29.7.0",
115+
"jest-environment-jsdom": "^29.7.0",
112116
"jsdom": "^24.0.0",
113117
"npm-run-all": "^4.1.5",
114118
"prettier": "3.2.5",

src/__tests__/_jest-setup.js

Lines changed: 9 additions & 0 deletions
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

Lines changed: 25 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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)