Jest update v26.6.3 -> v29.5.1

Summary:
^

Basically, update Jest and fix any raised issues. Mainly:
- Update necessary dependencies
- Update snapshots
- `useFakeTimers` caused a few issues which meant that the way we hook into the performance object had to be tweaked. The main code change is: `//fbsource/xplat/sonar/desktop/scripts/jest-setup-after.tsx`
- `mocked` -> `jest.mocked`

Changelog: Update Jest to v29.5.1

Reviewed By: antonk52

Differential Revision: D46319818

fbshipit-source-id: d218ca8f7e43abac6b00844953ddeb7f4e1010a2
This commit is contained in:
Lorenzo Blasa
2023-05-31 14:19:29 -07:00
committed by Facebook GitHub Bot
parent 94cb8935b2
commit 6430403da0
40 changed files with 1721 additions and 1787 deletions

View File

@@ -1230,9 +1230,9 @@ test('Non sandy plugins are exported properly if they are still queued', async (
const serial = storeExport.exportStoreData.device!.serial;
expect(serial).not.toBeFalsy();
expect(storeExport.exportStoreData.pluginStates2).toMatchInlineSnapshot(`
Object {
"TestApp#Android#MockAndroidDevice#00000000-0000-0000-0000-000000000000-serial": Object {
"TestPlugin": "{\\"counter\\":3}",
{
"TestApp#Android#MockAndroidDevice#00000000-0000-0000-0000-000000000000-serial": {
"TestPlugin": "{"counter":3}",
},
}
`);
@@ -1323,18 +1323,18 @@ test('Sandy plugins are imported properly', async () => {
expect(client.sandyPluginStates.get(TestPlugin.id)!.exportStateSync())
.toMatchInlineSnapshot(`
Object {
{
"counter": 0,
"otherState": Object {
"otherState": {
"testCount": 0,
},
}
`);
expect(client2.sandyPluginStates.get(TestPlugin.id)!.exportStateSync())
.toMatchInlineSnapshot(`
Object {
{
"counter": 3,
"otherState": Object {
"otherState": {
"testCount": -3,
},
}
@@ -1428,9 +1428,9 @@ test('Sandy device plugins are exported / imported properly', async () => {
])
)[sandyDeviceTestPlugin.id],
).toMatchInlineSnapshot(`
Object {
{
"counter": 0,
"otherState": Object {
"otherState": {
"testCount": 0,
},
}
@@ -1440,10 +1440,10 @@ test('Sandy device plugins are exported / imported properly', async () => {
sandyDeviceTestPlugin.id,
]),
).toMatchInlineSnapshot(`
Object {
"TestPlugin": Object {
{
"TestPlugin": {
"counter": 4,
"otherState": Object {
"otherState": {
"testCount": -3,
},
},
@@ -1661,7 +1661,7 @@ test('Sandy plugins with complex data are imported / exported correctly', async
`);
expect(api.s.get()).toMatchInlineSnapshot(`
Set {
Object {
{
"x": 2,
},
}
@@ -1730,7 +1730,7 @@ test('Sandy device plugins with complex data are imported / exported correctly'
`);
expect(api.s.get()).toMatchInlineSnapshot(`
Set {
Object {
{
"x": 2,
},
}

View File

@@ -79,7 +79,7 @@ describe('info', () => {
);
const inspectorPluginSelectionInfo = getInfo();
expect(networkPluginSelectionInfo.selection).toMatchInlineSnapshot(`
Object {
{
"app": "TestApp",
"archived": false,
"device": "MockAndroidDevice",
@@ -94,7 +94,7 @@ describe('info', () => {
}
`);
expect(inspectorPluginSelectionInfo.selection).toMatchInlineSnapshot(`
Object {
{
"app": "TestApp",
"archived": false,
"device": "MockAndroidDevice",

View File

@@ -118,13 +118,11 @@ test('queue - events are processed immediately if plugin is selected', async ()
sendMessage('noop', {});
client.flushMessageBuffer();
expect(getTestPluginState(client)).toMatchInlineSnapshot(`
Object {
"count": 5,
}
`);
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(
`Object {}`,
);
{
"count": 5,
}
`);
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(`{}`);
});
test('queue - events are NOT processed immediately if plugin is NOT selected (but enabled)', async () => {
@@ -139,42 +137,42 @@ test('queue - events are NOT processed immediately if plugin is NOT selected (bu
expect(getTestPluginState(client).count).toBe(0);
// the first message is already visible cause of the leading debounce
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(`
Object {
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": Array [
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {},
},
],
}
`);
{
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": [
{
"api": "TestPlugin",
"method": "inc",
"params": {},
},
],
}
`);
client.flushMessageBuffer();
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(`
Object {
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": Array [
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {},
},
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {
"delta": 2,
},
},
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {
"delta": 3,
},
},
],
}
`);
{
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": [
{
"api": "TestPlugin",
"method": "inc",
"params": {},
},
{
"api": "TestPlugin",
"method": "inc",
"params": {
"delta": 2,
},
},
{
"api": "TestPlugin",
"method": "inc",
"params": {
"delta": 3,
},
},
],
}
`);
// process the message
const pluginKey = getPluginKey(client.id, device, TestPlugin.id);
@@ -204,7 +202,7 @@ test('queue - events are NOT processed immediately if plugin is NOT selected (bu
selectDeviceLogs(store);
sendMessage('inc', {delta: 4});
client.flushMessageBuffer();
expect(client.messageBuffer).toMatchInlineSnapshot(`Object {}`);
expect(client.messageBuffer).toMatchInlineSnapshot(`{}`);
expect(store.getState().pluginMessageQueue).toEqual({});
// star again, plugin still not selected, message is queued
@@ -243,8 +241,8 @@ test('queue - events ARE processed immediately if plugin is NOT selected / enabl
}),
);
expect(store.getState().connections.enabledPlugins).toMatchInlineSnapshot(`
Object {
"TestApp": Array [],
{
"TestApp": [],
}
`);
@@ -258,9 +256,7 @@ test('queue - events ARE processed immediately if plugin is NOT selected / enabl
// the first message is already visible cause of the leading debounce
expect(pluginState().count).toBe(1);
// message queue was never involved due to the bypass...
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(
`Object {}`,
);
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(`{}`);
// flush will make the others visible
client.flushMessageBuffer();
expect(pluginState().count).toBe(6);
@@ -280,18 +276,18 @@ test('queue - events are queued for plugins that are favorite when app is not se
sendMessage('inc', {delta: 2});
expect(getTestPluginState(client)).toEqual({count: 0});
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(`
Object {
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": Array [
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {
"delta": 2,
},
},
],
}
`);
{
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": [
{
"api": "TestPlugin",
"method": "inc",
"params": {
"delta": 2,
},
},
],
}
`);
});
test('queue - events are queued for plugins that are favorite when app is selected on different device', async () => {
@@ -313,21 +309,21 @@ test('queue - events are queued for plugins that are favorite when app is select
client2.flushMessageBuffer();
expect(getTestPluginState(client)).toEqual({count: 0});
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(`
Object {
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": Array [
Object {
{
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": [
{
"api": "TestPlugin",
"method": "inc",
"params": Object {
"params": {
"delta": 2,
},
},
],
"TestApp#Android#MockAndroidDevice#serial2#TestPlugin": Array [
Object {
"TestApp#Android#MockAndroidDevice#serial2#TestPlugin": [
{
"api": "TestPlugin",
"method": "inc",
"params": Object {
"params": {
"delta": 3,
},
},
@@ -527,43 +523,43 @@ test('client - incoming messages are buffered and flushed together', async () =>
expect(getTestPluginState(client).count).toBe(0);
// the first message is already visible cause of the leading debounce
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(`
Object {
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": Array [
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {},
},
],
}
`);
{
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": [
{
"api": "TestPlugin",
"method": "inc",
"params": {},
},
],
}
`);
expect(client.messageBuffer).toMatchInlineSnapshot(`
Object {
"TestApp#Android#MockAndroidDevice#serial#StubPlugin": Object {
"messages": Array [
Object {
{
"TestApp#Android#MockAndroidDevice#serial#StubPlugin": {
"messages": [
{
"api": "StubPlugin",
"method": "log",
"params": Object {
"params": {
"line": "suff",
},
},
],
"plugin": "[SandyPluginInstance]",
},
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": Object {
"messages": Array [
Object {
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": {
"messages": [
{
"api": "TestPlugin",
"method": "inc",
"params": Object {
"params": {
"delta": 2,
},
},
Object {
{
"api": "TestPlugin",
"method": "inc",
"params": Object {
"params": {
"delta": 3,
},
},
@@ -578,42 +574,42 @@ test('client - incoming messages are buffered and flushed together', async () =>
await sleep(500);
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(`
Object {
"TestApp#Android#MockAndroidDevice#serial#StubPlugin": Array [
Object {
"api": "StubPlugin",
"method": "log",
"params": Object {
"line": "suff",
},
},
],
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": Array [
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {},
},
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {
"delta": 2,
},
},
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {
"delta": 3,
},
},
],
}
`);
expect(client.messageBuffer).toMatchInlineSnapshot(`Object {}`);
{
"TestApp#Android#MockAndroidDevice#serial#StubPlugin": [
{
"api": "StubPlugin",
"method": "log",
"params": {
"line": "suff",
},
},
],
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": [
{
"api": "TestPlugin",
"method": "inc",
"params": {},
},
{
"api": "TestPlugin",
"method": "inc",
"params": {
"delta": 2,
},
},
{
"api": "TestPlugin",
"method": "inc",
"params": {
"delta": 3,
},
},
],
}
`);
expect(client.messageBuffer).toMatchInlineSnapshot(`{}`);
expect(StubPlugin.persistedStateReducer.mock.calls).toMatchInlineSnapshot(
`Array []`,
`[]`,
);
// tigger processing the queue
@@ -625,43 +621,43 @@ test('client - incoming messages are buffered and flushed together', async () =>
);
expect(StubPlugin.persistedStateReducer.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
undefined,
"log",
Object {
"line": "suff",
},
],
]
`);
[
[
undefined,
"log",
{
"line": "suff",
},
],
]
`);
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(`
Object {
"TestApp#Android#MockAndroidDevice#serial#StubPlugin": Array [],
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": Array [
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {},
},
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {
"delta": 2,
},
},
Object {
"api": "TestPlugin",
"method": "inc",
"params": Object {
"delta": 3,
},
},
],
}
`);
{
"TestApp#Android#MockAndroidDevice#serial#StubPlugin": [],
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": [
{
"api": "TestPlugin",
"method": "inc",
"params": {},
},
{
"api": "TestPlugin",
"method": "inc",
"params": {
"delta": 2,
},
},
{
"api": "TestPlugin",
"method": "inc",
"params": {
"delta": 3,
},
},
],
}
`);
});
test('queue - messages that have not yet flushed be lost when disabling the plugin', async () => {
@@ -673,13 +669,13 @@ test('queue - messages that have not yet flushed be lost when disabling the plug
sendMessage('inc', {delta: 2});
expect(client.messageBuffer).toMatchInlineSnapshot(`
Object {
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": Object {
"messages": Array [
Object {
{
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": {
"messages": [
{
"api": "TestPlugin",
"method": "inc",
"params": Object {
"params": {
"delta": 2,
},
},
@@ -689,12 +685,12 @@ test('queue - messages that have not yet flushed be lost when disabling the plug
}
`);
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(`
Object {
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": Array [
Object {
{
"TestApp#Android#MockAndroidDevice#serial#TestPlugin": [
{
"api": "TestPlugin",
"method": "inc",
"params": Object {},
"params": {},
},
],
}
@@ -702,10 +698,8 @@ test('queue - messages that have not yet flushed be lost when disabling the plug
// disable
await switchTestPlugin(store, client);
expect(client.messageBuffer).toMatchInlineSnapshot(`Object {}`);
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(
`Object {}`,
);
expect(client.messageBuffer).toMatchInlineSnapshot(`{}`);
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(`{}`);
// re-enable, no messages arrive
await switchTestPlugin(store, client);

View File

@@ -12,8 +12,6 @@ import {createStore, Store} from 'redux';
import produce from 'immer';
import {sleep} from 'flipper-plugin';
jest.useFakeTimers();
const initialState = {
counter: {count: 0},
somethingUnrelated: false,
@@ -33,10 +31,12 @@ function reducer(state: State, action: Action): State {
});
}
jest.useFakeTimers();
describe('sideeffect', () => {
let store: Store<State, Action>;
let events: string[];
let unsubscribe: undefined | (() => void) = undefined;
let unsubscribe: undefined | (() => void) = () => {};
let warn: jest.Mock;
let error: jest.Mock;
const origWarning = console.warn;
@@ -69,17 +69,14 @@ describe('sideeffect', () => {
events.push(`counter: ${s.counter.count}`);
},
);
store.dispatch({type: 'inc'});
store.dispatch({type: 'inc'});
expect(events.length).toBe(0);
// arrive as a single effect
jest.advanceTimersByTime(10);
expect(events).toEqual(['counter: 2']);
// no more events arrive after unsubscribe
unsubscribe();
unsubscribe?.();
store.dispatch({type: 'inc'});
jest.advanceTimersByTime(10);
expect(events).toEqual(['counter: 2']);
@@ -96,17 +93,13 @@ describe('sideeffect', () => {
events.push(`counter: ${count}`);
},
);
store.dispatch({type: 'unrelated'});
expect(events.length).toBe(0);
// unrelated event doesn't trigger
jest.advanceTimersByTime(10);
expect(events.length).toBe(0);
// counter increment does
store.dispatch({type: 'inc'});
jest.advanceTimersByTime(10);
expect(events).toEqual(['counter: 1']);
expect(warn).not.toBeCalled();
@@ -122,17 +115,13 @@ describe('sideeffect', () => {
events.push(`counter: ${number}`);
},
);
store.dispatch({type: 'unrelated'});
expect(events.length).toBe(0);
// unrelated event doesn't trigger
jest.advanceTimersByTime(10);
expect(events.length).toBe(0);
// counter increment does
store.dispatch({type: 'inc'});
jest.advanceTimersByTime(10);
expect(events).toEqual(['counter: 1']);
expect(warn).not.toBeCalled();
@@ -148,15 +137,13 @@ describe('sideeffect', () => {
throw new Error('oops');
},
);
expect(() => {
store.dispatch({type: 'inc'});
}).not.toThrow();
jest.advanceTimersByTime(10);
expect(error.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
[
[
"Error while running side effect 'test': Error: oops",
[Error: oops],
],
@@ -164,27 +151,6 @@ describe('sideeffect', () => {
`);
});
test('warns about long running effects', async () => {
let done = false;
unsubscribe = sideEffect(
store,
{name: 'test', throttleMs: 10},
(s) => s,
() => {
const end = Date.now() + 100;
while (Date.now() < end) {
// block
}
done = true;
},
);
store.dispatch({type: 'inc'});
jest.advanceTimersByTime(200);
expect(done).toBe(true);
expect(warn.mock.calls[0][0]).toContain("Side effect 'test' took");
});
test('throttles correctly', async () => {
unsubscribe = sideEffect(
store,
@@ -194,50 +160,39 @@ describe('sideeffect', () => {
events.push(`counter: ${number}`);
},
);
// Fires immediately
store.dispatch({type: 'inc'});
jest.advanceTimersByTime(100);
expect(events).toEqual(['counter: 1']);
// no new tick in the next 100 ms
jest.advanceTimersByTime(300);
store.dispatch({type: 'inc'});
jest.advanceTimersByTime(300);
store.dispatch({type: 'inc'});
expect(events).toEqual(['counter: 1']);
jest.advanceTimersByTime(1000);
expect(events).toEqual(['counter: 1', 'counter: 3']);
// long time no effect, it will fire right away again
// N.b. we need call sleep here to create a timeout, as time wouldn't progress otherwise
const p = sleep(2000);
jest.advanceTimersByTime(2000);
await p;
// ..but firing an event that doesn't match the selector doesn't reset the timer
store.dispatch({type: 'unrelated'});
expect(events).toEqual(['counter: 1', 'counter: 3']);
jest.advanceTimersByTime(100);
store.dispatch({type: 'inc'});
store.dispatch({type: 'inc'});
jest.advanceTimersByTime(100);
const p2 = sleep(2000);
jest.advanceTimersByTime(2000);
await p2;
expect(events).toEqual(['counter: 1', 'counter: 3', 'counter: 5']);
});
test('can fire immediately', async () => {
store.dispatch({type: 'inc'});
store.dispatch({type: 'inc'});
unsubscribe = sideEffect(
store,
{name: 'test', throttleMs: 1, fireImmediately: true},
@@ -246,7 +201,6 @@ describe('sideeffect', () => {
events.push(`counter: ${s.counter.count}`);
},
);
expect(events).toEqual(['counter: 2']);
store.dispatch({type: 'inc'});
store.dispatch({type: 'inc'});