Instructor: We have here a test project using Jest in watch mode. Currently, it's passing because there is no test case in here. What I want to do is to intercept the access to any property that starts with underscore and treated as private. We can do that using a proxy.
Let's add our first test case. We expect here that we will throw whenever we try to access the underscore ID. If we save, you can see our tests are failing, on the right.
To intercept the get access in the proxy object, we need to define the get inside the handler. The get function will accept the target and the property, and will check if the first character is underscore, we throw. Otherwise, we return the property value.
If we save now, you can see our test is passing. Now, let's try to test setting the underscore ID. We expect trying to set the underscore ID here to throw an error. If we save, you can see our tests are failing.
To fix that, we need to define the set trap function. The set trap function will receive targets, the property, and its value. Again, we can do the same check. If it starts with underscore, we throw an error. Otherwise, we set the new value and return true. This is important for proxies.