-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathsetupTests.luau
More file actions
96 lines (80 loc) · 3.08 KB
/
setupTests.luau
File metadata and controls
96 lines (80 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
-- ROBLOX upstream: https://github.com/facebook/react/blob/v17.0.1/packages/react-devtools-shared/src/__tests__/setupTests.js
--[[**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
*
* LICENSE file in the root directory of this source tree.
* @flow
*]]
local Packages = script.Parent.Parent.Parent
local JestGlobals = require("@pkg/@jsdotlua/jest-globals")
local jestExpect = JestGlobals.expect
local beforeEach = JestGlobals.beforeEach
local beforeAll = JestGlobals.beforeAll
local jest = require("@pkg/@jsdotlua/jest-globals").jest
local afterEach = JestGlobals.afterEach
local LuauPolyfill = require("@pkg/@jsdotlua/luau-polyfill")
local Array = LuauPolyfill.Array
local global = _G
type Array<T> = { [number]: T }
beforeAll(function()
jestExpect.extend({
toErrorDev = require(Packages.Parent.jest.matchers.toErrorDev),
toWarnDev = require(Packages.Parent.jest.matchers.toWarnDev),
})
end)
beforeEach(function()
-- Fake timers let us flush Bridge operations between setup and assertions.
jest.useFakeTimers()
-- These files should be required (and re-required) before each test,
-- rather than imported at the head of the module.
-- That's because we reset modules between tests,
-- which disconnects the DevTool's cache from the current dispatcher ref.
local Agent = require("../backend/agent")
local initBackend = require("../backend").initBackend
local Bridge = require("../bridge")
local Store = require("../devtools/store")
local installHook = require("../hook").installHook
local utils = require("../utils")
local getDefaultComponentFilters = utils.getDefaultComponentFilters
local saveComponentFilters = utils.saveComponentFilters
-- Initialize filters to a known good state.
saveComponentFilters(getDefaultComponentFilters())
global.__REACT_DEVTOOLS_COMPONENT_FILTERS__ = getDefaultComponentFilters()
installHook(global)
local bridgeListeners = {}
local bridge = Bridge.new({
listen = function(callback)
table.insert(bridgeListeners, callback)
return function()
local index = Array.indexOf(bridgeListeners, callback)
if index >= 0 then
Array.splice(bridgeListeners, index, 1)
end
end
end,
send = function(event: string, payload: any, transferable: Array<any>?)
for _, callback in bridgeListeners do
callback({ event = event, payload = payload })
end
end,
})
local agent = Agent.new(bridge)
local hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__
initBackend(hook, agent, global)
local store = Store.new(bridge)
global.agent = agent
global.bridge = bridge
global.store = store
local ReactFeatureFlags = require("@pkg/@jsdotlua/shared").ReactFeatureFlags
ReactFeatureFlags.replayFailedUnitOfWorkWithInvokeGuardedCallback = true
end)
afterEach(function()
global.__REACT_DEVTOOLS_GLOBAL_HOOK__ = nil
-- It's important to reset modules between test runs;
-- Without this, ReactDOM won't re-inject itself into the new hook.
-- It's also important to reset after tests, rather than before,
-- so that we don't disconnect the ReactCurrentDispatcher ref.
jest.resetModules()
end)