Jun 03, 2020 11:24 AM
Hi, I am working on a custom block and would like to automate my deployments. I want to write some tests for it, but I haven’t had any success with this as of yet.
Any ideas for how this can be done?
Jun 05, 2020 05:56 AM
Welcome to the community @Graham_Vasquez!
There are a bunch of testing frameworks you can use for custom blocks. I believe the Airtable team uses Jest for their testing, and is one I’ve been using myself. The site has great documentation for how to get started, but in a nutshell, the first steps you would need to take are:
.test.js
for them to be found).jest
should do the trick. If using npm
, you can also add custom flags to a script and then run npm test
.Another popular unit testing package for Javascript is Mocha, but I think Jest was designed more closely with React in mind.
If this answers your question, please consider marking it as “solution”. If not, I’m happy to work with you further. Thanks!
Jun 06, 2020 01:54 PM
Hi Matt,
Thanks for the guidance. I am running into a bit of an issue in that when I try to run the tests it always fails giving me the following error:
@airtable/blocks can only run inside the block frame
Any thoughts on how I can test the Airtable functions with my tests?
Thanks :slightly_smiling_face:
Jun 08, 2020 04:13 AM
Ah, I see, this now looks a lot more limiting than I originally thought. I haven’t seen anything in the documentation or the community (yet) to indicate that this “offline” approach with Jest, or other similar package, would be possible for your Airtable-specific function code.
The next route I might suggest is a little more convoluted and not as nice, but should get the job done? You could build a debug/test mode into your custom block (e.g. a “Run tests” button) that appears only while you’re developing the block. This block could then iterate over any tests you’ve written, and since running within the Block itself, should have access to the Airtable functions.
Tests could be written much the same way, though probably needing plain Javascript, instead of Jest/other testing framework code. Keep tests in separate *.test.js
files that you then import into the helper function that runs the tests (i.e. just calling each test function sequentially).
Jun 08, 2020 11:54 AM
We’re still exploring the best way to test blocks. A few ideas:
layout
function that takes Array<{recordId, startDate, endDate}>
and returns information about how to lay out those items e.g. Array<{recordId, x, y}>
. You could write unit tests for this.Nov 05, 2020 08:08 AM
I haven’t yet done this myself, but ran into a similar concern with testing an app using specialized hooks. (I’m assuming it’s the hooks and their expected context that is giving you trouble.)
import { useHook } from 'some-lib'
const MyComponent = ({ foo, useSomeHook = useSome }) => {
const { bar, baz } = useSomeHook()
return (
<div
)
}
Then you kinda ignore it in your actual app code, but when doing testing or using storybookJS to isolate key states, you inject a fake mock hook that spits out whatever values you want to exercise :slightly_smiling_face:
Hopefully this is helpful!