Skip to content

Commit 44b0de5

Browse files
authoredApr 22, 2019
Slight improvements to code coverage (preactjs#1558)
* Ignore dist folder * Improve code coverage slightly
1 parent 3b7d356 commit 44b0de5

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
lines changed
 

‎debug/src/debug.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,19 @@ export function initDebug() {
130130
options.diffed = (vnode) => {
131131
if (vnode._component && vnode._component.__hooks) {
132132
let hooks = vnode._component.__hooks;
133-
if (hooks._list.length > 0) {
134-
hooks._list.forEach(hook => {
135-
if (hook._callback && (!hook._args || !Array.isArray(hook._args))) {
136-
console.warn(
137-
`In ${vnode.type.name || vnode.type} you are calling useMemo/useCallback without passing arguments.\n` +
138-
`This is a noop since it will not be able to memoize, it will execute it every render.`
139-
);
140-
}
141-
});
142-
}
133+
hooks._list.forEach(hook => {
134+
if (hook._callback && (!hook._args || !Array.isArray(hook._args))) {
135+
/* istanbul ignore next */
136+
console.warn(
137+
`In ${vnode.type.name || vnode.type} you are calling useMemo/useCallback without passing arguments.\n` +
138+
`This is a noop since it will not be able to memoize, it will execute it every render.`
139+
);
140+
}
141+
});
143142
if (hooks._pendingEffects.length > 0) {
144143
hooks._pendingEffects.forEach((effect) => {
145144
if (!effect._args || !Array.isArray(effect._args)) {
145+
/* istanbul ignore next */
146146
throw new Error('You should provide an array of arguments as the second argument to the "useEffect" hook.\n\n' +
147147
'Not doing so will invoke this effect on every render.\n\n' +
148148
'This effect can be found in the render of ' + (vnode.type.name || vnode.type) + '.');
@@ -152,6 +152,7 @@ export function initDebug() {
152152
if (hooks._pendingLayoutEffects.length > 0) {
153153
hooks._pendingLayoutEffects.forEach((layoutEffect) => {
154154
if (!layoutEffect._args || !Array.isArray(layoutEffect._args)) {
155+
/* istanbul ignore next */
155156
throw new Error('You should provide an array of arguments as the second argument to the "useEffect" hook.\n\n' +
156157
'Not doing so will invoke this effect on every render.\n\n' +
157158
'This effect can be found in the render of ' + (vnode.type.name || vnode.type) + '.');

‎debug/test/browser/debug.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ describe('debug', () => {
7777
expect(fn).to.throw(/createElement/);
7878
});
7979

80+
it('should print an error on invalid object component', () => {
81+
let fn = () => render(h({}), scratch);
82+
expect(fn).to.throw(/createElement/);
83+
});
84+
8085
it('should throw an error when using a hook outside a render', () => {
8186
class App extends Component {
8287
componentWillMount() {
@@ -172,6 +177,15 @@ describe('debug', () => {
172177
expect(warnings.length).to.equal(2);
173178
});
174179

180+
it('should warn when non-array args is passed', () => {
181+
const App = () => {
182+
const foo = useMemo(() => 'foo', 12);
183+
return <p>{foo}</p>;
184+
};
185+
render(<App />, scratch);
186+
expect(warnings[0]).to.match(/without passing arguments/);
187+
});
188+
175189
it('should print an error on invalid refs', () => {
176190
let fn = () => render(<div ref="a" />, scratch);
177191
expect(fn).to.throw(/createRef/);

‎hooks/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export function useDebugValue(value, formatter) {
201201
* Invoke a component's pending effects after the next frame renders
202202
* @type {(component: import('./internal').Component) => void}
203203
*/
204+
/* istanbul ignore next */
204205
let afterPaint = () => {};
205206

206207
/**
@@ -219,6 +220,7 @@ function scheduleFlushAfterPaint() {
219220
setTimeout(flushAfterPaintEffects, 0);
220221
}
221222

223+
/* istanbul ignore else */
222224
if (typeof window !== 'undefined') {
223225
afterPaint = (component) => {
224226
if (!component._afterPaintQueued && (component._afterPaintQueued = true) && afterPaintEffects.push(component) === 1) {

‎hooks/test/browser/useImperativeHandle.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,20 @@ describe('useImperativeHandle', () => {
4949
expect(ref.current).to.have.property('test');
5050
expect(ref.current.test()).to.equal('test1');
5151
});
52+
53+
it('should not update ref when args have not changed', () => {
54+
let ref;
55+
56+
function Comp() {
57+
ref = useRef({});
58+
useImperativeHandle(ref, () => ({ test: () => 'test' }), [1]);
59+
return <p>Test</p>;
60+
}
61+
62+
render(<Comp />, scratch);
63+
expect(ref.current.test()).to.equal('test');
64+
65+
render(<Comp />, scratch);
66+
expect(ref.current.test()).to.equal('test');
67+
});
5268
});

‎karma.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ module.exports = function(config) {
134134
exclude: [
135135
// Default config
136136
'coverage/**',
137+
'dist/**',
137138
'test/**',
138139
'test{,-*}.js',
139140
'**/*.test.js',

0 commit comments

Comments
 (0)