Skip to content

Commit 738083a

Browse files
committed
refactor: move mockWarn into test-utils
1 parent 30eb2f4 commit 738083a

File tree

6 files changed

+101
-6
lines changed

6 files changed

+101
-6
lines changed

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ module.exports = {
3232
['/node_modules/', '/examples/__tests__']
3333
: ['/node_modules/'],
3434
setupFiles: ['<rootDir>/jest.setup.ts'],
35+
setupFilesAfterEnv: ['<rootDir>/jest.setup.afterEnv.ts'],
3536
silent: true,
3637
}

jest.setup.afterEnv.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { mockWarn } from '@nativescript-vue/test-utils'
2+
3+
// set up warn mocks and assertion helpers
4+
mockWarn()

jest.setup.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// @vue/shared provides a mockWarn function that adds the following matchers
2-
// we have to declare them here, as otherwise jest fails to compile
3-
// could be a problem with the @vue/shared package not including this
4-
// in the generated .d.ts rollup.
51
declare global {
62
namespace jest {
73
interface Matchers<R, T> {

packages/runtime/__tests__/components/ActionBar.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ registerTestElement('Page', () => TestPage)
2121

2222
import { h, ActionBar, render, INSVElement } from '@nativescript-vue/runtime'
2323
import { NSVElement } from '../../src/nodes'
24-
import { mockWarn } from '@vue/shared'
2524

2625
describe('ActionBar', () => {
27-
mockWarn()
2826
let root: NSVElement
2927
beforeEach(() => {
3028
root = new NSVElement('Frame')

packages/test-utils/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './mocks/core/ViewBase.mock'
22
export * from './mocks/core/Frame.mock'
33
export * from './mocks/registry.mock'
44
export * from './platform'
5+
export * from './mockWarn'

packages/test-utils/src/mockWarn.ts

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// code taken from since it's been removed from `@vue/shared`
2+
// https://github.com/vuejs/vue-next/blob/bd96ec488ef94f03b3fac722710d0d53cbde016d/scripts/setupJestEnv.ts
3+
export function mockWarn() {
4+
expect.extend({
5+
toHaveBeenWarned(received: string) {
6+
asserted.add(received)
7+
const passed = warn.mock.calls.some(
8+
(args) => args[0].indexOf(received) > -1
9+
)
10+
if (passed) {
11+
return {
12+
pass: true,
13+
message: () => `expected "${received}" not to have been warned.`,
14+
}
15+
} else {
16+
const msgs = warn.mock.calls.map((args) => args[0]).join('\n - ')
17+
return {
18+
pass: false,
19+
message: () =>
20+
`expected "${received}" to have been warned.\n\nActual messages:\n\n - ${msgs}`,
21+
}
22+
}
23+
},
24+
25+
toHaveBeenWarnedLast(received: string) {
26+
asserted.add(received)
27+
const passed =
28+
warn.mock.calls[warn.mock.calls.length - 1][0].indexOf(received) > -1
29+
if (passed) {
30+
return {
31+
pass: true,
32+
message: () => `expected "${received}" not to have been warned last.`,
33+
}
34+
} else {
35+
const msgs = warn.mock.calls.map((args) => args[0]).join('\n - ')
36+
return {
37+
pass: false,
38+
message: () =>
39+
`expected "${received}" to have been warned last.\n\nActual messages:\n\n - ${msgs}`,
40+
}
41+
}
42+
},
43+
44+
toHaveBeenWarnedTimes(received: string, n: number) {
45+
asserted.add(received)
46+
let found = 0
47+
warn.mock.calls.forEach((args) => {
48+
if (args[0].indexOf(received) > -1) {
49+
found++
50+
}
51+
})
52+
53+
if (found === n) {
54+
return {
55+
pass: true,
56+
message: () =>
57+
`expected "${received}" to have been warned ${n} times.`,
58+
}
59+
} else {
60+
return {
61+
pass: false,
62+
message: () =>
63+
`expected "${received}" to have been warned ${n} times but got ${found}.`,
64+
}
65+
}
66+
},
67+
})
68+
69+
let warn: jest.SpyInstance
70+
const asserted: Set<string> = new Set()
71+
72+
beforeEach(() => {
73+
asserted.clear()
74+
warn = jest.spyOn(console, 'warn')
75+
warn.mockImplementation(() => {})
76+
})
77+
78+
afterEach(() => {
79+
const assertedArray = Array.from(asserted)
80+
const nonAssertedWarnings = warn.mock.calls
81+
.map((args) => args[0])
82+
.filter((received) => {
83+
return !assertedArray.some((assertedMsg) => {
84+
return received.indexOf(assertedMsg) > -1
85+
})
86+
})
87+
warn.mockRestore()
88+
if (nonAssertedWarnings.length) {
89+
nonAssertedWarnings.forEach((warning) => {
90+
console.warn(warning)
91+
})
92+
throw new Error(`test case threw unexpected warnings.`)
93+
}
94+
})
95+
}

0 commit comments

Comments
 (0)