You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently needed to write some tests which made assertions on an API response which did not have fixtures (i.e. e2e tests). This meant I was testing types more than literal values.
The jest assertions were quite cumbersome and I found myself wanting to reach for OK Computer. e.g.
import{assert,object,array,string,integer,or,nul,create,is,}from'ok-computer';import'jest-extended';constmatchers=require('jest-extended');expect.extend(matchersasany);// Assume this comes from a production API so varies in length and dataconstactual={status: 200,data: {drivers: {nodes: [{id: 'dvr_1',name: 'Lewis Hamilton',team: 'Mercedes',carNumber: 44,},{id: 'dvr_2',name: 'Max Verstappen',team: 'Red Bull Racing',carNumber: 33,},{id: 'dvr_3',name: 'George Russell',team: null,carNumber: null,},{id: 'dvr_4',name: 'George Russell',team: null,carNumber: null,},],},},headers: {'Content-Length': 100,},errors: [{code: 'FOO_BAR'}],};test('Jest matchers',()=>{expect(actual).toEqual({status: 200,data: {drivers: {nodes: expect.any(Array),},},headers: expect.anything(),errors: expect.any(Array),});actual.data.drivers.nodes.forEach(driver=>{expect(driver).toEqual({id: expect.any(String),name: expect.any(String),team: expect.toBeOneOf([expect.any(String),null]),carNumber: expect.toBeOneOf([expect.any(Number),null]),});});});test('OK Computer',()=>{constany=create(()=>true)('Expected any');// TODO: Add to OK Computer libconstvalidator=object({status: is(200),data: object({drivers: object({nodes: array(object({id: string,name: string,team: or(nul,string),carNumber: or(nul,integer),})),}),}),headers: any,errors: array(any),});assert(actual,validator);});
I expect the error output is better using the jest matchers, but perhaps a custom jest matcher could handle that?
I recently needed to write some tests which made assertions on an API response which did not have fixtures (i.e. e2e tests). This meant I was testing types more than literal values.
The jest assertions were quite cumbersome and I found myself wanting to reach for OK Computer. e.g.
I expect the error output is better using the jest matchers, but perhaps a custom jest matcher could handle that?
With OK Computer you also get type inference
The text was updated successfully, but these errors were encountered: